#!/bin/sh

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2020"
app="$(basename $0)"
msgid="The Starbuntu application
<i>\$app</i>
was written by"
info="$(gettext "$msgid" | (export PATH `envsubst --variables "$msgid"`; envsubst "$msgid"))"

yad --center --fixed --title="$title" --window-icon="info" --image="info" --borders=20 --text="$info"'\n''\n'"$author"'\n'"$mail"'\n''\n'"$year"'\n' --text-align=center --selectable-labels --button="OK" &
wunwa "$title" > /dev/null; xdotool key Home key Down
}

bye () {
  aplay $HOME/.config/start-stop.wav
  $ENABLE_SESSION_SAVING && [ -n "$(wmctrl -l | grep -v '\-1')" ] && systemctl hibernate && exit  # Save session if desired in case window open
}

hibernate () {
  systemctl hibernate
}

monitor_off () {
  xtrlock
}

log_out () {
  bye
  killall -u $user
}

pause () {
  systemctl suspend
}

reboot () {
  bye
  systemctl reboot
}

shut_down () {
  bye
  systemctl poweroff
}

param="$1"
[ "$param" = "info" ] && Info && exit 0
user=$(whoami)

case "$param" in
    -h|--hibernate ) hibernate ;;
    -l|--log-out ) log_out ;;
    -m|--monitor-off ) monitor_off ;;
    -p|--pause ) pause ;;
    -r|--reboot ) reboot ;;
    -s|--shut-down ) shut_down ;;
    * ) call="$(gettext "Call")"
	button="$(gettext "Help")"
	lang_dir="$(gettext "C")"
	text="    $call:"'\n''\n'"    <i>terminate</i> -h | --hibernate"'\n'"    <i>terminate</i> -l | --log-out"'\n'"    <i>terminate</i> -m | --monitor-off"'\n'"    <i>terminate</i> -p | --pause"'\n'"    <i>terminate</i> -r | --reboot"'\n'"    <i>terminate</i> -s | --shut-down"
	yad --center --title="terminate" --window-icon="gnome-logout" --image=gtk-dialog-warning --borders=20 --text="$text" --button=$button:"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#terminate" --button="Info":"$0 info" --button="OK"; exit 1 ;;
esac
    
exit 0
