Skip to content

Instantly share code, notes, and snippets.

@benburrill
Last active May 17, 2020 01:00
Show Gist options
  • Save benburrill/ec8039df9c7921a4beb8907f13c4d734 to your computer and use it in GitHub Desktop.
Save benburrill/ec8039df9c7921a4beb8907f13c4d734 to your computer and use it in GitHub Desktop.
OpenXcom nightly auto-update script
#!/usr/bin/env bash
# Runs and auto-updates OpenXcom nightlies in a portable environment
# By default, this script will try to download and use the latest
# nightly. If it finds a newer version than you already have, it will
# prompt you (with a zenity popup) to ask if you actually want to
# download it.
#
# If you want to stick to a particular version and don't want the popup
# telling you to update, you can change the $here/versions/greatest
# symlink to point to a specific AppImage rather than the default
# $here/versions/latest (which refers to the latest known version)
#
# If you want to launch a specific version in the portable environment,
# you can also pass that AppImage as an argument to this script.
#
# Another way to skip the update prompt is to define the environment
# variable OPENXCOM_UPDATE_LATEST. If defined, OpenXcom will be updated
# without prompting you first, except if the variable is set to "no", in
# which case OpenXcom will NOT be updated (which is similar to passing
# $here/versions/latest as an argument to the script)
#
# Old versions stick around in the versions directory, and you may want
# to delete them periodically to save disk space (though the AppImages
# currently only take up <10MiB). However, you may want the ability to
# revert to old versions if you encounter bugs in the nightlies, so old
# AppImages are never automatically deleted.
#
# Configuration, Data, and User directories (described in the OpenXcom
# README) for AppImages run with this script are found at $here/config,
# $here/data, and $here/user respectively.
# ~requires zenity
here="$(dirname "$(realpath "$0")")"
function nightlies {
# Outputs nightly AppImages available for download, sorted by
# modified date, most recent first.
curl -fL 'https://openxcom.org/git-builds' |
grep -o "https://[^\"]*/OpenXcom_\w*_$(arch | tr _ -).AppImage"
}
function confirm_download {
# Ask if it's ok to download OpenXcom
if [ -L "$here/versions/latest" ]; then
local title="Update OpenXcom?"
local info="A new OpenXcom nightly is available!\n$1"
else
# The latest symlink doesn't exist yet, so this is the first
# installation, not an "update".
local title="Download OpenXcom?"
local info="The latest OpenXcom nightly is\n$1"
fi
local body="$info\nDo you want to download it?"
zenity --question --title="$title" --text="$body"
}
function save_to {
# Download $1, writing it to the path $2
curl --create-dirs -f -o "$2" "$1" |
zenity --progress --no-cancel --pulsate --auto-close \
--text="Downloading..."
return "${PIPESTATUS[0]}"
}
function update_latest {
# Update the versions/latest symlink to the latest available
# version, downloading it if necessary.
# The return value of update_latest is not meaningful -- success
# does not imply that the versions/latest symlink actually exists.
if [ "$OPENXCOM_UPDATE_LATEST" = "no" ]; then
return
fi
echo "Checking for updates..." >&2
local latest_url="$(nightlies | head -n 1)"
if [ -z "$latest_url" ]; then
# Probably just a problem with the user's internet connection,
# but could also indicate a bug in the nightlies function.
zenity --notification --window-icon=error \
--text="Failed to check for OpenXcom updates"
return
fi
local latest_name="$(basename "$latest_url")"
local latest_path="$here/versions/$latest_name"
if [ ! -x "$latest_path" ]; then
if [ -z "$OPENXCOM_UPDATE_LATEST" ]; then
confirm_download "$latest_name" || return
fi
echo "Downloading $latest_url..." >&2
if save_to "$latest_url" "$latest_path"; then
chmod +x "$latest_path"
else
rm -- "$latest_path" &> /dev/null
zenity --notification --window-icon=error \
--text="Failed to download $latest_name"
fi
fi
if [ -x "$latest_path" ]; then
# If the download succeeds or we didn't need to download because
# the file already exists, update latest symlink to point to it.
ln -fs "$latest_name" "$here/versions/latest"
fi
}
function exec_app {
mkdir -p "$here/user" "$here/data" "$here/config"
DESKTOPINTEGRATION=no exec -- "$(realpath "$1")" \
-user "$here/user" \
-data "$here/data" \
-config "$here/config"
}
function prepend {
while read -r line; do
echo "$1$line"
done
}
if [ -n "$1" ]; then
exec_app "$1"
else
mkdir -p "$here/versions"
greatest="$here/versions/greatest"
if [ ! -L "$greatest" ]; then
ln -s "latest" "$greatest"
fi
if [ "$(basename "$(readlink "$greatest")")" = "latest" ]; then
update_latest
fi
if [ ! -x "$greatest" ]; then
zenity --error --no-wrap \
--text="The <tt>versions/greatest</tt> symlink\nis broken."
fi
exec_app "$greatest"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment