Update AppImage download script for command line completions
[geeqie.git] / scripts / geeqie-download-appimage.sh
1 #!/bin/sh
2 #**********************************************************************
3 # Copyright (C) 2023 - The Geeqie Team
4 #
5 # Author: Colin Clark
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #**********************************************************************
21
22 ## @file
23 ## @brief Download full and minimal AppImages from the Continuous build release on GitHub.
24 ## Optionally extract the full size AppImage.
25 ##
26 ## Downloads are made to $HOME/bin.
27 ## It is expected that $HOME/bin exists and that it is in $PATH.
28 ##
29 ## Downloads will not be made unless the server version is newer than the local file.
30 ##
31
32 version="2024-04-14"
33 backups=3
34
35 show_help()
36 {
37         printf "Download the latest Geeqie AppImages from the
38 Continuous Build release on GitHub.
39
40 -b --backups <n> Set number of backups (default is 3)
41 -d --desktop Install desktop icon and menu item
42 -e --extract Extract AppImage
43 -h --help Display this message
44 -m --minimal Download minimal version of AppImage
45 -r --revert <n> Revert to downloaded AppImage backup. For the latest version set <n> to 0
46 -v --version Display version of this file
47
48 The Continuous Build release is updated each
49 time the source code is updated.
50
51 The default action is to download an AppImage to
52 \$HOME/bin. A symbolic link will be set so that
53 \"geeqie\" points to the executable
54
55 No downloads will be made unless the file on the
56 server at GitHub is newer than the local file.
57
58 The full size AppImage is about 120MB and the
59 minimal AppImage is about 10MB. Therefore the full
60 size version will load much slower and will have
61 a slightly slower run speed.
62
63 However the minimal version has limited capabilities
64 compared to the full size version.
65
66 The minimal option (-m or --minimal) will download
67 the minimal version.
68
69 The extract option (-e or --extract) will extract
70 The contents of the AppImage into a sub-directory
71 of \$HOME/bin, and then set the symbolic link to the
72 extracted executable.
73
74 This will take up some disk space, but the
75 extracted executable will run as fast as a
76 packaged release.
77
78 If the extract option is selected, a symbolic link from
79 \$HOME/.local/share/bash-completion/completions/geeqie
80 to the extracted executable will be set to enable command line completion.
81 \n\n"
82 }
83
84 show_version()
85 {
86         printf "Version: %s\n" "$version"
87 }
88
89 spinner()
90 {
91         message="$1"
92         character_count=$((${#message} + 4))
93         pid=$!
94         delay=0.75
95         spinstr='\|/-'
96
97         while kill -0 "$pid" 2> /dev/null
98         do
99                 temp=${spinstr#?}
100                 printf "$message [%c]" "$spinstr"
101                 spinstr=$temp${spinstr%"$temp"}
102                 sleep "$delay"
103                 printf "%$character_count""s" | tr " " "\b"
104         done
105 }
106
107 architecture=$(arch)
108
109 extract=0
110 minimal=""
111 desktop=0
112 backups_option=0
113 revert=""
114 revert_option=0
115
116 while :
117 do
118         case $1 in
119                 -h | -\? | --help)
120                         show_help
121
122                         exit 0
123                         ;;
124                 -v | --version)
125                         show_version
126
127                         exit 0
128                         ;;
129                 -d | --desktop)
130                         desktop=1
131                         ;;
132                 -b | --backups)
133                         backups_option=1
134                         if [ -n "$2" ]
135                         then
136                                 backups=$2
137                                 shift
138                         else
139                                 printf '"--backups" requires a non-empty option argument.\n' >&2
140
141                                 exit 1
142                         fi
143                         ;;
144                 -r | --revert)
145                         revert_option=1
146                         if [ -n "$2" ]
147                         then
148                                 revert=$2
149                                 shift
150                         else
151                                 printf '"--revert" requires a non-empty option argument.\n' >&2
152
153                                 exit 1
154                         fi
155                         ;;
156                 -e | --extract)
157                         extract=1
158                         ;;
159                 -m | --minimal)
160                         minimal="-minimal"
161                         ;;
162                 --) # End of all options.
163                         shift
164                         break
165                         ;;
166                 ?*)
167                         printf 'Unknown option %s\n' "$1" >&2
168
169                         exit 1
170                         ;;
171                 *)
172                         break
173                         ;;
174         esac
175
176         shift
177 done
178
179 if [ ! -d "$HOME/bin" ]
180 then
181         printf "\$HOME/bin does not exist.
182 It is required for this script to run.
183 Also, \$HOME/bin should be in \$PATH.\n"
184
185         exit 1
186 fi
187
188 if [ "$backups_option" -eq 1 ] && {
189         [ "$minimal" = "-minimal" ] || [ "$extract" -eq 1 ] || [ "$revert_option" -eq 1 ]
190 }
191 then
192         printf "backups must be the only option\n"
193
194         exit 1
195 fi
196
197 if [ "$desktop" -eq 1 ] && {
198         [ "$minimal" = "-minimal" ] || [ "$extract" -eq 1 ]
199 }
200 then
201         printf "desktop must be the only option\n"
202
203         exit 1
204 fi
205
206 if [ "$backups_option" -eq 1 ]
207 then
208         if ! [ "$backups" -gt 0 ] 2> /dev/null
209         then
210                 printf "%s is not an integer\n" "$backups"
211
212                 exit 1
213         else
214                 tmp_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
215                 cp "$0" "$tmp_file"
216                 sed --in-place "s/^backups=.*/backups=$backups/" "$tmp_file"
217                 chmod +x "$tmp_file"
218                 mv "$tmp_file" "$0"
219
220                 exit 0
221         fi
222 fi
223
224 if [ "$desktop" -eq 1 ]
225 then
226         if [ -f "$HOME/Desktop/org.geeqie.Geeqie.desktop" ]
227         then
228                 printf "Desktop file already exists\n"
229
230                 exit 0
231         fi
232
233         file_count=$(find "$HOME/bin/" -name "Geeqie*latest*\.AppImage" -print | wc -l)
234         if [ "$file_count" -eq 0 ]
235         then
236                 printf "No AppImages have been downloaded\n"
237
238                 exit 1
239         fi
240
241         tmp_dir=$(mktemp --directory "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
242         cd "$tmp_dir" || exit 1
243
244         app=$(find "$HOME/bin/" -name "Geeqie*latest*\.AppImage" -print | sort --reverse | head -1)
245         $app --appimage-extract "usr/local/share/applications/org.geeqie.Geeqie.desktop" > /dev/null
246         $app --appimage-extract "usr/local/share/pixmaps/geeqie.png" > /dev/null
247         xdg-desktop-icon install --novendor "squashfs-root/usr/local/share/applications/org.geeqie.Geeqie.desktop"
248         xdg-icon-resource install --novendor --size 48 "squashfs-root/usr/local/share/pixmaps/geeqie.png"
249         xdg-desktop-menu install --novendor "squashfs-root/usr/local/share/applications/org.geeqie.Geeqie.desktop"
250         rm --recursive --force "$tmp_dir"
251
252         exit 0
253 fi
254
255 # The cd needs to be here because the --backups option needs PWD
256 cd "$HOME/bin/" || exit 1
257
258 if [ "$revert_option" -eq 1 ]
259 then
260         if ! [ "$revert" -ge 0 ] 2> /dev/null
261         then
262                 printf "%s is not an integer\n" "$revert"
263
264                 exit 1
265         else
266                 if [ "$revert" -eq 0 ]
267                 then
268                         revert=""
269                 else
270                         revert=".$revert"
271                 fi
272         fi
273
274         if ! [ -f "$HOME/bin/Geeqie$minimal-latest-$architecture.AppImage$revert" ]
275         then
276                 printf "Backup $HOME/bin/Geeqie%s-latest-$architecture.AppImage%s does not exist\n" "$minimal" "$revert"
277
278                 exit 1
279         fi
280
281         if [ "$extract" -eq 1 ]
282         then
283                 rm --recursive --force "Geeqie$minimal-latest-$architecture-AppImage"
284                 mkdir "Geeqie$minimal-latest-$architecture-AppImage"
285                 cd "Geeqie$minimal-latest-$architecture-AppImage" || exit 1
286
287                 (../Geeqie-latest-x86_64.AppImage --appimage-extract > /dev/null) & spinner "Extracting Geeqie AppImage..."
288
289                 printf "\nExtraction complete\n"
290
291                 cd ..
292                 ln --symbolic --force "./Geeqie$minimal-latest-$architecture-AppImage/squashfs-root/AppRun" geeqie
293         else
294                 ln --symbolic --force "$HOME/bin/Geeqie$minimal-latest-$architecture.AppImage$revert" geeqie
295         fi
296
297         exit 0
298 fi
299
300 log_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
301
302 wget --no-verbose --show-progress --backups="$backups" --output-file="$log_file" --timestamping "https://github.com/BestImageViewer/geeqie/releases/download/continuous/Geeqie$minimal-latest-$architecture.AppImage"
303
304 download_size=$(stat --printf "%s" "$log_file")
305 rm "$log_file"
306
307 # If a new file was downloaded, check if extraction is required
308 if [ "$download_size" -gt 0 ]
309 then
310         chmod +x "Geeqie$minimal-latest-$architecture.AppImage"
311
312         if [ "$extract" -eq 1 ]
313         then
314                 rm --recursive --force "Geeqie$minimal-latest-$architecture-AppImage"
315                 mkdir "Geeqie$minimal-latest-$architecture-AppImage"
316                 cd "Geeqie$minimal-latest-$architecture-AppImage" || exit 1
317
318                 (../Geeqie-latest-x86_64.AppImage --appimage-extract > /dev/null) & spinner "Extracting Geeqie AppImage..."
319
320                 printf "\nExtraction complete\n"
321
322                 if [ ! -f "$HOME/.local/share/bash-completion/completions/geeqie" ]
323                 then
324                         mkdir --parents "$HOME/.local/share/bash-completion/completions/"
325                         ln --symbolic "$HOME/bin/Geeqie-latest-x86_64-AppImage/squashfs-root/usr/local/share/bash-completion/completions/geeqie" "$HOME/.local/share/bash-completion/completions/geeqie"
326                 fi
327
328                 cd ..
329                 ln --symbolic --force "./Geeqie$minimal-latest-$architecture-AppImage/squashfs-root/AppRun" geeqie
330         else
331                 ln --symbolic --force "Geeqie$minimal-latest-$architecture.AppImage" geeqie
332         fi
333 fi