#!/bin/bash
#z2buntu Shutdown script for X

#Set Display for matchbox-remote
export DISPLAY=:0
#Suppress GTK errors so xdialog works properly
export XDIALOG_NO_GMSGS=1

XFBPID=`pidof Xfbdev`
if [ -n "$XFBPID" ]; then
#If X is running, display shutdown menu
Xdialog --title "Z2 Shutdown" \
        --menu "Choose a shutdown option:" 24 51 3 \
        "Quit X"  "Go To command line" \
	"Suspend" "Quit X and suspend" \
        "Reboot" "Reboot the Zipit" \
        "Shutdown" "Halt the Zipit" 2> /tmp/menu.tmp.$$

retval=$?
choice=`cat /tmp/menu.tmp.$$`
rm -f /tmp/menu.tmp.$$

case $retval in
  0)
    case "$choice" in
	"Quit X")
	  # Kill Xfbdev
	  kill `pidof Xfbdev`
	  # Kill the mouse too
	  kill `pidof z2mouse-option`	  
	  ;;
	"Suspend")
	  kill `pidof Xfbdev`
	  kill `pidof z2mouse-option`
	  op suspend
	  ;;
	"Reboot")
	  op reboot
	  ;;
	"Shutdown")
	  op shutdown
	  ;;
    esac
  ;;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed.";;
esac

else
  #Otherwise, suspend or whatever cli stuff you want
  #apm -s
  pm-suspend
fi
