#!/bin/bash

# Exit early if we don't have the tte show
if ! command -v tte &>/dev/null; then
  exit 1
fi

# Exit early if screensaver is already running
pgrep -f "omarchy-cmd-screensaver" && exit 0

# Allow screensaver to be turned off but also force started
if [[ -f ~/.local/state/omarchy/toggles/screensaver-off ]] && [[ $1 != "force" ]]; then
  exit 1
fi

focused=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true).name')

for m in $(hyprctl monitors -j | jq -r '.[] | .name'); do
  hyprctl dispatch focusmonitor $m

  hyprctl dispatch exec -- \
    xdg-terminal-exec --app-id=com.omarchy.screensaver \
    omarchy-cmd-screensaver
done

hyprctl dispatch focusmonitor $focused
