#!/bin/sh
set -eu

choice=$(printf '%s
' 'Lock' 'Suspend' 'Log out' 'Reboot' 'Power off' | fuzzel --dmenu --prompt='Session: ' --lines=5) || exit 0
case "$choice" in
    Lock)
        loginctl lock-session
        ;;
    Suspend)
        systemctl suspend
        ;;
    'Log out')
        desktop=$(printf '%s' "${XDG_CURRENT_DESKTOP:-${XDG_SESSION_DESKTOP:-}}" | tr '[:upper:]' '[:lower:]')
        case "$desktop" in
            *hyprland*) hyprctl dispatch exit ;;
            *niri*) niri msg action quit ;;
            *labwc*) labwc --exit ;;
            *sway*) swaymsg exit ;;
            *wayfire*) wayland-logout ;;
            *)
                session=${XDG_SESSION_ID:-$(loginctl show-user "$USER" -p Display --value)}
                [ -n "$session" ] && loginctl terminate-session "$session"
                ;;
        esac
        ;;
    Reboot)
        systemctl reboot
        ;;
    'Power off')
        systemctl poweroff
        ;;
esac
