snips/leave_session.laptop.sh

48 lines
990 B
Bash
Raw Normal View History

2019-05-05 03:18:08 +00:00
#!/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
#
2021-05-16 18:31:19 +00:00
if [[ -v WAYLAND_DISPLAY ]]; then
DMCMD='wofi -d -p'
else
DMCMD='dmenu -p'
fi
2019-05-05 03:18:08 +00:00
confirm() {
2021-05-16 18:31:19 +00:00
ans=$(echo -e "Yes\nNo" | $DMCMD "$1 - Confirm?")
2019-05-05 03:18:08 +00:00
echo $ans
if [ "$ans" = "Yes" ]; then
echo "Leaving..."
else
echo "Abort!"
exit 1
fi
}
2021-05-16 18:31:19 +00:00
cmd=$(echo -e "hibernate\nsuspend\nreboot\nshutdown" | $DMCMD "Leave session:")
2019-05-05 03:18:08 +00:00
echo $cmd
case "$cmd" in
suspend)
confirm "Suspend"
2020-12-25 06:05:18 +00:00
sudo /usr/sbin/pm-suspend-hybrid
2019-05-05 03:18:08 +00:00
;;
hibernate)
confirm "Hibernate"
2020-12-25 06:05:18 +00:00
sudo /usr/sbin/pm-hibernate
2019-05-05 03:18:08 +00:00
;;
reboot)
confirm "Reboot"
sudo /sbin/reboot
;;
shutdown)
confirm "Shutdown"
sudo /sbin/shutdown
;;
esac