Revised AppImage download script
[geeqie.git] / scripts / geeqie-download-appimage.sh
index 95e54e8..e4b2803 100755 (executable)
 ## @brief Download full and minimal AppImages from the Continuous build release on GitHub.
 ## Optionally extract the full size AppImage.
 ##
-## $1: -e | --extract Extract full size AppImage
-## The user should modify the symbolic links as appropriate.
+## The user may modify the symbolic links as appropriate.
 ##
 ## Downloads will not be made unless the server version is newer than the local file.
 ##
 
+version="2023-09-08"
+backups=3
+
 show_help()
 {
-printf "Download the latest Geeqie AppImages from the
+       printf "Download the latest Geeqie AppImages from the
 Continuous Build release on GitHub.
 
+-b --backups <n> Set number of backups (default is 3)
+-d --desktop Install desktop icon and menu item
+-e --extract Extract AppImage
+-h --help Display this message
+-m --minimal Download minimal version of AppImage
+-r --revert <n> Revert to downloaded AppImage backup
+-v --version Display version of this file
+
 The Continuous Build release is updated each
 time the source code is updated.
 
-You should read the contents of this file before
-running it and modify it to your requirements.
-
-The default action is to download both the
-minimal and full size AppImages to \$HOME/bin.
-Symbolic links will be set so that:
-\"geeqie\" points to the minimal version
-and
-\"Geeqie\" points to the full version.
+The default action is to download an AppImage to
+\$HOME/bin. A symbolic link will be set so that
+\"geeqie\" points to the executable
 
 No downloads will be made unless the file on the
 server at GitHub is newer than the local file.
@@ -58,49 +62,212 @@ a slightly slower run speed.
 However the minimal version has limited capabilities
 compared to the full size version.
 
+The minimal option (-m or --minimal) will download
+the minimal version.
+
 The extract option (-e or --extract) will extract
-The contents of the full size image into a sub-directory
+The contents of the AppImage into a sub-directory
 of \$HOME/bin, and then set the symbolic link to the
-extracted executable. This will take up disk space, but the
-extracted executable will run as fast as a packaged release.
+extracted executable.
 
-When a new file is downloaded the extracted files will
-be replaced by the newly downloaded files\n\n"
+This will take up some disk space, but the
+extracted executable will run as fast as a
+packaged release.
+\n\n"
 }
 
+show_version()
+{
+       printf "Version: %s\n" "$version"
+}
+
+architecture=$(arch)
+
 extract=0
+minimal=""
+desktop=0
+backups_option=0
+revert=0
+revert_option=0
+
+while :
+do
+       case $1 in
+               -h | -\? | --help)
+                       show_help
+
+                       exit 0
+                       ;;
+               -v | --version)
+                       show_version
+
+                       exit 0
+                       ;;
+               -d | --desktop)
+                       desktop=1
+                       ;;
+               -b | --backups)
+                       backups_option=1
+                       if [ -n "$2" ]
+                       then
+                               backups=$2
+                               shift
+                       else
+                               printf '"--backups" requires a non-empty option argument.\n' >&2
 
-while :; do
-    case $1 in
-        -h|-\?|--help)
-            show_help
-            exit
-            ;;
-        -e|--extract)    # AppImage extraction is required
-            extract=1
-            ;;
-        --)              # End of all options.
-            shift
-            break
-            ;;
-        -?*)
-            printf 'ERROR: Unknown option %s\n' "$1" >&2
-            exit
-            ;;
-        *)
-            break
-    esac
-
-    shift
+                               exit 1
+                       fi
+                       ;;
+               -r | --revert)
+                       revert_option=1
+                       if [ -n "$2" ]
+                       then
+                               revert=$2
+                               shift
+                       else
+                               printf '"--revert" requires a non-empty option argument.\n' >&2
+
+                               exit 1
+                       fi
+                       ;;
+               -e | --extract)
+                       extract=1
+                       ;;
+               -m | --minimal)
+                       minimal="-minimal"
+                       ;;
+               --) # End of all options.
+                       shift
+                       break
+                       ;;
+               ?*)
+                       printf 'Unknown option %s\n' "$1" >&2
+
+                       exit 1
+                       ;;
+               *)
+                       break
+                       ;;
+       esac
+
+       shift
 done
 
-cd "$HOME/bin/" || exit
-architecture=$(arch)
+if [ ! -d "$HOME/bin" ]
+then
+       printf "\$HOME/bin does not exist.
+It is required for this script to run.\n"
+
+       exit 1
+fi
+
+cd "$HOME/bin/" || exit 1
+
+if [ "$backups_option" -eq 1 ] && {
+       [ "$minimal" = "-minimal" ] || [ "$extract" -eq 1 ] || [ "$revert_option" -eq 1 ]
+}
+then
+       printf "backups must be the only option\n"
+
+       exit 1
+fi
+
+if [ "$desktop" -eq 1 ] && {
+       [ "$minimal" = "-minimal" ] || [ "$extract" -eq 1 ]
+}
+then
+       printf "desktop must be the only option\n"
+
+       exit 1
+fi
+
+if [ "$backups_option" -eq 1 ]
+then
+       if ! [ "$backups" -gt 0 ] 2> /dev/null
+       then
+               printf "%s is not an integer\n" "$backups"
+
+               exit 1
+       else
+               tmp_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
+               cp "$0" "$tmp_file"
+               sed --in-place "s/^backups=.*/backups=$backups/" "$tmp_file"
+               chmod +x "$tmp_file"
+               mv "$tmp_file" "$0"
+
+               exit 0
+       fi
+fi
+
+if [ "$desktop" -eq 1 ]
+then
+       if [ -f "$HOME/Desktop/geeqie.desktop" ]
+       then
+               printf "Desktop file already exists\n"
+
+               exit 0
+       fi
+
+       file_count=$(find "$HOME/bin/" -name "Geeqie*latest*\.AppImage" -print | wc -l)
+       if [ "$file_count" -eq 0 ]
+       then
+               printf "No AppImages have been downloaded\n"
+
+               exit 1
+       fi
+
+       tmp_dir=$(mktemp --directory "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
+       cd "$tmp_dir" || exit 1
+
+       app=$(find "$HOME/bin/" -name "Geeqie*latest*\.AppImage" -print | sort --reverse | head -1)
+       $app --appimage-extract "usr/local/share/applications/geeqie.desktop"
+       $app --appimage-extract "usr/local/share/pixmaps/geeqie.png"
+       xdg-desktop-icon install --novendor "squashfs-root/usr/local/share/applications/geeqie.desktop"
+       xdg-icon-resource install --novendor --size 48 "squashfs-root/usr/local/share/pixmaps/geeqie.png"
+       xdg-desktop-menu install --novendor "squashfs-root/usr/local/share/applications/geeqie.desktop"
+       rm --recursive --force "$tmp_dir"
+
+       exit 0
+fi
+
+if [ "$revert_option" -eq 1 ]
+then
+       if ! [ "$revert" -gt 0 ] 2> /dev/null
+       then
+               printf "%s is not an integer\n" "$revert"
+
+               exit 1
+       else
+               if ! [ -f "$HOME/bin/Geeqie$minimal-latest-$architecture.AppImage.$revert" ]
+               then
+                       printf "Backup $HOME/bin/Geeqie%s-latest-$architecture.AppImage.%s does not exist\n" "$minimal" "$revert"
+
+                       exit 1
+               fi
+
+               if [ "$extract" -eq 1 ]
+               then
+                       rm --recursive --force "Geeqie$minimal-latest-$architecture-AppImage"
+                       mkdir "Geeqie$minimal-latest-$architecture-AppImage"
+                       cd "Geeqie$minimal-latest-$architecture-AppImage" || exit 1
+
+                       printf "Extracting AppImage\n"
+                       ../"Geeqie$minimal-latest-$architecture.AppImage.$revert" --appimage-extract | cut --characters 1-50 | tr '\n' '\r'
+                       printf "\nExtraction complete\n"
+
+                       cd ..
+                       ln --symbolic --force "./Geeqie$minimal-latest-$architecture-AppImage/squashfs-root/AppRun" geeqie
+               else
+                       ln --symbolic --force "$HOME/bin/Geeqie$minimal-latest-$architecture.AppImage.$revert" geeqie
+               fi
+
+               exit 0
+       fi
+fi
 
-# Download full size AppImage
 log_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
 
-wget --no-verbose --show-progress --backups=3 --output-file="$log_file" --timestamping "https://github.com/BestImageViewer/geeqie/releases/download/continuous/Geeqie-latest-$architecture.AppImage";
+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"
 
 download_size=$(stat --printf "%s" "$log_file")
 rm "$log_file"
@@ -108,26 +275,21 @@ rm "$log_file"
 # If a new file was downloaded, check if extraction is required
 if [ "$download_size" -gt 0 ]
 then
-       chmod +x "Geeqie-latest-$architecture.AppImage"
+       chmod +x "Geeqie$minimal-latest-$architecture.AppImage"
 
        if [ "$extract" -eq 1 ]
        then
-               rm -rf "Geeqie-latest-$architecture-AppImage"
-               mkdir "Geeqie-latest-$architecture-AppImage"
-               cd "Geeqie-latest-$architecture-AppImage" || exit
+               rm --recursive --force "Geeqie$minimal-latest-$architecture-AppImage"
+               mkdir "Geeqie$minimal-latest-$architecture-AppImage"
+               cd "Geeqie$minimal-latest-$architecture-AppImage" || exit 1
 
                printf "Extracting AppImage\n"
-               ../"Geeqie-latest-$architecture.AppImage" --appimage-extract | cut -c 1-50 | tr '\n' '\r'
+               ../"Geeqie$minimal-latest-$architecture.AppImage" --appimage-extract | cut --characters 1-50 | tr '\n' '\r'
+               printf "\nExtraction complete\n"
 
-               printf "\n"
                cd ..
-               ln --symbolic --force "./Geeqie-latest-$architecture-AppImage/squashfs-root/AppRun" Geeqie
+               ln --symbolic --force "./Geeqie$minimal-latest-$architecture-AppImage/squashfs-root/AppRun" geeqie
        else
-               ln --symbolic --force "Geeqie-latest-$architecture.AppImage" Geeqie
+               ln --symbolic --force "Geeqie$minimal-latest-$architecture.AppImage" geeqie
        fi
 fi
-
-# Download minimal AppImage
-wget --quiet --show-progress --backups=3 --timestamping "https://github.com/BestImageViewer/geeqie/releases/download/continuous/Geeqie-minimal-latest-$architecture.AppImage";
-chmod +x "Geeqie-minimal-latest-$architecture.AppImage"
-ln --symbolic --force "Geeqie-minimal-latest-$architecture.AppImage" geeqie