snips/leave_session.laptop.sh

48 lines
990 B
Bash
Executable File

#!/bin/bash
#
# For this to work, something along the following lines needs to be
# in a file in /etc/sudoers.d/. The file must have 0440 permissions
#
# username ALL=(ALL:ALL) NOPASSWD:/usr/sbin/pm-suspend,/usr/sbin/pm-hibernate,/sbin/reboot,/sbin/shutdown
#
if [[ -v WAYLAND_DISPLAY ]]; then
DMCMD='wofi -d -p'
else
DMCMD='dmenu -p'
fi
confirm() {
ans=$(echo -e "Yes\nNo" | $DMCMD "$1 - Confirm?")
echo $ans
if [ "$ans" = "Yes" ]; then
echo "Leaving..."
else
echo "Abort!"
exit 1
fi
}
cmd=$(echo -e "hibernate\nsuspend\nreboot\nshutdown" | $DMCMD "Leave session:")
echo $cmd
case "$cmd" in
suspend)
confirm "Suspend"
sudo /usr/sbin/pm-suspend-hybrid
;;
hibernate)
confirm "Hibernate"
sudo /usr/sbin/pm-hibernate
;;
reboot)
confirm "Reboot"
sudo /sbin/reboot
;;
shutdown)
confirm "Shutdown"
sudo /sbin/shutdown
;;
esac