#!/bin/sh
# AutoKeyClicker Free update, run as root through pkexec by the app's updater: a root-owned copy
# of the downloaded package, its SHA-256 checked again, then the system's own installer.
#   update-install <downloaded .rpm> <its sha256>   (apps before 1.0.1-beta.11: file already there)
#   update-install --wait <its sha256>                  (password first: prints "ready" once the
#     password is accepted, then reads the downloaded file's path from stdin; EOF installs nothing)
# Everything after "ready" goes to stderr - the app has stopped reading stdout by then.
set -e
[ $# -eq 2 ] || exit 2
case "$2" in *[!0-9a-f]*) exit 2 ;; esac
[ ${#2} -eq 64 ] || exit 2
if [ "$1" = --wait ]; then
    echo ready
    IFS= read -r f || exit 3
    [ -n "$f" ] || exit 3
else
    f=$1
fi
d=$(mktemp -d)
trap 'rm -rf "$d"' EXIT
cp -- "$f" "$d/update.rpm"
echo "$2  $d/update.rpm" | sha256sum -c --quiet - >&2
{ if rpm -U --quiet "$d/update.rpm" 2>/dev/null; then :; elif command -v dnf >/dev/null; then dnf install -y "$d/update.rpm"; elif command -v zypper >/dev/null; then zypper --non-interactive install --allow-unsigned-rpm "$d/update.rpm"; else rpm -U --replacepkgs "$d/update.rpm"; fi; } >&2
