#!/bin/sh
# User sessions are frozen here. Queue system services without waiting; they run the check as each desktop user.
[ "${1:-}" = post ] || exit 0
for runtime in /run/user/[0-9]*; do
    [ -d "$runtime/hypr" ] || continue
    uid=${runtime##*/}
    case "$uid" in *[!0-9]*|'') continue ;; esac
    # The privileged suspend handoff belongs only to the active local desktop, never a remote login.
    session=$(loginctl show-user "$uid" --property=Display --value 2>/dev/null) || continue
    [ -n "$session" ] || continue
    info=$(loginctl show-session "$session" --property=Active --property=Remote --property=Seat 2>/dev/null) || continue
    printf '%s\n' "$info" | grep -qx 'Active=yes' || continue
    printf '%s\n' "$info" | grep -qx 'Remote=no' || continue
    printf '%s\n' "$info" | grep -q '^Seat=.' || continue
    # Restart also replaces a worker whose suspend request is only returning on this wake.
    systemctl --no-block restart "starchaser-lid-resume@$uid.service"
done
