install 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 usr/sbin/smartdns
  70. if [ $? -ne 0 ]; then
  71. return 1
  72. fi
  73. if [ -e "$PREFIX$SMARTDNS_CONF_DIR/smartdns.conf" ]; then
  74. cp etc/smartdns/smartdns.conf $PREFIX$SMARTDNS_CONF_DIR/smartdns.conf.pkg
  75. else
  76. install -v -m 0640 -t $PREFIX$SMARTDNS_CONF_DIR etc/smartdns/smartdns.conf
  77. if [ $? -ne 0 ]; then
  78. return 1
  79. fi
  80. fi
  81. install -v -m 0640 -t $PREFIX/etc/default etc/default/smartdns
  82. if [ $? -ne 0 ]; then
  83. return 1
  84. fi
  85. install -v -m 0755 -t $SMARTDNS_INIT_DIR etc/init.d/smartdns
  86. if [ $? -ne 0 ]; then
  87. if [ $ISSYSTEMD -ne 0 ]; then
  88. return 1
  89. fi
  90. fi
  91. if [ $ISSYSTEMD -eq 0 ]; then
  92. SYSTEM_UNIT_PATH="`get_systemd_path`"
  93. if [ -z "$SYSTEM_UNIT_PATH" ]; then
  94. return 1
  95. fi
  96. install -v -m 0644 -t $PREFIX$SYSTEM_UNIT_PATH systemd/smartdns.service
  97. if [ $? -ne 0 ]; then
  98. return 1
  99. fi
  100. fi
  101. return 0
  102. }
  103. uninstall_smartdns()
  104. {
  105. if [ -z "$PREFIX" ]; then
  106. stop_service 2>/dev/null
  107. fi
  108. rmdir $PREFIX$SMARTDNS_CONF_DIR 2>/dev/null
  109. rm -f $PREFIX/usr/sbin/smartdns
  110. rm -f $PREFIX/etc/default/smartdns
  111. if [ $ISWSL -eq 0 ]; then
  112. sed -i '\#%sudo ALL=NOPASSWD: /etc/init.d/smartdns#d' /etc/sudoers 2>/dev/null
  113. fi
  114. if [ $ISSYSTEMD -eq 0 ]; then
  115. SYSTEM_UNIT_PATH="`get_systemd_path`"
  116. if [ ! -z "$SYSTEM_UNIT_PATH" ]; then
  117. rm -f $PREFIX$SYSTEM_UNIT_PATH/smartdns.service
  118. fi
  119. fi
  120. if [ -z "$PREFIX" ]; then
  121. clean_service
  122. fi
  123. }
  124. install_smartdns()
  125. {
  126. local ret
  127. which smartdns >/dev/null 2>&1
  128. if [ $? -eq 0 ]; then
  129. echo "Already installed."
  130. return 1
  131. fi
  132. install_files
  133. ret=$?
  134. if [ $ret -ne 0 ]; then
  135. uninstall_smartdns
  136. return $ret
  137. fi
  138. if [ -z "$PREFIX" ]; then
  139. start_service
  140. fi
  141. if [ $ISWSL -eq 0 ]; then
  142. grep "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" /etc/sudoers >/dev/null 2>&1
  143. if [ $? -ne 0 ]; then
  144. echo "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" >> /etc/sudoers
  145. fi
  146. fi
  147. return 0
  148. }
  149. init_dir()
  150. {
  151. local ID=`id -u`
  152. if [ $ID -ne 0 ]; then
  153. echo "Please run as root."
  154. return 1
  155. fi
  156. SMARTDNS_CONF_DIR=$PREFIX/etc/smartdns
  157. SMARTDNS_INIT_DIR=$PREFIX/etc/init.d
  158. which systemctl >/dev/null 2>&1
  159. ISSYSTEMD="$?"
  160. # Running under WSL (Windows Subsystem for Linux)?
  161. cat /proc/version | grep Microsoft >/dev/null 2>&1;
  162. if [ $? -eq 0 ]; then
  163. ISSYSTEMD=1
  164. ISWSL=0
  165. fi
  166. cd $INST_DIR
  167. }
  168. main()
  169. {
  170. ACTION=""
  171. OPTS=`getopt -o iuh --long help,prefix: \
  172. -n "" -- "$@"`
  173. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  174. # Note the quotes around `$TEMP': they are essential!
  175. eval set -- "$OPTS"
  176. while true; do
  177. case "$1" in
  178. --prefix)
  179. PREFIX="$2"
  180. shift 2;;
  181. -h | --help )
  182. showhelp
  183. return 0
  184. shift ;;
  185. -i )
  186. ACTION="INSTALL"
  187. shift ;;
  188. -u )
  189. ACTION="UNINSTALL"
  190. shift ;;
  191. -- ) shift; break ;;
  192. * ) break ;;
  193. esac
  194. done
  195. init_dir
  196. if [ -z "$ACTION" ]; then
  197. showhelp
  198. return 0
  199. elif [ "$ACTION" = "INSTALL" ]; then
  200. install_smartdns
  201. return $?
  202. elif [ "$ACTION" = "UNINSTALL" ]; then
  203. uninstall_smartdns
  204. return 0
  205. fi
  206. }
  207. main $@
  208. exit $?