forked from Botspot/pi-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater
executable file
·514 lines (402 loc) · 17.2 KB
/
updater
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/bash
{ #prevents errors if script was modified while in use
DIRECTORY="$(readlink -f "$(dirname "$0")")"
function error {
echo -e "\e[91m$1\e[39m"
exit 1
}
#for the will_reinstall() and list_intersect() functions
source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api"
get_date() {
#number of days since 1/1/1970
echo $(($(date --utc +%s)/86400))
}
check_update_interval() { #return 0 if update-interval allows update-checking today, exit 1 otherwise
local lastupdatecheck="$(cat "${DIRECTORY}/data/last-update-check" 2>/dev/null)"
if [ -z $lastupdatecheck ];then
warning "${DIRECTORY}/data/last-update-check does not exist!" 1>&2
lastupdatecheck=0
fi
#write today's date to file. Format is "number of days since 1/1/1970"
get_date > "${DIRECTORY}/data/last-update-check"
local updateinterval="$(cat "${DIRECTORY}/data/settings/Check for updates")"
#allowed values: Always, Daily, Weekly, Never
if [ "$updateinterval" == 'Never' ];then
return 1
elif [ "$updateinterval" == 'Daily' ];then
#if updates checked today, don't check
if [ "$(get_date)" == "$lastupdatecheck" ];then
return 1
fi
elif [ "$updateinterval" == 'Weekly' ];then
#if updates checked less than 7 days ago, don't check
if [ "$(get_date)" -le "$((lastupdatecheck + 7))" ];then
return 1
fi
elif [ "$updateinterval" == 'Always' ];then
return 0
elif [ -z "$updateinterval" ];then
warning "Something isn't right. Does '${DIRECTORY}/data/settings/Check for updates' exist?" 1>&2
else
echo -e "\e[91mUnrecognized update interval! \e[0m" 1>&2
fi
return 0
}
list_files() { #list all files on pi-apps with relative paths - both on main directory and update directory
if [ ! -d "${DIRECTORY}/update" ];then
error "${DIRECTORY}/update does not exist. Most likely there is no Internet connection."
fi
#list all files in update folder
cd "${DIRECTORY}/update/pi-apps" || error "Failed to enter update directory!"
local updatefiles="$(find . -type f | cut -c 3- | grep -v '.git/' | grep -v 'apps/' | grep -v 'data/')"
#list all files in main folder
cd "${DIRECTORY}"
local localfiles="$(find . -type f | cut -c 3- | grep -v '.git/' | grep -v 'apps/' | grep -v 'data/' | grep -v 'logs/' | grep -v 'xlunch/')"
echo -e "${localfiles}\n${updatefiles}" | sort | uniq
cd $HOME
}
get_updatable_files() { #sets the updatable_files variable
echo -n "Scanning files... " 1>&2
#get list of pi-apps files without absolute paths. Example line: 'etc/terminal-run'
local file_list="$(list_files)" || exit 1
updatable_files='' #the variable to be returned
#exclude files mentioned in data/update-exclusion file, ignoring any commented lines
local IFS=$'\n'
if [ "$speed" == fast ] && [ -f "${DIRECTORY}/data/update-status/updatable-files" ];then
#speed is set to 'fast' - don't hash anything but rely on past results
updatable_files="$(cat "${DIRECTORY}/data/update-status/updatable-files")"
else #speed was not set to 'fast', so compare each file to the one in the update folder
for file in $file_list ;do
echo -en "Scanning files... $file\033[0K\r" 1>&2
if [ ! -f "${DIRECTORY}/${file}" ];then
#file is missing locally - add to updatable_files list
updatable_files="${updatable_files}
${file}"
elif [ ! -f "${DIRECTORY}/update/pi-apps/${file}" ];then
#file is missing in the update folder - local
true #do not add to updatable_apps list
elif ! diff "${DIRECTORY}/update/pi-apps/${file}" "${DIRECTORY}/${file}" -q >/dev/null ;then
#files don't match - add to updatable_files list
updatable_files="${updatable_files}
${file}"
fi
done
#remove initial newline character
updatable_files="${updatable_files:1}"
fi
#If an updatable file is listed in update-exclusion, remove it from list and save notification text for later.
for file in $(cat "${DIRECTORY}/data/update-exclusion" | grep "^[^#;]") ;do
updatable_files="$(echo "$updatable_files" | grep -v "$file")"
local exclusion_msg="$exclusion_msg\n'$file' won't be updated - it's listed in data/update-exclusion."
done
echo "$updatable_files"
echo -e "Scanning files... Done\033[0K" 1>&2
#if any files were excluded by update-exclusion, list them now, after echoing "Done"
[ ! -z "$exclusion_msg" ] && echo -e "$exclusion_msg\n" 1>&2
return 0
}
get_updatable_apps() { #sets the updatable_apps variable
if [ "$speed" == fast ] && [ -f "${DIRECTORY}/data/update-status/updatable-apps" ];then
#speed is set to 'fast' - don't hash anything but rely on past results
cat "${DIRECTORY}/data/update-status/updatable-apps"
else #speed was not set to 'fast', so compare each file to the one in the update folder
updatable_apps="$("${DIRECTORY}/manage" check-all)"
local exitcode=$?
echo "$updatable_apps"
exit $exitcode #return the same code that the manage script exited with
fi
}
generate_yad_list() { #input: updatable_apps and updatable_files variables
local IFS=$'\n'
#If updatable_apps and updatable_files variables empty, set them to the results of the last update-check
if [ -z "$updatable_apps" ] && [ -z "$updatable_files" ];then
updatable_apps="$(cat "${DIRECTORY}/data/update-status/updatable-apps")"
updatable_files="$(cat "${DIRECTORY}/data/update-status/updatable-files")"
fi
for app in $updatable_apps ;do #generate a yad list for every updatable app
echo "TRUE
${DIRECTORY}/update/pi-apps/apps/${app}/icon-24.png
$app ($([ ! -d "${DIRECTORY}/apps/${app}" ] && echo 'new ')app$(will_reinstall "$app" && echo ', <b>will be reinstalled</b>'))
app:$app"
done
for file in $updatable_files ;do #generate a yad list for every updatable file
#determine mimetype of updatable_apps file to display an informative icon in the list
if [ "$(file -b --mime-type "${DIRECTORY}/${file}")" == 'text/x-shellscript' ];then
#if updatable_apps file in question is a shell script, then display shellscript icon.
mimeicon="${DIRECTORY}/icons/shellscript.png"
mimetype='script'
elif [[ "${DIRECTORY}/${file}" == *.png ]] || [[ "${DIRECTORY}/${file}" == *.svg ]];then
mimeicon="${DIRECTORY}/icons/image.png"
mimetype='image'
else
#otherwise display txt icon.
mimeicon="${DIRECTORY}/icons/txt.png"
mimetype='file'
fi
echo "TRUE
${mimeicon}
$file ($mimetype)
file:$file"
done
}
list_updates_gui() { #input: updatable_apps and updatable_files variables
LIST="$(generate_yad_list)"
#echo "List: ${LIST}EOL"
if [ -z "$LIST" ];then
status_green "mNothing to update. Nothing to do!"
exit 0
fi
#Display a list of everything updatable
output="$(echo -e "$LIST" | yad --center --title='Pi-Apps' \
--window-icon="${DIRECTORY}/icons/logo.png" --width=310 --height=300 \
--list --checklist --separator='\n' --print-column=4 --no-headers \
--text="Updates available:"$'\n'"Uncheck an item to skip updating it." \
--column=:CHK --column=:IMG --column=Name --column=ID:HD \
--button='Later'!"${DIRECTORY}/icons/exit.png"!"Remind me later":1 \
--button='Update now'!"${DIRECTORY}/icons/download.png":0)" || exit 0
#remove empty newlines from output
output="$(echo "$output" | grep .)"
#regenerate list of updatable apps and files, based on what the user selected
updatable_apps="$(echo "$output" | grep '^app:' | sed 's/^app://g')"
updatable_files="$(echo "$output" | grep '^file:' | sed 's/^file://g')"
}
update_now_gui() { #input: updatable_files and updatable_apps variables
local IFS=$'\n'
for file in $updatable_files ;do
mkdir -p "$(dirname "${DIRECTORY}/${file}")"
#copy new version
cp -f "${DIRECTORY}/update/pi-apps/${file}" "${DIRECTORY}/${file}" || echo -e "\e[91mFailed to copy ${DIRECTORY}/update/pi-apps/${file}! \e[0m"
#special edge-case: if current file ends in .c, delete the corresponsing executable so that it will be recompiled.
if [[ "$file" == *.c ]];then
rm -f "${DIRECTORY}/$(echo "$file" | sed 's/\.c$//g')"
fi
echo
status_green "${file} file was copied successfully."
done
IFS="$PREIFS"
if [ ! -z "$updatable_apps" ];then
"${DIRECTORY}/etc/terminal-run" '
DIRECTORY="'"$DIRECTORY"'"
updatable_apps="'"$updatable_apps"'"
trap "sleep 10" EXIT
PREIFS="$IFS"
IFS=$'\''\n'\''
for i in $updatable_apps
do
"${DIRECTORY}/manage" update "$i" nofetch
done
IFS="$PREIFS"
echo -e "\e[92mAll updates complete. Closing in 10 seconds.\e[39m"
"${DIRECTORY}/api" refresh_app_list
' "Updating $(echo "$updatable_apps" | wc -l) app$([ $(echo "$updatable_apps" | wc -l) != 1 ] && echo s)..."
fi
#delete .git folder, then copy the new one
rm -rf "${DIRECTORY}/.git" || sudo rm -rf "${DIRECTORY}/.git" || error "Failed to delete old ${DIRECTORY}/.git folder!"
cp -a "${DIRECTORY}/update/pi-apps/.git" "${DIRECTORY}" || error "Failed to copy new .git folder!"
status_green "\nPi-Apps updates complete.\n"
}
update_now_cli() { #input: updatable_files and updatable_apps variables
local IFS=$'\n'
for file in $updatable_files ;do
mkdir -p "$(dirname "${DIRECTORY}/${file}")"
#copy new version
cp -f "${DIRECTORY}/update/pi-apps/${file}" "${DIRECTORY}/${file}" || echo -e "\e[91mFailed to copy ${DIRECTORY}/update/pi-apps/${file}! \e[0m"
#special edge-case: if current file ends in .c, delete the corresponsing executable so that it will be recompiled.
if [[ "$file" == *.c ]];then
rm -f "${DIRECTORY}/$(echo "$file" | sed 's/\.c$//g')"
fi
status_green "${file} file was copied successfully."
done
for app in $updatable_apps ;do
"${DIRECTORY}/manage" update "$app" nofetch
done
#delete .git folder, then copy the new one
rm -rf "${DIRECTORY}/.git" || sudo rm -rf "${DIRECTORY}/.git" || error "Failed to delete old ${DIRECTORY}/.git folder!"
cp -a "${DIRECTORY}/update/pi-apps/.git" "${DIRECTORY}" || error "Failed to copy new .git folder!"
status_green "\nPi-Apps updates complete."
}
screen_width="$(xrandr | grep -oP '(?<=primary )\d+')"
screen_height="$(xrandr | grep -oP '(?<=primary )[x\d]+' | cut -d x -f 2)"
runmode="$1"
speed="$2"
if [ ! -z "$speed" ] && [ "$speed" != 'fast' ];then
error "Unknown value for speed: "\""$speed"\"". Allowed value: fast"
fi
if [ "$runmode" == onboot ];then
runmode=autostarted
elif [ -z "$runmode" ];then
runmode=gui
fi
#runmode values: autostarted, get-status, set-status, gui, gui-yes, cli, cli-yes
status "\nUpdater mode: $runmode\n"
if [ "$runmode" == autostarted ];then #if update-interval allows, and one app installed, display notification on boot
#check if update interval allows update-checks, otherwise exit
check_update_interval
if [ $? != 0 ];then
status "Won't check for updates today, because of the update interval is set to '$(cat "${DIRECTORY}/data/settings/Check for updates")' in Settings."
exit 0
fi
#check that at least one app has been installed by the user
if [ "$(ls "${DIRECTORY}/data/status" | wc -l)" == 0 ];then
status "No apps have been installed yet, so exiting now."
exit 0
fi
updatable_apps="$(get_updatable_apps)"
updatable_files="$(get_updatable_files)"
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "Nothing is updatable."
exit 0
fi
status "Displaying notification in lower-right of screen..."
{ #display notification in lower-right
output="$(yad --form --text='Pi-Apps updates available.' --separator='\n' \
--on-top --skip-taskbar --undecorated --close-on-unfocus \
--geometry=260+$((screen_width-262))+$((screen_height-150)) \
--image="${DIRECTORY}/icons/logo-64.png" \
--field='Never show again':CHK FALSE \
--button="Details!${DIRECTORY}/icons/info.png":0 --button="Close!${DIRECTORY}/icons/exit.png":2)"
button=$?
#if Details not clicked, and checkbox clicked, launch a dialog to change the update interval
if [ $button != 0 ];then
if [ "$(echo "$output" | grep . )" == TRUE ];then
#User checked the 'Never show again' box, so ask to change update interval
curval="$(cat "${DIRECTORY}/data/settings/Check for updates")"
[ -z "$curval" ] && curval="$(cat "${DIRECTORY}/etc/setting-params/Check for updates" | grep -v '#' | head -n1)"
params="$(cat "${DIRECTORY}/etc/setting-params/Check for updates" | grep -v '#')"
params="$(echo "$params" | grep -x "$curval" | tr '\n' '!')!$(echo "$params" | grep -vx "$curval" | tr '\n' '!')"
params="$(echo -e "$params" | sed 's/!!/!/g' | sed 's/!$//g' | sed 's/^!//g')"
echo "Params: '$params'"
output="$(yad --center --title='Change Pi-Apps update interval' --width=440 \
--form --separator='\n' --window-icon="${DIRECTORY}/icons/logo.png" \
--text="You just requested for Pi-Apps to <i>never check for updates</i> on boot."$'\n'"Are you sure? If so, change the update interval to "\""<b>Never</b>"\"" below." \
--field='Update interval: ':CB "$params" \
--button=Cancel!"${DIRECTORY}/icons/exit.png":1 \
--button=Save!"${DIRECTORY}/icons/check.png":0)"
button=$?
output="$(echo "$output" | grep .)"
if [ $button == 0 ];then #save button clicked
echo "$output" > "${DIRECTORY}/data/settings/Check for updates"
fi
fi
#since Details was not clicked, exit now
exit 0
fi
}
list_updates_gui
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "User did not allow anything to be updated."
exit 0
fi
update_now_gui
#display notification saying that pi-apps updates are complete
yad --form --text='Pi-Apps updates complete.' \
--on-top --skip-taskbar --undecorated --close-on-unfocus \
--geometry=260+$((screen_width-262))+$((screen_height-150)) \
--image="${DIRECTORY}/icons/logo-64.png" \
--button="Close!${DIRECTORY}/icons/exit.png":1
elif [ "$runmode" == 'get-status' ];then #Check if anything was deemed updatable the last time updates were checked for.
if [ -s "${DIRECTORY}/data/update-status/updatable-files" ] || [ -s "${DIRECTORY}/data/update-status/updatable-apps" ];then
exit 0
else
exit 1
fi
elif [ "$runmode" == 'set-status' ];then #check for updates and write updatable apps/files to "${DIRECTORY}/data/update-status"
updatable_apps="$(get_updatable_apps)"
updatable_files="$(get_updatable_files)"
echo "$updatable_apps" | grep . > "${DIRECTORY}/data/update-status/updatable-apps"
echo "$updatable_files" | grep . > "${DIRECTORY}/data/update-status/updatable-files"
"$0" get-status &>/dev/null
exit $?
elif [ "$runmode" == gui ];then #dialog-list of updatable apps, with checkboxes and an Update button
updatable_apps="$(get_updatable_apps)"
updatable_files="$(get_updatable_files)"
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "Nothing is updatable."
exit 0
fi
echo
list_updates_gui
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "User did not allow anything to be updated."
echo
exit 0
fi
update_now_gui
"$0" set-status
elif [ "$runmode" == gui-yes ];then #update now without asking for confirmation
updatable_apps="$(get_updatable_apps)"
updatable_files="$(get_updatable_files)"
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "Nothing is updatable."
exit 0
fi
update_now_gui
elif [ "$runmode" == cli ];then #return list of updatable apps, and ask the user permission to update
updatable_apps="$(get_updatable_apps)"
updatable_files="$(get_updatable_files)"
echo
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "Nothing is updatable."
exit 0
fi
if [ -z "$updatable_apps" ];then
status "No apps are updatable."
echo
else
status "These apps can be updated:"
echo
echo "$updatable_apps" | sed 's/^/ - /g'
echo
fi
if [ -z "$updatable_files" ];then
status "No files are updatable"
echo
else
status "These files can be updated:"
echo
echo "$updatable_files" | sed 's/^/ - /g'
echo
fi
read -p "$(status -n 'Update now? [Y/n] ')" answer
if [ "$answer" == n ] || [ "$answer" == N ];then
echo
echo "Exiting now."
echo
exit 0
fi
echo
update_now_cli
elif [ "$runmode" == cli-yes ];then #update now without asking for confirmation
updatable_apps="$(get_updatable_apps)"
updatable_files="$(get_updatable_files)"
echo
if [ -z "$updatable_files" ] && [ -z "$updatable_apps" ];then
status "Nothing is updatable."
exit 0
fi
if [ -z "$updatable_apps" ];then
status "No apps can be updated."
echo
else
status "These apps can be updated:"
echo
echo "$updatable_apps" | sed 's/^/ - /g'
echo
fi
if [ -z "$updatable_files" ];then
status "No files can be updated."
echo
else
status "These files can be updated:"
echo
echo "$updatable_files" | sed 's/^/ - /g'
echo
fi
echo
update_now_cli
else
error "updater: unknown run mode. Exiting now.\n"
fi
echo
exit 0
} #prevents errors if script was modified while in use