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