-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappimage-updater.sh
executable file
·109 lines (88 loc) · 2.71 KB
/
appimage-updater.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
#!/bin/bash
# ==== SETUP ====
# change if necessary
TRACKED_DIRS=("/Applications" "$HOME/Applications")
AIU_NAME="appimageupdatetool-*.AppImage"
# internal constants
read -r -d '' USAGE_STRING << 'EOF'
Usage: %s [-vh]
-n send a notification after updates with the number of
updated applications (only if there's at least one)
-v verbose mode (show output from appimageupdatetool-*.AppImage)
-h displays this help and exits
EOF
NOTIFY=""
VERBOSE=""
# flags management
while getopts 'nvh' flag; do
case "$flag" in
n) NOTIFY="1" ;;
v) VERBOSE="1" ;;
h) printf "$USAGE_STRING\n" "$0"; exit 0 ;;
*) printf "$USAGE_STRING\n" "$0"; exit 1 ;;
esac
done
# ==== MAIN CODE ====
tmpdir=$(mktemp -d)
function aborted() {
rm -rf "$tmpdir"
echo -e "\e[31m# Aborted, updated $updated AppImages.\e[0m"
exit 1
}
trap aborted SIGINT
[ "$VERBOSE" ] && out="/dev/stdout" || out="/dev/null"
updated="0"
aiu_exec=""
# update logic
function handle_update() {
local app="$1"
local elevate=""
# work on a temp dir link to avoid permission issues and clobbering with temp files
ln -srt "$tmpdir" "$app"
pushd "$tmpdir" > /dev/null
# nb: -O doesn't actually "overwrite", but rather replaces the file (i.e. original file is unlinked)
"$aiu_exec" -O "$app" &> "$out"
local success="$?"
popd > /dev/null
if [ "$success" -eq 0 ]; then
[ ! -w . ] && elevate="pkexec" # in case we don't have write access to the original directory
$elevate mv -ft . "$tmpdir/$app"
echo -e "\e[32m# Successfully updated $app\e[0m"
((updated+=1))
else
echo -e "\e[31m# Something went wrong while updating $app (exit code $success)\e[0m"
fi
}
# lookup appimageupdatetool
for d in ${TRACKED_DIRS[*]}; do
if [ -x "$d"/$AIU_NAME ]; then
aiu_exec=$(echo "$d"/$AIU_NAME) # force glob expansion
break
fi
done
if [ ! "$aiu_exec" ]; then
echo -e "\e[31m# appimageupdatetool not found in ${TRACKED_DIRS[*]}, or missing x permission. Cannot check updates.\e[0m"
exit 1
fi
# iterate over appimages
for d in ${TRACKED_DIRS[*]}; do
cd "$d" &> "$out" || continue
shopt -s nullglob # prevents literal globs in case there are no matches
for i in *.AppImage *.appimage; do
echo -e "\e[34m# Checking updates for $i\e[0m"
"$aiu_exec" -j "$i" &> "$out"
updatable="$?"
[ "$VERBOSE" ] && echo # sometimes $aiu_exec doesn't print trailing \n
case "$updatable" in
0) echo -e "\e[1;33m# No updates available for $i\e[0m" ;;
1) handle_update "$i" ;;
*) echo -e "\e[31m# Cannot check updates for $i (exit code $updatable)\e[0m" ;;
esac
done
done
# final stuff
rm -rf "$tmpdir"
echo -e "\e[34m# Done, updated $updated AppImages.\e[0m"
if [ "$NOTIFY" ] && [ "$updated" -gt 0 ]; then
notify-send -i dialog-information-symbolic "Updated $updated AppImages"
fi