#!/bin/sh
# Update loamly through the package manager. Run as root, normally by the app
# through pkexec so the desktop asks for the password its own way.
#
# It exists as a file rather than as a command line inside the app because
# pkexec runs a *program*: an app that asked for a root shell with arguments it
# composed would be a much larger thing to trust than one that asks for this.
#
# --only-upgrade: this never installs loamly on a machine that does not have
# it, and never pulls the daemon onto a client that deliberately has none.
set -e
export DEBIAN_FRONTEND=noninteractive
apt-get update
installed=""
for pkg in loamly loamlyd; do
    if dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -q "^install ok installed$"; then
        installed="$installed $pkg"
    fi
done
[ -n "$installed" ] || { echo "neither loamly nor loamlyd is installed by dpkg"; exit 1; }
# shellcheck disable=SC2086
apt-get install -y --only-upgrade $installed
echo "done:$installed"
