-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·217 lines (196 loc) · 6.58 KB
/
install.sh
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
#!/usr/bin/env bash
# This is the name of the process we assume to be always present and to have been run for the current
# user. For a headless install, this might be different.
PERVASIVE_PROCESS="${NORDVPN_RECONNECTOR_PREVASIVE_PROfCESS-Xorg}"
# This is where we will copy the executable script.
INSTALL_LOCATION="${NORDVPN_RECONNECTOR_INSTALL_LOCATION-/opt/nordvpn-reconnector}"
# This is the location of the symbolic link in PATH.
PATH_BINARY="${NORDVPN_RECONNECTOR_PATH_BINARY-/usr/bin/nordvpn-reconnector}"
PATH_BINARY_KILLER="${NORDVPN_RECONNECTOR_PATH_BINARY_KILLER-/usr/bin/nordvpnd-killer}"
app_check_root() {
[[ "$(id -u)" != "0" ]] && echo "This script must be executed as root." && exit 1
}
app_check_install() {
if [[ ! -f "${INSTALL_LOCATION}/nordvpn-reconnect.sh" ]]; then
echo "Service executable has not been installed. Please run $0 install first"
exit 0
fi
if [[ ! -f "${INSTALL_LOCATION}/nordvpnd-killer.sh" ]]; then
echo "Service executable has not been installed. Please run $0 install first"
exit 0
fi
if [[ ! -f "${PATH_BINARY}" ]]; then
echo "Service executable has not been installed. Please run $0 install first"
exit 0
fi
if [[ ! -f "${PATH_BINARY_KILLER}" ]]; then
echo "Service executable has not been installed. Please run $0 install first"
exit 0
fi
if [[ ! -f /etc/systemd/user/nordvpn-reconnector.service ]]; then
echo "Service unit has not been installed. Please run $0 install first"
exit 0
fi
if [[ ! -f /usr/lib/systemd/system/nordvpnd-killer.service ]]; then
echo "Service unit has not been installed. Please run $0 install first"
exit 0
fi
}
app_current_user() {
# shellcheck disable=SC2009
ps aux | grep -v grep | grep "${PERVASIVE_PROCESS}" | head -n 1 | cut -d' ' -f 1
}
app_systemctl_command() {
local verbose="false"
if [[ "$1" == "-v" ]]; then
verbose="true"
shift
fi
local command="$1"
command="systemctl ${command} --machine=$(app_current_user)@.host --user nordvpn-reconnector.service"
if [[ "${verbose}" != "true" ]]; then
command="${command} > /dev/null"
fi
eval "${command} 2>&1"
}
app_systemctl_link() {
app_systemctl_command -v "status" | app_systemctl_status
}
app_systemctl_status() {
grep -E "nordvpn-reconnector.service;\s*(enabled|disabled)" | sed -E "s/^.*nordvpn-reconnector.service;\s*(enabled|disabled).*$/\1/"
}
app_install() {
app_check_root
app_uninstall
cd "$(dirname "${0}")" || exit 1
echo " - Installing nordvpn-connector"
echo " - Copying the script to ${INSTALL_LOCATION}"
mkdir -p "${INSTALL_LOCATION}"
cp "$(pwd)/nordvpn-reconnect.sh" "${INSTALL_LOCATION}"
cp "$(pwd)/nordvpnd-killer.sh" "${INSTALL_LOCATION}"
cp "$(pwd)/icon.png" "${INSTALL_LOCATION}"
chmod +x "${INSTALL_LOCATION}/nordvpn-reconnect.sh"
chmod +x "${INSTALL_LOCATION}/nordvpnd-killer.sh"
echo " - Adding symbolic link at ${PATH_BINARY}"
ln -s "${INSTALL_LOCATION}/nordvpn-reconnect.sh" "${PATH_BINARY}"
echo " - Adding symbolic link at ${PATH_BINARY_KILLER}"
ln -s "${INSTALL_LOCATION}/nordvpnd-killer.sh" "${PATH_BINARY_KILLER}"
echo " - Copying service unit definition for all users"
cp "$(pwd)/nordvpn-reconnector.service" /etc/systemd/user/nordvpn-reconnector.service
cp "$(pwd)/nordvpnd-killer.service" /usr/lib/systemd/system/nordvpnd-killer.service
}
app_uninstall() {
app_check_root
echo " - Uninstalling nordvpn-reconnector"
if pgrep -f nordvpn-reconnector >/dev/null; then
echo
echo "Service nordvpn-reconnector is still running in the background. Please stop it first using:"
echo " $0 stop"
exit 1
fi
if pgrep -f nordvpnd-killer >/dev/null; then
echo
echo "Service nordvpn-killer is still running in the background. Please stop it first using:"
echo " $0 stop"
exit 1
fi
if [[ "$(app_systemctl_link)" == "enabled" ]]; then
echo
echo "Service nordvpn-reconnector is still enabled. Removing it may cause issues on next login."
echo "Please disable it first by running:"
echo " $0 disable"
exit 0
fi
if [[ "$(systemctl status nordvpnd-killer 2>&1 | app_systemctl_status)" == "enabled" ]]; then
echo
echo "Service nordvpnd-killer is still enabled. Removing it may cause issues on next login."
echo "Please disable it first by running:"
echo " $0 disable"
exit 0
fi
echo " - Removing the executable"
rm -rf "${INSTALL_LOCATION}"
echo " - Removing the symbolic link"
rm -rf "${PATH_BINARY}"
rm -rf "${PATH_BINARY_KILLER}"
echo " - Removing the service unit definition"
rm -rf /etc/systemd/user/nordvpn-reconnector.service
rm -rf /usr/lib/systemd/system/nordvpnd-killer.service
}
app_enable() {
app_check_install
echo " - Enabling service nordvpn-reconnector.service"
app_systemctl_command enable
echo " - Enabling service nordvpnd-killer.service"
systemctl enable nordvpnd-killer.service > /dev/null 2>&1
}
app_start() {
app_check_install
echo " - Starting service nordvpn-reconnector.service"
app_systemctl_command start
echo " - Starting service nordvpnd-killer.service"
systemctl start nordvpnd-killer.service > /dev/null 2>&1
}
app_disable() {
echo " - Disabling service nordvpn-reconnector.service"
app_systemctl_command disable
echo " - Disabling service nordvpnd-killer.service"
systemctl disable nordvpnd-killer.service > /dev/null 2>&1
}
app_stop() {
echo " - Stopping service nordvpn-reconnector.service"
app_systemctl_command stop
echo " - Stopping service nordvpnd-killer.service"
systemctl stop nordvpnd-killer.service > /dev/null 2>&1
}
app_run() {
local command="$1"
case "${command}" in
install)
app_install
;;
uninstall)
app_uninstall
;;
enable)
app_enable
;;
disable)
app_disable
;;
start)
app_start
;;
stop)
app_stop
;;
*)
echo "Unknown command ${command}"
exit 1
;;
esac
}
app_main() {
if [[ $# == 0 ]]; then
echo "Usage: $0 <command1> [<command2> <command3> ...]"
echo
echo "Available commands are:"
echo " - install: installs this library into ${INSTALL_LOCATION}"
echo " - uninstall: uninstalls the library"
echo " - enable: enables the service for the current user"
echo " - disable: disables the service for the current user"
echo " - start: starts the service for the current user"
echo " - stop: stops the service for the current user"
echo
echo "For example, to install, enable, and then start the service run:"
echo " $0 install enable start"
echo
echo "See README.md for more details."
exit 0
fi
while [[ "$#" -gt 0 ]]; do
app_run "$1"
shift
done
}
app_main "${@}"