#!/bin/sh
set -eu
case "${1:-}" in
 triggered) /opt/screen2screen-linux-host/packaging/repository-config.sh;exit 0;;
 configure|abort-upgrade|abort-remove|abort-deconfigure) ;;
 *) exit 0;;
esac
for path in /opt/screen2screen-linux-host /var/lib/screen2screen-linux-host /etc/screen2screen-linux-host;do
 [ ! -L "$path" ] || { echo 'Screen2Screen: unsafe installation path.' >&2;exit 1; }
done
if ! getent passwd screen2screen-host >/dev/null;then useradd --system --user-group --home-dir /var/lib/screen2screen-linux-host --shell /usr/sbin/nologin screen2screen-host;fi
[ "$(id -u screen2screen-host)" != 0 ] || exit 1
install -d -m 0700 -o screen2screen-host -g screen2screen-host /var/lib/screen2screen-linux-host
install -d -m 0755 -o root -g root /etc/screen2screen-linux-host
while read -r expected path;do
 if [ -f "$path" ] && [ ! -L "$path" ];then
  actual=$(sha256sum "$path");actual=${actual%% *}
  if grep -Fqx "$actual $path" /opt/screen2screen-linux-host/packaging/legacy-files.sha256;then rm -f -- "$path";
  else printf 'Screen2Screen: preserved custom legacy file %s; review after upgrade.\n' "$path" >&2;fi
 fi
done < /opt/screen2screen-linux-host/packaging/legacy-files.sha256
/opt/screen2screen-linux-host/packaging/repository-config.sh
/opt/screen2screen-linux-host/packaging/package-state.sh restore
if [ ! -f /var/lib/screen2screen-linux-host/settings.json ];then
 echo 'Installed. Authorize and join: sudo screen2screen-linux-host setup --joincode'
fi
echo 'Unpair online before removal/purge to release the organization device allowance.'
