#!/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 .deb> <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.deb"
echo "$2  $d/update.deb" | sha256sum -c --quiet - >&2
{ if command -v apt-get >/dev/null; then apt-get install -y --allow-downgrades "$d/update.deb"; else dpkg -i "$d/update.deb"; fi; } >&2
