#!/usr/bin/env bash

# omarchy:summary=Returns true when the compositor holds a session lock
# omarchy:hidden=true

# Exits 0 locked, 1 unlocked, 2 undetermined, as omarchy-hyprland-session-locked
# does. sway reports no lock state over IPC, so the shell leaves
# $XDG_RUNTIME_DIR/omarchy-session-lock naming the socket of the session it
# locked and removes it on unlock. A marker a dead shell left behind in this
# session is the stranded lock worth detecting; one from an earlier sway names
# a socket that is gone.

case $("${BASH_SOURCE[0]%/*}/omarchy-compositor") in
  hyprland) exec "${BASH_SOURCE[0]%/*}/omarchy-hyprland-session-locked" ;;
  sway) ;;
  *) exit 2 ;;
esac

socket=${SWAYSOCK:-$(ls -t "${XDG_RUNTIME_DIR:-/run/user/$UID}"/sway-ipc.*.sock 2>/dev/null | head -n1)}
marker=${XDG_RUNTIME_DIR:-/run/user/$UID}/omarchy-session-lock

[[ -n $socket && -f $marker ]] || exit 1
[[ $(<"$marker") == "$socket" ]]
