install 5.2 KB

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