#!/bin/sh
# Privileged display-manager verbs for the punktfunk managed gamescope takeover.
#
# On DM-autologin boxes whose display manager does not survive a masked session unit (Nobara's
# plasmalogin, unknown DMs), taking the Gaming Mode session over means stopping the display
# manager for the length of the stream and restarting it afterwards — root-only operations. The
# host invokes this helper via pkexec under its own polkit action
# (io.unom.punktfunk.dm-helper.policy, installed by the packages), the same mechanism these
# distros use for their own session switching (Nobara's os-session-select).
#
# The unit is NEVER caller-controlled: it is derived here from the display-manager.service alias
# symlink, so the polkit grant's blast radius is exactly "the box's own display manager" — a
# local-seat operation, not arbitrary unit management.
set -eu

dm_unit() {
  target=$(readlink /etc/systemd/system/display-manager.service) || {
    echo "pf-dm-helper: no display-manager.service alias — no display manager to manage" >&2
    exit 1
  }
  basename "$target"
}

case "${1-}" in
  stop)
    exec systemctl stop "$(dm_unit)"
    ;;
  restore)
    dm=$(dm_unit)
    # reset-failed first: a relogin loop may have tripped the unit's start limit, and a plain
    # restart would be refused until the accounting is cleared.
    systemctl reset-failed "$dm" 2>/dev/null || :
    exec systemctl restart "$dm"
    ;;
  linger)
    # Keep the caller's own `systemd --user` manager alive across the stop. Stopping the display
    # manager ends the user's last login session, and logind then stops user@<uid>.service after
    # UserStopDelaySec (10s by default) — which takes the host that asked for the takeover with
    # it, so nothing is left to restart the display manager and the box stays dark. Lingering is
    # what breaks that dependency (the setup docs already ask for it).
    #
    # The user is NEVER caller-named: PKEXEC_UID is set by pkexec from the authenticated caller,
    # so this grant enables lingering for that caller alone.
    uid=${PKEXEC_UID:-}
    [ -n "$uid" ] || {
      echo "pf-dm-helper: no PKEXEC_UID in the environment — refusing to guess a user" >&2
      exit 1
    }
    exec loginctl enable-linger "$uid"
    ;;
  *)
    echo "usage: pf-dm-helper stop|restore|linger" >&2
    exit 2
    ;;
esac
