#!/bin/sh
# Bring this machine up to date: the operating system and the phone system,
# both through the package manager, both from the repositories the first
# boot wrote down. Nothing is fetched by hand and nothing is trusted that
# the package manager would not trust: the Alpine repository is signed with
# the key the package installed into /etc/apk/keys, the Debian one with the
# key in /usr/share/keyrings.
set -eu

if [ "$(id -u)" != 0 ]; then
	echo "run this as root" >&2
	exit 1
fi

before=$(nuxpbx version 2>/dev/null || echo "?")

if command -v apk >/dev/null 2>&1; then
	apk update -q
	apk upgrade -q
	rc-service nuxpbx restart >/dev/null
elif command -v apt-get >/dev/null 2>&1; then
	apt-get update -q
	DEBIAN_FRONTEND=noninteractive apt-get upgrade -y -q
	systemctl restart nuxpbx
else
	echo "neither apk nor apt: fetch the release yourself" >&2
	exit 1
fi

after=$(nuxpbx version 2>/dev/null || echo "?")
if [ "$before" = "$after" ]; then
	echo "done. nuxpbx $after was already the newest."
else
	echo "done. nuxpbx $before is now $after."
fi
