install 4.6 KB

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