#!/bin/bash

# omarchy:summary=Reconcile displays and wake the screen when the laptop lid opens
# omarchy:group=system
# omarchy:hidden=true

# On non-ACPI laptops, omarchy-hw-laptop-closed queries logind, which reads the
# switch independently of Hyprland and can briefly lag behind switch:off.
# Re-check once before treating the lid as closed.
lid_closed() {
  omarchy-hw-laptop-closed && sleep 0.1 && omarchy-hw-laptop-closed
}

# Hyprland can replay a switch binding while the lid is still closed, and a
# closed lid must never light the panel. When open and locked, wake through the
# lock service (which runs omarchy-system-wake, resumes fingerprint auth, and
# re-arms the blank timer); fall back to omarchy-system-wake when unlocked or
# if the shell cannot answer. Both wake paths reconcile clamshell state.
if lid_closed; then
  omarchy-hyprland-monitor-clamshell
elif ! wake_reply=$(omarchy-shell lock wake 2>/dev/null) || [[ $wake_reply != "ok" ]]; then
  omarchy-system-wake >/dev/null 2>&1 || true
fi
