install 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018 Ruilin Peng (Nick) <[email protected]>
  4. #
  5. INST_DIR=$(cd $(dirname $0);pwd)
  6. showhelp()
  7. {
  8. echo "Usage: install [OPTION]"
  9. echo "Options:"
  10. echo " -i install smartdns."
  11. echo " -u uninstall smartdns."
  12. echo " --prefix [dir] prefix directory."
  13. echo " -h show this message."
  14. }
  15. start_service()
  16. {
  17. if [ $ISSYSTEMD -ne 0 ]; then
  18. chkconfig smartdns on
  19. service smartdns start
  20. return $?
  21. fi
  22. systemctl daemon-reload
  23. systemctl enable smartdns
  24. systemctl start smartdns
  25. }
  26. stop_service()
  27. {
  28. if [ $ISSYSTEMD -ne 0 ]; then
  29. service smartdns stop
  30. chkconfig smartdns off
  31. return 0
  32. fi
  33. systemctl stop smartdns
  34. systemctl disable smartdns
  35. return 0
  36. }
  37. clean_service()
  38. {
  39. if [ $ISSYSTEMD -ne 0 ]; then
  40. return 0
  41. fi
  42. systemctl daemon-reload
  43. }
  44. get_systemd_path()
  45. {
  46. service="`systemctl --no-legend| grep .service | head -n 1 | awk '{print $1}'`"
  47. SERVICE_PATH="`systemctl show $service | grep FragmentPath | awk -F'=' '{print $2}'`"
  48. dirname $SERVICE_PATH
  49. }
  50. install_files()
  51. {
  52. install -v -d $SMARTDNS_CONF_DIR
  53. if [ $? -ne 0 ]; then
  54. return 1
  55. fi
  56. install -v -m 0755 -t $PREFIX/usr/sbin src/smartdns
  57. if [ $? -ne 0 ]; then
  58. return 1
  59. fi
  60. install -v -m 0640 -t $PREFIX$SMARTDNS_CONF_DIR etc/smartdns/smartdns.conf
  61. if [ $? -ne 0 ]; then
  62. return 1
  63. fi
  64. install -v -m 0640 -t $PREFIX/etc/default etc/default/smartdns
  65. if [ $? -ne 0 ]; then
  66. return 1
  67. fi
  68. install -v -m 0755 -t $SMARTDNS_INIT_DIR etc/init.d/smartdns
  69. if [ $? -ne 0 ]; then
  70. return 1
  71. fi
  72. if [ $ISSYSTEMD -eq 0 ]; then
  73. SYSTEM_UNIT_PATH="`get_systemd_path`"
  74. if [ -z "$SYSTEM_UNIT_PATH" ]; then
  75. return 1
  76. fi
  77. install -v -m 0644 -t $PREFIX$SYSTEM_UNIT_PATH systemd/smartdns.service
  78. if [ $? -ne 0 ]; then
  79. return 1
  80. fi
  81. fi
  82. return 0
  83. }
  84. uninstall_smartdns()
  85. {
  86. if [ -z "$PREFIX" ]; then
  87. stop_service
  88. fi
  89. rm -f $PREFIX$SMARTDNS_CONF_DIR/smartdns.conf
  90. rmdir $PREFIX$SMARTDNS_CONF_DIR
  91. rm -f $PREFIX/usr/sbin/smartdns
  92. rm -f $PREFIX/etc/default/smartdns
  93. if [ $ISSYSTEMD -eq 0 ]; then
  94. SYSTEM_UNIT_PATH="`get_systemd_path`"
  95. if [ ! -z "$SYSTEM_UNIT_PATH" ]; then
  96. rm -f $PREFIX$SYSTEM_UNIT_PATH/smartdns.service
  97. fi
  98. fi
  99. if [ -z "$PREFIX" ]; then
  100. clean_service
  101. fi
  102. }
  103. install_smartdns()
  104. {
  105. local ret
  106. install_files
  107. ret=$?
  108. if [ $ret -ne 0 ]; then
  109. uninstall_smartdns
  110. return $ret
  111. fi
  112. if [ -z "$PREFIX" ]; then
  113. start_service
  114. fi
  115. return 0
  116. }
  117. init_dir()
  118. {
  119. SMARTDNS_CONF_DIR=$PREFIX/etc/smartdns
  120. SMARTDNS_INIT_DIR=$PREFIX/etc/init.d
  121. which systemctl >/dev/null 2>&1
  122. ISSYSTEMD="$?"
  123. cd $INST_DIR
  124. }
  125. main()
  126. {
  127. ACTION=""
  128. OPTS=`getopt -o iuh --long help,prefix: \
  129. -n "" -- "$@"`
  130. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  131. # Note the quotes around `$TEMP': they are essential!
  132. eval set -- "$OPTS"
  133. while true; do
  134. case "$1" in
  135. --prefix)
  136. PREFIX="$2"
  137. shift 2;;
  138. -h | --help )
  139. showhelp
  140. return 0
  141. shift ;;
  142. -i )
  143. ACTION="INSTALL"
  144. shift ;;
  145. -u )
  146. ACTION="UNINSTALL"
  147. shift ;;
  148. -- ) shift; break ;;
  149. * ) break ;;
  150. esac
  151. done
  152. init_dir
  153. if [ -z "$ACTION" ]; then
  154. showhelp
  155. return 0
  156. elif [ "$ACTION" = "INSTALL" ]; then
  157. install_smartdns
  158. return $?
  159. elif [ "$ACTION" = "UNINSTALL" ]; then
  160. uninstall_smartdns
  161. return 0
  162. fi
  163. }
  164. main $@
  165. exit $?