123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #!/bin/sh
- #
- # Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
- #
- # smartdns is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # smartdns is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- INST_DIR=$(cd $(dirname $0);pwd)
- ISWSL=1 # 1 means not WSL, 0 means wsl
- showhelp()
- {
- echo "Usage: install [OPTION]"
- echo "Options:"
- echo " -i install smartdns."
- echo " -u uninstall smartdns."
- echo " -U upgrade install smartdns."
- echo " --prefix [dir] prefix directory."
- echo " -h show this message."
- }
- start_service()
- {
- if [ $ISSYSTEMD -ne 0 ]; then
- chkconfig smartdns on >/dev/null 2>&1
- service smartdns start
- return $?
- fi
- systemctl daemon-reload
- systemctl enable smartdns
- systemctl start smartdns
- }
- stop_service()
- {
- if [ $ISSYSTEMD -ne 0 ]; then
- service smartdns stop
- chkconfig smartdns off >/dev/null 2>&1
- return 0
- fi
- systemctl stop smartdns
- systemctl disable smartdns
- return 0
- }
- clean_service()
- {
- if [ $ISSYSTEMD -ne 0 ]; then
- return 0
- fi
- systemctl daemon-reload
- }
- get_systemd_path()
- {
- service="`systemctl --no-legend| grep '\.service' | head -n 1 | awk '{print $1}' 2>/dev/null`"
- SERVICE_PATH="`systemctl show $service | grep FragmentPath | awk -F'=' '{print $2}' 2>/dev/null`"
- if [ ! -z "$SERVICE_PATH" ]; then
- SERVICE_PATH="`dirname $SERVICE_PATH 2>/dev/null`"
- if [ -d "$SERVICE_PATH" ]; then
- echo "$SERVICE_PATH"
- return 0
- fi
- fi
- SERVICE_PATH="`pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null`"
- if [ ! -z "$SERVICE_PATH" ]; then
- if [ -d "$SERVICE_PATH" ]; then
- echo "$SERVICE_PATH"
- return 0
- fi
- fi
- SERVICE_PATH="/lib/systemd/system"
- if [ -d "$SERVICE_PATH" ]; then
- echo "$SERVICE_PATH"
- return 0
- fi
- return 1
- }
- install_files()
- {
- install -v -d $SMARTDNS_CONF_DIR
- if [ $? -ne 0 ]; then
- return 1
- fi
- install -v -m 0755 -t $PREFIX/usr/sbin usr/sbin/smartdns
- if [ $? -ne 0 ]; then
- return 1
- fi
- if [ -e "$PREFIX$SMARTDNS_CONF_DIR/smartdns.conf" ]; then
- cp etc/smartdns/smartdns.conf $PREFIX$SMARTDNS_CONF_DIR/smartdns.conf.pkg
- else
- install -v -m 0640 -t $PREFIX$SMARTDNS_CONF_DIR etc/smartdns/smartdns.conf
- if [ $? -ne 0 ]; then
- return 1
- fi
- fi
- install -v -m 0640 -t $PREFIX/etc/default etc/default/smartdns
- if [ $? -ne 0 ]; then
- return 1
- fi
-
- install -v -m 0755 -t $SMARTDNS_INIT_DIR etc/init.d/smartdns
- if [ $? -ne 0 ]; then
- if [ $ISSYSTEMD -ne 0 ]; then
- return 1
- fi
- fi
- if [ $ISSYSTEMD -eq 0 ]; then
- SYSTEM_UNIT_PATH="`get_systemd_path`"
- if [ -z "$SYSTEM_UNIT_PATH" ]; then
- echo "cannot find systemd path"
- return 1
- fi
- install -v -m 0644 -t $PREFIX$SYSTEM_UNIT_PATH systemd/smartdns.service
- if [ $? -ne 0 ]; then
- return 1
- fi
- fi
- return 0
- }
- uninstall_smartdns()
- {
- if [ -z "$PREFIX" ]; then
- stop_service 2>/dev/null
- fi
- rmdir $PREFIX$SMARTDNS_CONF_DIR 2>/dev/null
- rm -f $PREFIX/usr/sbin/smartdns
- rm -f $PREFIX/etc/default/smartdns
- rm -f $PREFIX/etc/init.d/smartdns
- if [ $ISWSL -eq 0 ]; then
- sed -i '\#%sudo ALL=NOPASSWD: /etc/init.d/smartdns#d' /etc/sudoers 2>/dev/null
- fi
- if [ $ISSYSTEMD -eq 0 ]; then
- SYSTEM_UNIT_PATH="`get_systemd_path`"
- if [ ! -z "$SYSTEM_UNIT_PATH" ]; then
- rm -f $PREFIX$SYSTEM_UNIT_PATH/smartdns.service
- fi
- fi
- if [ -z "$PREFIX" ]; then
- clean_service
- fi
- }
- install_smartdns()
- {
- local ret
- which smartdns >/dev/null 2>&1
- if [ $? -eq 0 ]; then
- echo "Already installed."
- return 1
- fi
- install_files
- ret=$?
- if [ $ret -ne 0 ]; then
- uninstall_smartdns
- return $ret
- fi
- if [ -z "$PREFIX" ]; then
- start_service
- fi
- if [ $ISWSL -eq 0 ]; then
- grep "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" /etc/sudoers >/dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" >> /etc/sudoers
- fi
- fi
- return 0
- }
- init_dir()
- {
- local ID=`id -u`
- if [ $ID -ne 0 ]; then
- echo "Please run as root."
- return 1
- fi
- SMARTDNS_CONF_DIR=$PREFIX/etc/smartdns
- SMARTDNS_INIT_DIR=$PREFIX/etc/init.d
- which systemctl >/dev/null 2>&1
- ISSYSTEMD="$?"
- # Running under WSL (Windows Subsystem for Linux)?
- cat /proc/version | grep -E '[Mm]icrosoft' >/dev/null 2>&1;
- if [ $? -eq 0 ]; then
- ISSYSTEMD=1
- ISWSL=0
- fi
- cd $INST_DIR
- }
- main()
- {
- ACTION=""
- OPTS=`getopt -o iuhU --long help,prefix: \
- -n "" -- "$@"`
- if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
- # Note the quotes around `$TEMP': they are essential!
- eval set -- "$OPTS"
- while true; do
- case "$1" in
- --prefix)
- PREFIX="$2"
- shift 2;;
- -h | --help )
- showhelp
- return 0
- shift ;;
- -i )
- ACTION="INSTALL"
- shift ;;
- -u )
- ACTION="UNINSTALL"
- shift ;;
- -U )
- ACTION="UPGRADE"
- shift ;;
- -- ) shift; break ;;
- * ) break ;;
- esac
- done
- init_dir
- if [ -z "$ACTION" ]; then
- showhelp
- return 0
- elif [ "$ACTION" = "INSTALL" ]; then
- install_smartdns
- return $?
- elif [ "$ACTION" = "UNINSTALL" ]; then
- uninstall_smartdns
- return 0
- elif [ "$ACTION" = "UPGRADE" ]; then
- uninstall_smartdns
- install_smartdns
- return $?
- else
- showhelp
- return 1
- fi
- }
- main $@
- exit $?
|