#!/usr/bin/sh

isPhone(){
	case "$(hostname)" in
		*-phone) return 0 ;;
		*) return 1 ;;
	esac
}

if type -p rofi 2> /dev/null; then
	if isPhone; then
		theme="themes/power-phone.rasi"
	else
		theme="themes/power.rasi"
	fi
	alias selectcommand="rofi -dmenu -i -theme $theme -me-select-entry '' -me-accept-entry MousePrimary"
	shutdown=""
	reboot=""
	lock=""
	suspend=""
	logout="﫼"
else
	alias selectcommand="dmenu -i -p 'Option'"
	shutdown="Shutdown"
	reboot="Reboot"
	lock="Lock"
	suspend="Suspend"
	logout="Logout"
fi

hostname="$(hostname)"

if isPhone; then
	selection="$(echo -e "$shutdown\n$reboot\n$lock\n$logout" | selectcommand)"
else
	notify-send "Not phone"
	selection="$(echo -e "$shutdown\n$reboot\n$lock\n$logout\n$suspend" | selectcommand)"
fi

sleep .2

case $selection in
	$lock)
		if isPhone; then
			screenlock --suspend
		else
			i3exit lock
		fi
		;;
	$logout)
		pkill dwm
		;;
	$suspend)
		systemctl suspend
		if !isPhone; then
			i3exit lock
		fi
		;;
	$reboot)
		systemctl reboot
		;;
	$shutdown)
		systemctl poweroff
		;;
esac
