#!/usr/bin/env bash logfile="$(mktemp --suffix=".log" --tmpdir ombi-update.XXX)" exec >> "$logfile" exec 2>&1 printf -- '---- %b ----\n%b\n' "$(date)" "$0 $*" re='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' if [[ "$1" =~ $re ]]; then ombiurl="$1" shift else printf 'Invalid URL, aborting: "%s"\n' "$1" >&2 exit 1 fi newversion="$(cut -f8 -d/ <<< "$ombiurl")" if [[ "$newversion" =~ ^v[0-9.]+$ ]]; then targetfile="$(dirname "$(mktemp --dry-run --tmpdir)")/Ombi.$newversion.tar.gz" touch "$targetfile" else targetfile="$(mktemp --suffix=".tar.gz" --tmpdir Ombi.XXX)" fi tempopts=$(getopt -o 'P:n:h' --long 'applicationPath:,applicationpath:,processName:,processname:,help' -n 'ombi-update' -- "$@") || { printf 'Option parsing failed, aborting\n' >&2 && exit 1; } eval set -- "$tempopts" unset tempopts while true; do case "$1" in '-h'|'--help') printf -- 'Usage: %s URL --applicationPath PATH [--processname NAME]\n' "$(basename "$0")" exit 0 ;; '-P'|'--applicationPath'|'--applicationpath') ombipath="$2" shift 2 continue ;; '-n'|'--processname'|'--processName') ombiprocess="$2" shift 2 continue ;; '--') shift break ;; *) printf 'Internal error!' >&2 exit 1 ;; esac done # Any additional arguments are silently discarded at this point if [ -z "$ombipath" ]; then printf '--applicationPath is required, but not specified. Aborting.\n' >&2 exit 1 elif [ ! -d "$ombipath" ]; then printf '"%s" does not appear to be a valid path, aborting.\n' "$ombipath" >&2 exit 1 fi ombiservice="$(ps -o unit= -C "$ombiprocess")" ombiservice="${ombiservice:-ombi.service}" if systemctl is-enabled --quiet "$ombiservice" 2> /dev/null; then printf 'Downloading update to "%b"... ' "$targetfile" curl -fsSLo "$targetfile" --silent "$ombiurl" || { printf 'failed, aborting.\n' >&2 && exit 1; } printf 'done!\n' cd "$ombipath" printf 'Extracting update... ' tar xvvzf "$targetfile" || { printf 'failed, aborting.\n' >&2 && exit 1; } printf 'done!\nRestarting...\n' sudo systemctl restart "$ombiservice" else printf 'Unable to find service with name "%s", aborting.\n' "$ombiservice" >&2 fi