Minor change to AppImage download script
[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-11"
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 \n\n"
78 }
79
80 show_version()
81 {
82         printf "Version: %s\n" "$version"
83 }
84
85 spinner()
86 {
87         message="$1"
88         character_count=$((${#message} + 4))
89         pid=$!
90         delay=0.75
91         spinstr='\|/-'
92
93         while kill -0 "$pid" 2> /dev/null
94         do
95                 temp=${spinstr#?}
96                 printf "$message [%c]" "$spinstr"
97                 spinstr=$temp${spinstr%"$temp"}
98                 sleep "$delay"
99                 printf "%$character_count""s" | tr " " "\b"
100         done
101 }
102
103 architecture=$(arch)
104
105 extract=0
106 minimal=""
107 desktop=0
108 backups_option=0
109 revert=""
110 revert_option=0
111
112 while :
113 do
114         case $1 in
115                 -h | -\? | --help)
116                         show_help
117
118                         exit 0
119                         ;;
120                 -v | --version)
121                         show_version
122
123                         exit 0
124                         ;;
125                 -d | --desktop)
126                         desktop=1
127                         ;;
128                 -b | --backups)
129                         backups_option=1
130                         if [ -n "$2" ]
131                         then
132                                 backups=$2
133                                 shift
134                         else
135                                 printf '"--backups" requires a non-empty option argument.\n' >&2
136
137                                 exit 1
138                         fi
139                         ;;
140                 -r | --revert)
141                         revert_option=1
142                         if [ -n "$2" ]
143                         then
144                                 revert=$2
145                                 shift
146                         else
147                                 printf '"--revert" requires a non-empty option argument.\n' >&2
148
149                                 exit 1
150                         fi
151                         ;;
152                 -e | --extract)
153                         extract=1
154                         ;;
155                 -m | --minimal)
156                         minimal="-minimal"
157                         ;;
158                 --) # End of all options.
159                         shift
160                         break
161                         ;;
162                 ?*)
163                         printf 'Unknown option %s\n' "$1" >&2
164
165                         exit 1
166                         ;;
167                 *)
168                         break
169                         ;;
170         esac
171
172         shift
173 done
174
175 if [ ! -d "$HOME/bin" ]
176 then
177         printf "\$HOME/bin does not exist.
178 It is required for this script to run.
179 Also, \$HOME/bin should be in \$PATH.\n"
180
181         exit 1
182 fi
183
184 if [ "$backups_option" -eq 1 ] && {
185         [ "$minimal" = "-minimal" ] || [ "$extract" -eq 1 ] || [ "$revert_option" -eq 1 ]
186 }
187 then
188         printf "backups must be the only option\n"
189
190         exit 1
191 fi
192
193 if [ "$desktop" -eq 1 ] && {
194         [ "$minimal" = "-minimal" ] || [ "$extract" -eq 1 ]
195 }
196 then
197         printf "desktop must be the only option\n"
198
199         exit 1
200 fi
201
202 if [ "$backups_option" -eq 1 ]
203 then
204         if ! [ "$backups" -gt 0 ] 2> /dev/null
205         then
206                 printf "%s is not an integer\n" "$backups"
207
208                 exit 1
209         else
210                 tmp_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
211                 cp "$0" "$tmp_file"
212                 sed --in-place "s/^backups=.*/backups=$backups/" "$tmp_file"
213                 chmod +x "$tmp_file"
214                 mv "$tmp_file" "$0"
215
216                 exit 0
217         fi
218 fi
219
220 if [ "$desktop" -eq 1 ]
221 then
222         if [ -f "$HOME/Desktop/org.geeqie.Geeqie.desktop" ]
223         then
224                 printf "Desktop file already exists\n"
225
226                 exit 0
227         fi
228
229         file_count=$(find "$HOME/bin/" -name "Geeqie*latest*\.AppImage" -print | wc -l)
230         if [ "$file_count" -eq 0 ]
231         then
232                 printf "No AppImages have been downloaded\n"
233
234                 exit 1
235         fi
236
237         tmp_dir=$(mktemp --directory "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
238         cd "$tmp_dir" || exit 1
239
240         app=$(find "$HOME/bin/" -name "Geeqie*latest*\.AppImage" -print | sort --reverse | head -1)
241         $app --appimage-extract "usr/local/share/applications/org.geeqie.Geeqie.desktop" > /dev/null
242         $app --appimage-extract "usr/local/share/pixmaps/geeqie.png" > /dev/null
243         xdg-desktop-icon install --novendor "squashfs-root/usr/local/share/applications/org.geeqie.Geeqie.desktop"
244         xdg-icon-resource install --novendor --size 48 "squashfs-root/usr/local/share/pixmaps/geeqie.png"
245         xdg-desktop-menu install --novendor "squashfs-root/usr/local/share/applications/org.geeqie.Geeqie.desktop"
246         rm --recursive --force "$tmp_dir"
247
248         exit 0
249 fi
250
251 # The cd needs to be here because the --backups option needs PWD
252 cd "$HOME/bin/" || exit 1
253
254 if [ "$revert_option" -eq 1 ]
255 then
256         if ! [ "$revert" -ge 0 ] 2> /dev/null
257         then
258                 printf "%s is not an integer\n" "$revert"
259
260                 exit 1
261         else
262                 if [ "$revert" -eq 0 ]
263                 then
264                         revert=""
265                 else
266                         revert=".$revert"
267                 fi
268         fi
269
270         if ! [ -f "$HOME/bin/Geeqie$minimal-latest-$architecture.AppImage$revert" ]
271         then
272                 printf "Backup $HOME/bin/Geeqie%s-latest-$architecture.AppImage%s does not exist\n" "$minimal" "$revert"
273
274                 exit 1
275         fi
276
277         if [ "$extract" -eq 1 ]
278         then
279                 rm --recursive --force "Geeqie$minimal-latest-$architecture-AppImage"
280                 mkdir "Geeqie$minimal-latest-$architecture-AppImage"
281                 cd "Geeqie$minimal-latest-$architecture-AppImage" || exit 1
282
283                 (../Geeqie-latest-x86_64.AppImage --appimage-extract > /dev/null) & spinner "Extracting Geeqie AppImage..."
284
285                 printf "\nExtraction complete\n"
286
287                 cd ..
288                 ln --symbolic --force "./Geeqie$minimal-latest-$architecture-AppImage/squashfs-root/AppRun" geeqie
289         else
290                 ln --symbolic --force "$HOME/bin/Geeqie$minimal-latest-$architecture.AppImage$revert" geeqie
291         fi
292
293         exit 0
294 fi
295
296 log_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
297
298 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"
299
300 download_size=$(stat --printf "%s" "$log_file")
301 rm "$log_file"
302
303 # If a new file was downloaded, check if extraction is required
304 if [ "$download_size" -gt 0 ]
305 then
306         chmod +x "Geeqie$minimal-latest-$architecture.AppImage"
307
308         if [ "$extract" -eq 1 ]
309         then
310                 rm --recursive --force "Geeqie$minimal-latest-$architecture-AppImage"
311                 mkdir "Geeqie$minimal-latest-$architecture-AppImage"
312                 cd "Geeqie$minimal-latest-$architecture-AppImage" || exit 1
313
314                 (../Geeqie-latest-x86_64.AppImage --appimage-extract > /dev/null) & spinner "Extracting Geeqie AppImage..."
315
316                 printf "\nExtraction complete\n"
317
318                 cd ..
319                 ln --symbolic --force "./Geeqie$minimal-latest-$architecture-AppImage/squashfs-root/AppRun" geeqie
320         else
321                 ln --symbolic --force "Geeqie$minimal-latest-$architecture.AppImage" geeqie
322         fi
323 fi