-
Notifications
You must be signed in to change notification settings - Fork 1
/
MiSTer_misc.sh
412 lines (358 loc) · 10.9 KB
/
MiSTer_misc.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#/usr/bin/env bash
MISC_DIR="/media/fat/misc"
LINK_DIR="/media/fat/Scripts"
SCRIPT_DIR="/media/fat/Scripts"
PACKAGE_UPDATER_OWNER="pocomane"
PACKAGE_UPDATER_NAME="MiSTer_misc"
PACKAGE_UPDATER_TYPE="github.master"
ACTION_HOOK="hook/action"
EXPOSE_HOOK="hook/expose"
BOOT_HOOK="hook/boot"
QUICK_HOOK_NAME="__unnamed__"
# DEBUG="true"
# DEBUG_MISC_SUB="/media/data/temp/MiSTer_misc_test/misc"
# DEBUG_LINK_DIR="/media/data/temp/MiSTer_misc_test/Scripts"
# DEBUG_SCRIPT_DIR="/media/data/temp/MiSTer_misc_test/Scripts"
# ---------------------------------------------------------------------------------
TMPFILE="./.download.tmp"
CURL=" curl -L -k "
# TAR=" tar --no-same-owner --no-same-permissions "
TAR=" tar --no-same-owner "
UNZIP=" unzip "
die(){
echo "ERROR $1"
exit 127
}
# All the function use the following suffix (from "Updater Script"): us_
us_init() {
echo ""
}
us_is_default_argument() {
if [ "$1" = "" -o "$1" = "." ]; then
return 0 # true when checked in a "if"
fi
return 1 # false when checked in a "if"
}
us_set_package_info() {
if [ "$DEBUG" == "true" ]; then
MISC_DIR="$DEBUG_MISC_SUB"
LINK_DIR="$DEBUG_LINK_DIR"
SCRIPT_DIR="$DEBUG_SCRIPT_DIR"
fi
PACKAGE_OWNER="$1"
PACKAGE_NAME="$2"
PACKAGE_PATTERN="$3"
PACKAGE_TYPE="$4"
PACKAGE_SIMPLENAME="$5"
# Fallback to UPDATER package (the one containing this file) when the first two
# arguments are empty
PACKAGE_FALLBACK=""
if us_is_default_argument "$PACKAGE_OWNER"; then
if us_is_default_argument "$PACKAGE_NAME"; then
PACKAGE_FALLBACK="true"
fi
fi
if [ "$PACKAGE_FALLBACK" = "true" ]; then
PACKAGE_TYPE="$PACKAGE_UPDATER_TYPE"
PACKAGE_OWNER="$PACKAGE_UPDATER_OWNER"
PACKAGE_NAME="$PACKAGE_UPDATER_NAME"
fi
# Other defaults
if us_is_default_argument "$PACKAGE_PATTERN"; then
PACKAGE_PATTERN="$PACKAGE_NAME"
fi
if us_is_default_argument "$PACKAGE_TYPE"; then
PACKAGE_TYPE="gz.tar"
fi
if us_is_default_argument "$PACKAGE_SIMPLENAME"; then
PACKAGE_SIMPLENAME="$PACKAGE_NAME"
fi
PACKAGE_REPO_SERVER="https://github.com"
PACKAGE_REPO_API_SERVER="https://api.github.com/repos"
PACKAGE_REPO="$PACKAGE_OWNER/$PACKAGE_NAME"
PACKAGE_REPO_URL="$PACKAGE_REPO_SERVER/$PACKAGE_REPO"
PACKAGE_REPO_API_URL="$PACKAGE_REPO_API_SERVER/$PACKAGE_REPO"
PACKAGE_REPO_CONTENT="https://raw.githubusercontent.com/$PACKAGE_OWNER/$PACKAGE_NAME"
PACKAGE_WORKING_DIR="$MISC_DIR/$PACKAGE_NAME"
PACKAGE_DEFAULT_SCRIPT_NAME="$PACKAGE_NAME.sh"
PACKAGE_DEFAULT_SCRIPT="$PACKAGE_WORKING_DIR/$PACKAGE_DEFAULT_SCRIPT_NAME"
PACKAGE_EXPOSE="$PACKAGE_WORKING_DIR/$EXPOSE_HOOK"
PACKAGE_ACTION="$PACKAGE_WORKING_DIR/$ACTION_HOOK"
PACKAGE_BOOT="$PACKAGE_WORKING_DIR/$BOOT_HOOK"
}
us_prepare_folder() {
if [ "$LINK_DIR" != "" ] ; then
mkdir -p "$LINK_DIR" ||die "can not create the link directory '$LINK_DIR'"
fi
if [ "$SCRIPT_DIR" != "" ] ; then
mkdir -p "$SCRIPT_DIR" ||die "can not create the script directory '$SCRIPT_DIR'"
fi
}
us_remove() {
echo "Removing $PACKAGE_NAME..."
# Remove links for the exposed hooks
if [ "$LINK_DIR" != "" ] ; then
for HOOK in $(ls "$PACKAGE_EXPOSE" 2>/dev/null) ; do
# Print error when not found, but DO NOT stop the process !
FULLPATH="$LINK_DIR/${PACKAGE_SIMPLENAME}_$HOOK"
if [ "$HOOK" = "$QUICK_HOOK_NAME" ] ; then
FULLPATH="$LINK_DIR/${PACKAGE_SIMPLENAME}"
fi
rm "$FULLPATH"
done
fi
# Remove script wrappers for the action hooks
if [ "$SCRIPT_DIR" != "" ] ; then
for HOOK in $(ls "$PACKAGE_ACTION" 2>/dev/null) ; do
# Print error when not found, but DO NOT stop the process !
FULLPATH="$SCRIPT_DIR/${PACKAGE_SIMPLENAME}_$HOOK"
if [ "$HOOK" = "$QUICK_HOOK_NAME" ] ; then
FULLPATH="$SCRIPT_DIR/${PACKAGE_SIMPLENAME}"
fi
rm "$FULLPATH"
done
fi
# Remove package content
rm -fR "$PACKAGE_WORKING_DIR"
}
us_install(){
echo "Updating $PACKAGE_NAME..."
mkdir -p "$PACKAGE_WORKING_DIR" ||die "can not create the working directory '$PACKAGE_WORKING_DIR'"
cd "$PACKAGE_WORKING_DIR" ||die "can not enter in the working direrctory '$PACKAGE_WORKING_DIR'"
# download
case $PACKAGE_TYPE in
"github.master")
PACK_URL="$PACKAGE_REPO_URL/archive/refs/heads/master.zip"
;;
*)
PACK_LIST=$($CURL -L -s "$PACKAGE_REPO_API_URL/releases/latest" | grep '"browser_download_url"' | sed 's:.*"\(.*\)"[^"]*:\1:g')
PACK_URL=$(echo "$PACK_LIST" | grep "$PACKAGE_PATTERN" | head -n 1)
PACKAGE_INFO="repo '$PACKAGE_REPO_URL' / file '$PACK_URL'"
;;
esac
$CURL "$PACK_URL" -o "$TMPFILE" ||die "can not download $PACKAGE_INFO"
# extract
case $PACKAGE_TYPE in
"bare")
# mv "$TMPFILE" "$(echo "$PACK_URL" | sed 's:^.*/::')"
mv "$TMPFILE" "$PACKAGE_PATTERN"
;;
"uudecode.xz")
uudecode -o "$TMPFILE.xz" "$TMPFILE" ||die "can not unpack $PACKAGE_INFO"
rm "$TMPFILE" ||die "can not unpack $PACKAGE_INFO"
xz --decompress "$TMPFILE.xz" ||die "can not unpack $PACKAGE_INFO"
cp "$TMPFILE" "$PACKAGE_NAME" ||die "can not unpack $PACKAGE_INFO"
;;
"gz.tar")
$TAR -xzf "$TMPFILE" ||die "can not unpack $PACKAGE_INFO"
;;
"tar")
$TAR -xf "$TMPFILE" ||die "can not unpack $PACKAGE_INFO"
;;
"github.master")
$UNZIP "$TMPFILE" ||die "can not unpack $PACKAGE_INFO"
SUBNAM="$(ls)"
mv "$SUBNAM"/* "$SUBNAM"/.[!.]* "$SUBNAM"/..?* . 2> /dev/null
rmdir "$SUBNAM" ||die "can not unpack $PACKAGE_INFO"
;;
*)
false ||die "unsupported package type"
;;
esac
if [ "$?" != "0" ]; then
false ||die "Installation failed"
fi
rm -f "$TMPFILE"
}
us_show_shortcut() {
echo "#!/usr/bin/env bash"
if [ "$SHORTCUT_INFO" != "" ] ;then
echo -e "\n# $SHORTCUT_INFO\n"
fi
cat << EOF
# Test internet
ping -c 1 www.google.com > /dev/null
if [ "\$?" != "0" ]; then
echo "Network not found: check your internet connection or try later"
exit 126
fi
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# You can simply run the following command instead of running this file
#
curl -L -k "$PACKAGE_REPO_CONTENT/master/$PACKAGE_DEFAULT_SCRIPT_NAME" | bash -s update
#
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
X=("\${PIPESTATUS[@]}")
EXIT_CODE=\${X[0]}
if [ "\$EXIT_CODE" = "0" ]; then
EXIT_CODE=\${X[1]}
fi
if [ "\$EXIT_CODE" != "0" ]; then
echo "Error downloading the package (\$EXIT_CODE)"
fi
read -n 1 -s -r -p "Press any key to continue"
echo ""
exit \$EXIT_CODE
EOF
}
us_generate_wrapper() {
cat << EOF
#!/usr/bin/env bash
cd "$PACKAGE_WORKING_DIR"
"$1" \$@
EXIT_CODE="\$?"
read -n 1 -s -r -p "Press any key to continue"
echo ""
exit \$EXIT_CODE
EOF
}
us_is_updater_package(){
if [ "$PACKAGE_OWNER" = "$PACKAGE_UPDATER_OWNER" -a "$PACKAGE_NAME" = "$PACKAGE_UPDATER_NAME" ]; then
return 0 # true when checked in a "if"
fi
return 1 # false when checked in a "if"
}
us_config() {
echo "Configuring $PACKAGE_NAME..."
if us_is_updater_package; then
# This is done with a wrapper for other packages, however the Updater is
# an exception since the "Shortcut" MUST work also withou any installation)
if [ "$SCRIPT_DIR" != "" ] ; then
us_show_shortcut > "$SCRIPT_DIR/${PACKAGE_NAME}_update.sh" ||die
fi
else
# Add link for the exposed hooks
if [ "$LINK_DIR" != "" ] ; then
for HOOK in $(ls "$PACKAGE_EXPOSE" 2>/dev/null) ; do
FULLPATH="$LINK_DIR/${PACKAGE_SIMPLENAME}_$HOOK" ||die
if [ "$HOOK" = "$QUICK_HOOK_NAME" ] ; then
FULLPATH="$LINK_DIR/${PACKAGE_SIMPLENAME}" ||die
fi
ln -s "$PACKAGE_EXPOSE/$HOOK" "$FULLPATH" ||die
done
fi
# Add script wrappers for the action hooks
if [ "$SCRIPT_DIR" != "" ] ; then
for HOOK in $(ls "$PACKAGE_ACTION" 2>/dev/null) ; do
FULLPATH="$SCRIPT_DIR/${PACKAGE_SIMPLENAME}_$HOOK" ||die
if [ "$HOOK" = "$QUICK_HOOK_NAME" ] ; then
FULLPATH="$SCRIPT_DIR/${PACKAGE_SIMPLENAME}" ||die
fi
us_generate_wrapper "$PACKAGE_ACTION/$HOOK" > "$FULLPATH" ||die
done
fi
fi
# TODO : other configs ? boot hooks ?
}
us_finish(){
echo "Done."
}
us_package_do() {
ACTION="$1"
shift
# This will fallback to the UPDATER package (i.e. the one containing this
# file) when no other parameter are given
us_set_package_info $@
case $ACTION in
"install")
us_install
;;
"remove")
us_remove
;;
"config")
us_config
;;
*)
echo "Invalid action '$1'"
exit -1;
;;
esac
}
# PACKAGE LIST
us_do_for_other() {
us_package_do "$1" pocomane webkeyboard 'arm.*tar.gz'
us_package_do "$1" pocomane MiSTer_Batch_Control 'mbc.tar.gz' . 'mbc'
us_package_do "$1" nilp0inter MiSTer_WebMenu 'webmenu.sh' uudecode.xz
us_package_do "$1" pocomane MiSTer_webmenu_package . . 'webmenu'
# TODO : add ther packages
}
us_do_for_updater() {
us_package_do "$1"
}
us_do_for_all() {
us_do_for_updater $1
us_do_for_other $1
}
us_info(){
echo "Usage Summary."
echo "To download and update the software:"
echo " $0 update"
echo "To remove the software:"
echo " $0 remove"
echo "To configure the software:"
echo " $0 config"
echo "To view a simple Updater Shortcut script:"
echo " $0 show_shortcut"
}
us_is_updater_installed() {
us_set_package_info
if [[ -x "$PACKAGE_DEFAULT_SCRIPT" ]]; then
return 0 # true when checked in a "if"
fi
return 1 # false when checked in a "if"
}
us_run_installed_updater() {
us_set_package_info
if [ "$DEBUG" == "true" ]; then
# for debug: use current script to install packages
us_main_dispatch $@
else
# for release: use the downloaded script to install packages
"$PACKAGE_DEFAULT_SCRIPT" $@ ||die
fi
}
us_main_dispatch() {
if [ "$#" = "0" ]; then
us_info
else
case $1 in
"update")
us_set_package_info
us_prepare_folder
if us_is_updater_installed; then
us_run_installed_updater remove
else
us_do_for_all remove # it will call us_remove
fi
us_do_for_updater install
us_run_installed_updater internal_installer_for_update
;;
"remove")
us_do_for_all remove # it will call us_remove
;;
"internal_installer_for_update")
us_do_for_other install # it will call us_install
us_do_for_all config # it will call us_config
;;
"config")
us_do_for_all config # it will call us_config
;;
"show_shortcut")
us_set_package_info
SHORTCUT_INFO="This file was generated by '$0 $@'"
us_show_shortcut
SHORTCUT_INFO=""
exit 0 # skip the ending summary so the result can be store in a script without any changes
;;
*)
echo "Invalid option"
us_info
false ||die "invalid option"
;;
esac
fi
}
us_init
us_main_dispatch $@
us_finish