#!/bin/bash

#defaults
GX_POWER_BUTTON_ACTION=shutdown

. /usr/share/groovix/global.conf


#doesn't work due to zenity bug, maybe try yad
#groovix-root-run-in-display zenity --timeout=15 --question --title 'Power Button Pressed'  --text 'This computer will shut down computer in 15 seconds.  Are you sure?' --ok-label " Cancel" --cancel-label ' Shut Down Computer NOW! ' 
#if [ $? -eq 0 ] ; then
#       echo "shutdown cancelled by user"
#else
#       echo "power-button-shutdown" > /var/lib/groovix/shutdown-action
#       /sbin/shutdown -h now "Power button pressed"
#fi

# still asks for the root password, but if we shut down right away that doesn't matter
# just shutdown - don't confirm, don't ask for a password
#zenity --info --text 'Power Button Pressed, Shutting Down NOW!' &

CURDATE=$(date)


if [ "$GX_POWER_BUTTON_ACTION" = "reboot" ] ; then
        groovix-root-run-in-display zenity  --width=400  --warning --title "Rebooting ... " --text  "Please do not turn the computers off unless instructed to by staff.\n\nRebooting  ...\n\n"  &
        echo "power button pressed at $CURDATE , configured to reboot" >> /var/log/groovix/shutdown.log
        echo "power-button-reboot" > /var/lib/groovix/shutdown-action
        /sbin/shutdown -r now "Power button pressed - rebooting"

elif [ "$GX_POWER_BUTTON_ACTION" = "shutdown" ] ; then
        groovix-root-run-in-display zenity  --width=400  --warning --title "Shutting Down ..." --text  "Please do not turn the computers off unless instructed to by staff.\n\nShutting down ...\n\n"  &
        echo "power button pressed at $CURDATE , configured to shutdown" >> /var/log/groovix/shutdown.log
        echo "power-button-shutdown" > /var/lib/groovix/shutdown-action
        /sbin/shutdown -h now "Power button pressed - shutting down"

elif [ "$GX_POWER_BUTTON_ACTION" = "ignore" ] ; then
  # if set to ignore, inore first press but then shutdown if pressed again between 10 seconds and 5 minutes have passed since first press
  # for whatever reason a power button press triggers this script twice 
  #if  [ ! -e /var/run/groovix/power-button-pressed ] ; then
  if test $(find /var/run/groovix/power-button-pressed  -not -newermt '-10 seconds' -newermt '-300 seconds'   2>/dev/null )  ; then
	# power button was hit between 5 and 600 seconds ago, trigger actual shutdown
        groovix-root-run-in-display zenity --width=400  --warning --title "Shutting Down ..." --text  "Please do not turn the computers off unless instructed to by staff.\n\nShutting down ...\n\n"  &
	echo "power button pressed at $CURDATE , configured to ignore, shutting down due to second press" >> /var/log/groovix/shutdown.log
        echo "power-button-shutdown" > /var/lib/groovix/shutdown-action
        /sbin/shutdown -h now "Power button pressed - shutting down"
  else
	# just display a message, don't actually trigger shutdown
	echo "power button pressed at $CURDATE , configured to ignore, ignoring first press" >> /var/log/groovix/shutdown.log
	groovix-root-run-in-display zenity --width=400  --warning --title "Please Do Not Turn The Computers Off" --text  "Please do not turn the computers off, ask staff for help.\n\n\n"  &
	touch /var/run/groovix/power-button-pressed
  fi

else
	# power button action is an unknown value, do nothing
	echo "power button pressed at $CURDATE , doing nothing, GX_POWER_BUTTON_ACTION is set to unrecognized value of $GX_POWER_BUTTON_ACTION" >> /var/log/groovix/shutdown.log
	

fi
