#!/bin/sh
set -eu
actions_icons='/usr/share/icons/gnome/16x16/actions'
awk_who='
BEGIN {
display_ere = " [(]" display "[)]$";
printed = 0;
}
NR == 1 {
headings = $0;
}
NR > 1 {
if (!match($0, display_ere)) {
if (!printed) {
print("\n\nThe following sessions " \
"will also be hung up:\n");
print(" " headings \
"");
printed = 1;
}
print(" " $0 "");
}
}
'
do_logout()
{
zenity --no-wrap --question --title='Log Out' \
--window-icon="${actions_icons}/system-log-out.png" \
--text='Are you sure you want to log out?' \
--ok-label='Log Out' --cancel-label='Cancel' && \
printf 'quit\n' >~/.xsessionmanage
return 0
}
do_shutdown()
{
zenity --no-wrap --question --title='Shut Down' \
--window-icon="${actions_icons}/system-shutdown.png" \
--text="Are you sure you want to shut down this system?$(\
who -H | awk -v "display=${DISPLAY}" "${awk_who}")" \
--ok-label='Shut Down' --cancel-label='Cancel' && \
sudo shutdown -hP now
return 0
}
do_reboot()
{
zenity --no-wrap --question --title='Reboot' \
--window-icon="${actions_icons}/system-shutdown.png" \
--text="Are you sure you want to reboot this system?$(\
who -H | awk -v "display=${DISPLAY}" "${awk_who}")" \
--ok-label='Reboot' --cancel-label='Cancel' && \
sudo reboot
return 0
}
do_s2mem()
{
zenity --no-wrap --question --title='Suspend' \
--window-icon="${actions_icons}/media-playback-pause.png" \
--text='Are you sure you want to suspend this system?' \
--ok-label='Suspend' --cancel-label='Cancel' && \
sudo dd of='/sys/power/state' <<-EOF
mem
EOF
return 0
}
do_s2disk()
{
zenity --no-wrap --question --title='Hibernate' \
--window-icon="${actions_icons}/media-playback-pause.png" \
--text='Are you sure you want to hibernate this system?' \
--ok-label='Hibernate' --cancel-label='Cancel' && \
sudo dd of='/sys/power/state' <<-EOF
disk
EOF
return 0
}
usage()
{
printf 'Usage: %s {logout|shutdown|reboot|s2mem|s2disk}\n' "${0}"
return 0
}
main()
{
local action=
if [ ${#} -ne 1 ]; then
usage 1>&2
return 1
fi
action="${1}"
shift 1
read -r DISPLAY 0<~/.xdisplay
case "${action}" in
'logout' | 'shutdown' | 'reboot' | 's2mem' | 's2disk')
"do_${action}"
;;
*)
usage 1>&2
return 1
;;
esac
return 0
}
main "${@}"