#!/bin/bash
set -euo pipefail

THEME=${1:-logout}

question=$(echo "󰌾 lock|󰍃 logout|󰜉 reboot|󰐥 shutdown" | rofi -sep "|" \
    -dmenu -i -p 'System: ' "" \
    -hide-scrollbar \
    -eh 1 \
    -color-enabled true \
    -theme "$THEME")

case $question in
    *lock)
        # use lowercase only
        case "${XDG_CURRENT_DESKTOP,,}" in
            hyprland)
                hyprctl dispatch exit
                ;;
            *)
                if [ "$(pgrep -x lightdm)" ] && [ "$(command -v light-locker)" ]; then
                    light-locker-command -l
                fi
                ;;
        esac
        ;;
    *logout)
        case "${XDG_CURRENT_DESKTOP,,}" in
            hyprland)
                hyprctl dispatch exit
                ;;
            openbox)
                openbox --exit
                ;;
            i3)
                i3-msg exit
                ;;
            qtile)
                qtile cmd-obj -o cmd -f shutdown
                ;;
        esac
        ;;
    *reboot)
        if [[ $(command -v systemctl) ]]; then
            systemctl reboot
        else
            shutdown -r now
        fi
        ;;
    *shutdown)
        if [[ $(command -v systemctl) ]]; then
            systemctl poweroff
        else
            poweroff
        fi
        ;;
    *)
        exit 0  # do nothing on wrong response
        ;;
esac
