-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·279 lines (228 loc) · 9.12 KB
/
install.sh
File metadata and controls
executable file
·279 lines (228 loc) · 9.12 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICE_NAME="zenbook-duo-daemon"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
PRE_SLEEP_SERVICE_NAME="zenbook-duo-daemon-pre-sleep"
PRE_SLEEP_SERVICE_FILE="/etc/systemd/system/${PRE_SLEEP_SERVICE_NAME}.service"
POST_SLEEP_SERVICE_NAME="zenbook-duo-daemon-post-sleep"
POST_SLEEP_SERVICE_FILE="/etc/systemd/system/${POST_SLEEP_SERVICE_NAME}.service"
INSTALL_DIR="/opt/zenbook-duo-daemon"
BINARY_PATH="${INSTALL_DIR}/zenbook-duo-daemon"
GITHUB_REPO="https://github.com/PegasisForever/zenbook-duo-daemon/releases/latest/download"
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root" >&2
exit 1
fi
}
uninstall() {
check_root
echo "Uninstalling ${SERVICE_NAME}..."
# Stop and disable main service if it exists
if systemctl is-active --quiet "${SERVICE_NAME}" 2>/dev/null; then
echo "Stopping service..."
systemctl stop "${SERVICE_NAME}" || true
fi
if systemctl is-enabled --quiet "${SERVICE_NAME}" 2>/dev/null; then
echo "Disabling service..."
systemctl disable "${SERVICE_NAME}" || true
fi
# Stop and disable pre-sleep service if it exists
if systemctl is-active --quiet "${PRE_SLEEP_SERVICE_NAME}" 2>/dev/null; then
echo "Stopping pre-sleep service..."
systemctl stop "${PRE_SLEEP_SERVICE_NAME}" || true
fi
if systemctl is-enabled --quiet "${PRE_SLEEP_SERVICE_NAME}" 2>/dev/null; then
echo "Disabling pre-sleep service..."
systemctl disable "${PRE_SLEEP_SERVICE_NAME}" || true
fi
# Stop and disable post-sleep service if it exists
if systemctl is-active --quiet "${POST_SLEEP_SERVICE_NAME}" 2>/dev/null; then
echo "Stopping post-sleep service..."
systemctl stop "${POST_SLEEP_SERVICE_NAME}" || true
fi
if systemctl is-enabled --quiet "${POST_SLEEP_SERVICE_NAME}" 2>/dev/null; then
echo "Disabling post-sleep service..."
systemctl disable "${POST_SLEEP_SERVICE_NAME}" || true
fi
# Remove service files
if [ -f "${SERVICE_FILE}" ]; then
echo "Removing main service file..."
rm -f "${SERVICE_FILE}"
fi
if [ -f "${PRE_SLEEP_SERVICE_FILE}" ]; then
echo "Removing pre-sleep service file..."
rm -f "${PRE_SLEEP_SERVICE_FILE}"
fi
if [ -f "${POST_SLEEP_SERVICE_FILE}" ]; then
echo "Removing post-sleep service file..."
rm -f "${POST_SLEEP_SERVICE_FILE}"
fi
systemctl daemon-reload
# Remove installation directory
if [ -d "${INSTALL_DIR}" ]; then
echo "Removing installation directory..."
rm -rf "${INSTALL_DIR}"
fi
echo "Uninstallation complete."
}
install() {
check_root
echo "Installing ${SERVICE_NAME}..."
# Uninstall old version first
echo "Uninstalling old version..."
uninstall
# Create installation directory
echo "Creating installation directory..."
mkdir -p "${INSTALL_DIR}"
# Download binary
echo "Downloading binary from ${GITHUB_REPO}/zenbook-duo-daemon..."
if ! curl -fSL -o "${BINARY_PATH}" "${GITHUB_REPO}/zenbook-duo-daemon"; then
echo "Error: Failed to download binary" >&2
exit 1
fi
if [ ! -s "${BINARY_PATH}" ]; then
echo "Error: Downloaded binary is empty" >&2
rm -f "${BINARY_PATH}"
exit 1
fi
chmod +x "${BINARY_PATH}"
# Download main service file
echo "Downloading service file from ${GITHUB_REPO}/zenbook-duo-daemon.service..."
if ! curl -fSL -o "${SERVICE_FILE}" "${GITHUB_REPO}/zenbook-duo-daemon.service"; then
echo "Error: Failed to download service file" >&2
rm -f "${BINARY_PATH}"
exit 1
fi
if [ ! -s "${SERVICE_FILE}" ]; then
echo "Error: Downloaded service file is empty" >&2
rm -f "${BINARY_PATH}" "${SERVICE_FILE}"
exit 1
fi
# Download pre-sleep service file
echo "Downloading pre-sleep service file from ${GITHUB_REPO}/zenbook-duo-daemon-pre-sleep.service..."
if ! curl -fSL -o "${PRE_SLEEP_SERVICE_FILE}" "${GITHUB_REPO}/zenbook-duo-daemon-pre-sleep.service"; then
echo "Error: Failed to download pre-sleep service file" >&2
rm -f "${BINARY_PATH}" "${SERVICE_FILE}"
exit 1
fi
if [ ! -s "${PRE_SLEEP_SERVICE_FILE}" ]; then
echo "Error: Downloaded pre-sleep service file is empty" >&2
rm -f "${BINARY_PATH}" "${SERVICE_FILE}" "${PRE_SLEEP_SERVICE_FILE}"
exit 1
fi
# Download post-sleep service file
echo "Downloading post-sleep service file from ${GITHUB_REPO}/zenbook-duo-daemon-post-sleep.service..."
if ! curl -fSL -o "${POST_SLEEP_SERVICE_FILE}" "${GITHUB_REPO}/zenbook-duo-daemon-post-sleep.service"; then
echo "Error: Failed to download post-sleep service file" >&2
rm -f "${BINARY_PATH}" "${SERVICE_FILE}" "${PRE_SLEEP_SERVICE_FILE}"
exit 1
fi
if [ ! -s "${POST_SLEEP_SERVICE_FILE}" ]; then
echo "Error: Downloaded post-sleep service file is empty" >&2
rm -f "${BINARY_PATH}" "${SERVICE_FILE}" "${PRE_SLEEP_SERVICE_FILE}" "${POST_SLEEP_SERVICE_FILE}"
exit 1
fi
# Run migrate-config command
echo "Running config migration..."
"${BINARY_PATH}" migrate-config || {
echo "Warning: Config migration failed, continuing anyway..." >&2
}
# Reload systemd and enable/start services
echo "Reloading systemd daemon..."
systemctl daemon-reload
echo "Enabling services..."
systemctl enable "${SERVICE_NAME}"
systemctl enable "${PRE_SLEEP_SERVICE_NAME}"
systemctl enable "${POST_SLEEP_SERVICE_NAME}"
echo "Starting main service..."
systemctl start "${SERVICE_NAME}"
echo "Installation complete."
echo "Service status:"
systemctl status "${SERVICE_NAME}" --no-pager || true
}
local_install() {
check_root
if [ -z "$1" ]; then
echo "Error: local-install requires a binary path argument" >&2
echo "Usage: $0 local-install <binary-path>" >&2
exit 1
fi
local binary_path="$1"
local service_file_path="${SCRIPT_DIR}/zenbook-duo-daemon.service"
local pre_sleep_service_file_path="${SCRIPT_DIR}/zenbook-duo-daemon-pre-sleep.service"
local post_sleep_service_file_path="${SCRIPT_DIR}/zenbook-duo-daemon-post-sleep.service"
if [ ! -f "${binary_path}" ]; then
echo "Error: Binary file not found: ${binary_path}" >&2
exit 1
fi
if [ ! -f "${service_file_path}" ]; then
echo "Error: Service file not found: ${service_file_path}" >&2
exit 1
fi
if [ ! -f "${pre_sleep_service_file_path}" ]; then
echo "Error: Pre-sleep service file not found: ${pre_sleep_service_file_path}" >&2
exit 1
fi
if [ ! -f "${post_sleep_service_file_path}" ]; then
echo "Error: Post-sleep service file not found: ${post_sleep_service_file_path}" >&2
exit 1
fi
echo "Installing ${SERVICE_NAME} from local files..."
# Uninstall old version first
echo "Uninstalling old version..."
uninstall
# Create installation directory
echo "Creating installation directory..."
mkdir -p "${INSTALL_DIR}"
# Copy binary
echo "Copying binary from ${binary_path}..."
cp "${binary_path}" "${BINARY_PATH}"
chmod +x "${BINARY_PATH}"
# Copy service files
echo "Copying service file from ${service_file_path}..."
cp "${service_file_path}" "${SERVICE_FILE}"
echo "Copying pre-sleep service file from ${pre_sleep_service_file_path}..."
cp "${pre_sleep_service_file_path}" "${PRE_SLEEP_SERVICE_FILE}"
echo "Copying post-sleep service file from ${post_sleep_service_file_path}..."
cp "${post_sleep_service_file_path}" "${POST_SLEEP_SERVICE_FILE}"
# Run migrate-config command
echo "Running config migration..."
"${BINARY_PATH}" migrate-config || {
echo "Warning: Config migration failed, continuing anyway..." >&2
}
# Reload systemd and enable/start services
echo "Reloading systemd daemon..."
systemctl daemon-reload
echo "Enabling services..."
systemctl enable "${SERVICE_NAME}"
systemctl enable "${PRE_SLEEP_SERVICE_NAME}"
systemctl enable "${POST_SLEEP_SERVICE_NAME}"
echo "Starting main service..."
systemctl start "${SERVICE_NAME}"
echo "Installation complete."
echo "Service status:"
systemctl status "${SERVICE_NAME}" --no-pager || true
}
# Main command dispatcher
case "${1:-}" in
uninstall)
uninstall
;;
install)
install
;;
local-install)
local_install "$2"
;;
*)
echo "Usage: $0 {uninstall|install|local-install <binary-path>}" >&2
echo "" >&2
echo "Commands:" >&2
echo " uninstall Remove service files and delete /opt/zenbook-duo-daemon" >&2
echo " install Download and install from GitHub releases" >&2
echo " local-install Install using local binary and service files" >&2
exit 1
;;
esac