-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
246 lines (205 loc) · 4.61 KB
/
install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/sh
#
# Copyright (C) 2018-2020 Ruilin Peng (Nick) <pymumu@gmail.com>.
#
# 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 " --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}'`"
SERVICE_PATH="`systemctl show $service | grep FragmentPath | awk -F'=' '{print $2}'`"
dirname $SERVICE_PATH
}
install_files()
{
install -v -d $SMARTDNS_CONF_DIR
if [ $? -ne 0 ]; then
return 1
fi
install -v -m 0755 -t $PREFIX/usr/sbin src/smartdns
if [ $? -ne 0 ]; then
return 1
fi
install -v -m 0640 -t $PREFIX$SMARTDNS_CONF_DIR etc/smartdns/smartdns.conf
if [ $? -ne 0 ]; then
return 1
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
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
rm -f $PREFIX$SMARTDNS_CONF_DIR/smartdns.conf
rmdir $PREFIX$SMARTDNS_CONF_DIR 2>/dev/null
rm -f $PREFIX/usr/sbin/smartdns
rm -f $PREFIX/etc/default/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 Microsoft >/dev/null 2>&1;
if [ $? -eq 0 ]; then
ISSYSTEMD=1
ISWSL=0
fi
cd $INST_DIR
}
main()
{
ACTION=""
OPTS=`getopt -o iuh --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 ;;
-- ) 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
fi
}
main $@
exit $?