#!/bin/sh
# Patriot Jan 2009 for Puppy Linux 4.1.1 GPL
# Revision 0.0.1
# 13sep09 dialogbox by shinobar

_farewell="Power button is pushed, and about to shut down..."
_press_ok="Press 'OK' to shutdown right now,"
_or_cancel="'Cancel' to continue puppy."
_ten_sec="Shutting down in 10 seconds."
#echo $0 > /root/acpi.log
#date >> /root/acpi.log

SOUND="/usr/share/audio/bark.au"
PLAY="aplay"
[ -f "$SOUND" ] && which $(basename $PLAY) >/dev/null && $(basename $PLAY) "$SOUND" 

#echo "DISPLAY=$DISPLAY" >> /root/acpi.log
X_pid=`ps ax | awk '{if (match($5, "X$") || $5 == "X") print $1}'`
if [ "$X_pid" != "" ]; then
#if [ "$DISPLAY" != "" ]; then
 [ -f /etc/rc.d/PUPSTATE ] && source /etc/rc.d/PUPSTATE
 [ -f /etc/rc.d/pupsave.conf ] && source /etc/rc.d/pupsave.conf
 GTKDIALOG=$(which gtkdialog3)
 PUPSAVECONFIG=$(which pupsaveconfig)
 [ "$PUPSAVECONFIG" = "" ] && PUPSAVECONFIG=$(which pupsave)
 if [ "$GTKDIALOG" != "" ] && [ "$PUPMODE" != "5" -o "$PRECHOICE" != "" -o "$PUPSAVECONFIG" = "" ]; then

mo=acpi.mo
# set locale
for lng in C $(echo $LANGUAGE|cut -d':' -f1) $LC_ALL $LANG;do :;done   # ex.    ja_JP.UTF-8
# search locale file
lng1=$(echo $lng|cut -d'.' -f1)      # ex.   ja_JP
lng2=$(echo $lng|cut -d'_' -f1)   # ex.   ja
LOCALEDIR=/usr/share/locale
[ "$mo" ] || mo=$(basename $0).mo
for D in en C $lng2 $lng1 $lng
do
   F="$LOCALEDIR/$D/LC_MESSAGES/$mo"
   [ -f "$F" ] && . "$F"
done

 	export DIALOG="<window title=\"acpid\"><vbox>
  <text><label>$_farewell</label></text>
  <text><label>$_press_ok</label></text>
  <text><label>$_or_cancel</label></text>
  <progressbar><label>$_ten_sec</label>
      <input>for i in \$(seq 0 10 100); do echo \$i; sleep 1; done</input>
      <action type=\"exit\">OK</action>
  </progressbar>
  <hbox>
   <button cancel></button>
   <button ok></button>
  </hbox>
 </vbox></window>"
 #echo "$DIALOG"
	eval $($GTKDIALOG -p DIALOG -c)
	[ "$EXIT" = "Cancel" ] && exit
 fi
 /usr/X11/bin/wmpoweroff
else
 /sbin/poweroff
fi
