#!/usr/bin/env bash

DIR="$HOME/.config/"

rofi_command="rofi -theme $DIR/rofi/themes/powermenu.rasi"

# set background for betterlockscreen based on the selected theme  #
Background=$(head ~/.config/bspwm/i3lockBackground)

uptime=$(uptime -p | sed -e 's/up //g')

shutdown=" "
reboot=" "
lock=" "
suspend=" "
logout=" "

# Variable passed to rofi
options="$shutdown\n$reboot\n$lock\n$suspend\n$logout"
_msg="Options  -  yes / y / no / n"

chosen="$(echo -e "$options" | $rofi_command -p "UP - $uptime" -dmenu -selected-row 2)"
case $chosen in
    $shutdown)
		ans=$($HOME/.config/rofi/bin/confirm)
		if [[ $ans == "yes" ]] || [[ $ans == "YES" ]] || [[ $ans == "y" ]]; then
        systemctl poweroff
		elif [[ $ans == "no" ]] || [[ $ans == "NO" ]] || [[ $ans == "n" ]]; then
        exit
        else
        rofi -theme ~/.config/rofi/themes/askpass.rasi -e "$_msg"
        fi
        ;;
    $reboot)
		ans=$($HOME/.config/rofi/bin/confirm)
		if [[ $ans == "yes" ]] || [[ $ans == "YES" ]] || [[ $ans == "y" ]]; then
        systemctl reboot
		elif [[ $ans == "no" ]] || [[ $ans == "NO" ]] || [[ $ans == "n" ]]; then
        exit
        else
        rofi -theme ~/.config/rofi/themes/askpass.rasi -e "$_msg"
        fi
        ;;
    $lock)
        bsplock
        ;;
    $suspend)
		ans=$($HOME/.config/rofi/bin/confirm)
		if [[ $ans == "yes" ]] || [[ $ans == "YES" ]] || [[ $ans == "y" ]]; then
        mpc -q pause
        amixer set Master mute
        betterlockscreen --suspend
        #betterlockscreen --suspend -u /usr/share/backgrounds/"$Background"
		elif [[ $ans == "no" ]] || [[ $ans == "NO" ]] || [[ $ans == "n" ]]; then
        exit
        else
        rofi -theme ~/.config/rofi/themes/askpass.rasi -e "$_msg"
        fi
        ;;
    $logout)
		ans=$($HOME/.config/rofi/bin/confirm)
		if [[ $ans == "yes" ]] || [[ $ans == "YES" ]] || [[ $ans == "y" ]]; then
        bspc quit
		elif [[ $ans == "no" ]] || [[ $ans == "NO" ]] || [[ $ans == "n" ]]; then
        exit
        else
        rofi -theme ~/.config/rofi/themes/askpass.rasi -e "$_msg"
        fi
        ;;
esac

