#!/usr/bin/env bash
# Guard wrapper for hyprlock (0.9.x): upstream has crashes on DPMS-off / monitor
# power transitions while the lock screen is running (hyprlock#991, #953, #695).
# If the real hyprlock dies, this restarts it so the session doesn't end up in a
# "lockscreen app died" state. Requires misc:allow_session_lock_restore = true
# in Hyprland config for the replacement lock to be accepted.

set -u

REAL_HYPRLOCK=/usr/bin/hyprlock
MAX_RESTARTS=20

restart=0
while true; do
    "$REAL_HYPRLOCK" "$@"
    code=$?

    # 0: exited cleanly (e.g. unlocked) -- done.
    # 1: auth failed, retry -- let the loop back off and try again.
    if [ "$code" -eq 0 ]; then
        exit 0
    fi

    restart=$((restart + 1))
    if [ "$restart" -ge "$MAX_RESTARTS" ]; then
        exit "$code"
    fi

    # Brief pause so a crash loop doesn't burn CPU (fade back in quickly).
    sleep 0.5
done
