install 4.0 KB

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