#!/bin/sh
# postinst script for headscale.

set -e

# Summary of how this script can be called:
#        * <postinst> 'configure' <most-recently-configured-version>
#        * <old-postinst> 'abort-upgrade' <new version>
#        * <conflictor's-postinst> 'abort-remove' 'in-favour' <package>
#          <new-version>
#        * <postinst> 'abort-remove'
#        * <deconfigured's-postinst> 'abort-deconfigure' 'in-favour'
#          <failed-install-package> <version> 'removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.

HEADSCALE_USER="headscale"
HEADSCALE_GROUP="headscale"
HEADSCALE_HOME_DIR="/var/lib/headscale"
HEADSCALE_SHELL="/usr/sbin/nologin"
HEADSCALE_SERVICE="headscale.service"

case "$1" in
    configure)
      groupadd --force --system "$HEADSCALE_GROUP"
      if ! id -u "$HEADSCALE_USER" >/dev/null 2>&1; then
        useradd --system --shell "$HEADSCALE_SHELL" \
          --gid "$HEADSCALE_GROUP" --home-dir "$HEADSCALE_HOME_DIR" \
          --comment "headscale default user" "$HEADSCALE_USER"
      fi

      # Enable service and keep track of its state
      if deb-systemd-helper --quiet was-enabled "$HEADSCALE_SERVICE"; then
        deb-systemd-helper enable "$HEADSCALE_SERVICE" >/dev/null || true
      else
        deb-systemd-helper update-state "$HEADSCALE_SERVICE" >/dev/null || true
      fi

      # Bounce service
      if [ -d /run/systemd/system ]; then
        systemctl --system daemon-reload >/dev/null || true
        # Check what the unit would load: its user, working directory and
        # HOME, and no HEADSCALE_* variables from this shell. A rejected
        # configuration is not restarted, so an upgraded headscale keeps the
        # previous version until something else restarts it.
        if (cd "$HEADSCALE_HOME_DIR" && setpriv --reuid="$HEADSCALE_USER" --regid="$HEADSCALE_GROUP" --init-groups env -i HOME="$HEADSCALE_HOME_DIR" /usr/bin/headscale check config) >/dev/null; then
          if [ -n "$2" ]; then
            deb-systemd-invoke restart "$HEADSCALE_SERVICE" >/dev/null || true
          else
            deb-systemd-invoke start "$HEADSCALE_SERVICE" >/dev/null || true
          fi
        else
          if [ -n "$2" ]; then
            echo 'headscale was not restarted after the upgrade: its configuration does not pass "headscale check config" (errors above).' >&2
            echo 'A running headscale keeps the previous version only until it restarts: needrestart after apt, a reboot or a crash starts this version, which refuses the configuration and stays stopped until it is fixed.' >&2
          else
            echo 'headscale was not started: its configuration does not pass "headscale check config" (errors above).' >&2
          fi
          echo 'Fix what it names in /etc/headscale/config.yaml; a new install needs server_url.' >&2
          echo '"headscale generate config --minimal" prints the keys every server must set.' >&2
          echo 'Then run: sudo systemctl restart headscale' >&2
          echo 'The check does not see "systemctl edit" drop-ins (variables, an EnvironmentFile, or an ExecStart that points at another config);' >&2
          echo "if headscale's configuration is set there, start it yourself." >&2
        fi
      fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac
