-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayjay-manager.sh
executable file
·335 lines (281 loc) · 6.65 KB
/
grayjay-manager.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env bash
# A script to manage Grayjay installations
# Configuration / Defaults
version="prototype-1"
zip_url="https://updater.grayjay.app/Apps/Grayjay.Desktop/Grayjay.Desktop-linux-x64.zip"
uid=$(id -u)
# Global Variables
verbose=false
is_system=false
command=""
tmp_dir="/tmp/grayjay-manager"
mkdir -p "$tmp_dir"
print_help() {
cat <<EOF
Usage: $(basename "$0") [options] <command>
Options:
-w, --verbose Enable verbose output
-v, --version Print the current version and exit
-h, --help Print this help message
--system System wide installation
Commands:
install Install Grayjay
remove Remove Grayjay
update Update Grayjay
check Check that Grayjay is properly installed
clean Clean up temporary files
EOF
}
# Credit: Dave Dopson, https://stackoverflow.com/a/246128/17637456
script_directory=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Clean up temporary files
cleanup() {
${1:-$verbose} && echo "Running cleanup."
if [[ -n "$tmp_dir" && -d "$tmp_dir" ]]; then
rm -rf "$tmp_dir"/*
fi
}
# Download and unpack Grayjay into a temporary directory.
fetch_gj() {
echo "Fetching latest release."
pushd "$tmp_dir" >/dev/null 2>&1
$verbose && echo "Downloading: $zip_url"
curl -sLO "$zip_url" || {
echo "Error: Failed to download from $zip_url"
cleanup
exit 1
}
local zip_file="$(basename "$zip_url")"
$verbose && echo "Unzipping: $zip_file"
unzip -q "$zip_file" || {
echo "Error: Failed to unzip $zip_file"
cleanup
exit 1
}
# If there's exactly one top-level directory, flatten it
local top_level_count
top_level_count=$(find . -mindepth 1 -maxdepth 1 -type d | wc -l | tr -d ' ')
if [[ $top_level_count -eq 1 ]]; then
local single_dir
single_dir=$(find . -mindepth 1 -maxdepth 1 -type d | head -n 1)
if [[ -n "$single_dir" && "$single_dir" != "." ]]; then
$verbose && echo "Flattening single directory: $single_dir"
shopt -s dotglob
mv "$single_dir"/* .
rmdir "$single_dir"
shopt -u dotglob
fi
fi
popd >/dev/null 2>&1
}
# Compare fetched contents in tmp_dir to the existing install directory.
# Return 0 if differences exist, 1 if no difference.
compare_fetched_to_install() {
[[ ! -d "$installation" ]] && return 0
diff -r "$tmp_dir" "$installation" >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
return 1 # no differences
else
return 0 # differences exist
fi
}
check_install() {
local output=${1:-false}
echo "Checking for Grayjay installation."
if [[ ! -d "$installation" ]]; then
$output && echo "Missing installation: $installation"
return 1
fi
$output && echo "Found Grayjay: $installation"
if [[ ! -L "$binary_link" ]]; then
$output && echo "Missing link: $binary_link"
return 1
fi
$output && echo "Found link: $binary_link"
local target="$(readlink "$binary_link")"
if [[ "$target" != "$installation/Grayjay" ]]; then
$output && echo "Invalid link target: $target"
return 1;
fi
$output && echo "Link points to binary: $target"
if [[ ! -f "$desktop" ]]; then
$output && echo "Missing application shortcut: $desktop"
return 1
fi
$output && echo "Found application shortcut: $desktop"
$output && echo "Grayjay is properly installed."
return 0;
}
do_install() {
check_install $verbose
if [[ $? -eq 0 ]]; then
echo "Grayjay is already installed."
cleanup
exit 0
fi
fetch_gj
$verbose && echo "Creating installation directory: $installation"
mkdir -p "$installation" || {
echo "Error: Failed to create directory $installation"
cleanup
exit 1
}
$verbose && echo "Copying files to $installation"
find "$tmp_dir" -mindepth 1 \( ! -name "$(basename "$zip_url")" \) -exec cp -r {} "$installation/" \; || {
echo "Error: Failed to copy files to $installation"
cleanup
exit 1
}
$verbose && echo "Creating symlink: $binary_link -> $installation/Grayjay"
ln -sf "$installation/Grayjay" "$binary_link" || {
echo "Error: Failed to create symlink $binary_link"
cleanup
exit 1
}
$verbose && echo "Creating application shortcut: $desktop"
echo "$desktop_file_content" > "$desktop" || {
echo "Error: Failed to create application shortcut"
cleanup
exit 1
}
cleanup
echo "Installation complete."
exit 0
}
do_remove() {
echo "Are you sure you want to remove Grayjay from $installation? [y/N]"
read -r confirm
case "$confirm" in
y|Y|yes|YES)
;;
*)
echo "Aborted."
cleanup
exit 0
;;
esac
[[ -d "$installation" ]] && {
$verbose && echo "Removing: $installation"
rm -rf "$installation"
}
if [[ -L "$binary_link" || -f "$binary_link" ]]; then
$verbose && echo "Removing: $binary_link"
rm -f "$binary_link"
fi
if [[ -f "$desktop" ]]; then
$verbose && echo "Removing: $desktop"
rm -f "$desktop"
fi
cleanup
echo "Removed Grayjay."
exit 0
}
do_update() {
fetch_gj
compare_fetched_to_install
if [[ $? -eq 1 ]]; then
echo "Grayjay is already up to date."
cleanup
exit 0
fi
$verbose && echo "Updating files in $installation"
mkdir -p "$installation"
rsync -a "$tmp_dir"/ "$installation"/ || {
echo "Error: Failed to sync updated files to $installation"
cleanup
exit 1
}
cleanup
echo "Grayjay updated successfully."
exit 0
}
# Argument Parsing
while [[ $# -gt 0 ]]; do
case "$1" in
-w|--verbose)
verbose=true
shift
;;
-v|--version)
echo "$version"
exit 0
;;
--system)
is_system=true
shift
;;
-h|--help)
print_help
exit 0
;;
install|remove|update|check|clean)
if [[ -z "$command" ]]; then
command="$1"
else
echo "Error: Multiple commands specified ('$command' and '$1')."
cleanup
exit 1
fi
shift
;;
*)
echo "Error: Unknown argument '$1'"
print_help
cleanup
exit 1
;;
esac
done
if [[ -z "$command" ]]; then
echo "Error: No command provided."
print_help
exit 1
fi
if [[ $uid -ne 0 && $is_system = true ]]; then
echo "Error: You must root to use --system"
exit 1
fi
if $is_system; then
installation="/opt/grayjay"
binaries="/usr/local/bin"
applications="/usr/share/applications"
else
installation="$HOME/.local/opt/grayjay"
binaries="$HOME/.local/bin"
applications="$HOME/.local/share/applications"
fi
binary_link="$binaries/grayjay"
desktop="$applications/grayjay.desktop"
desktop_file_content="[Desktop Entry]
Name=Grayjay
Exec=$installation/Grayjay --no-sandbox
Path=$installation
Icon=$installation/grayjay.png
Type=Application
Categories=Utility;
"
mkdir -p "$(dirname "$installation")" 2>/dev/null || true
# Dispatch
case "$command" in
install)
do_install
;;
remove)
do_remove
;;
update)
do_update
;;
check)
check_install true
exit $?
;;
clean)
cleanup true
;;
*)
echo "Error: Unknown command '$command'"
cleanup
exit 1
;;
esac