#!/usr/bin/env sh

status() {
    ps -ef | grep -v grep | grep -m 1 -q "systemd-inhibit --what=idle"
}

inhibit() {
    systemd-inhibit --what=idle --who=swayidle-inhibit --why=commanded --mode=block sleep $1 &
    waybar-signal idle
}

case $1'' in
'interactive')
    MINUTES=$(echo -e "1\n10\n15\n20\n30\n45\n60\n90\n120\nUnlimited" | rofi -dmenu -p "Select how many minutes to inhibit idle:")
    if [ "$MINUTES" = "Unlimited" ]; then
        SECONDS=$((365 * 24 * 60 * 60))
    else
        SECONDS=$((MINUTES * 60))
    fi
    inhibit $SECONDS
    ;;
'off')
    pkill -U $USER -f "systemd-inhibit --what=idle" || true
    waybar-signal idle
    ;;
esac

# Returns data for Waybar
if status; then
    class="on"
    text="Inhibiting idle (Mid click to clear)"
else
    class="off"
    text="Idle not inhibited"
fi

printf '{"alt":"%s","tooltip":"%s"}\n' "$class" "$text"
