#!/usr/bin/env bash
# Presentation mode: keep the screen awake, and say so with a themed icon.
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
# Licensed under the GNU General Public License v2 only.
#
# waybar's own idle_inhibitor module draws its state with format-icons, which
# is text. This drives the same idea through hypridle and reports the state as
# an icon path for an `image` module instead.
#
#     wb-idle status   # print "$path\n$tooltip" for the image module
#     wb-idle toggle   # flip the inhibitor, then refresh the module
#
# The inhibitor is hypridle's own pause, so nothing else has to be running.
set -euo pipefail

state_file="${XDG_RUNTIME_DIR:-/tmp}/wb-idle.state"

active() { [[ -f "$state_file" ]]; }

case "${1:-status}" in
toggle)
	if active; then
		rm -f "$state_file"
		# Resume idling. Guarded: hypridle may not be running, and that is
		# not an error worth failing a click over.
		pkill -USR2 -x hypridle 2>/dev/null || true
	else
		touch "$state_file"
		pkill -USR1 -x hypridle 2>/dev/null || true
	fi
	# Refresh the module now rather than waiting for its interval.
	pkill -RTMIN+9 -x waybar 2>/dev/null || true
	;;
status)
	if active; then
		wb-icon idle activated
	else
		wb-icon idle deactivated
	fi
	;;
*)
	echo "usage: wb-idle <status|toggle>" >&2
	exit 2
	;;
esac
