#!/bin/sh
#introduced v0.9.8
#called from /root/.fvwm95rc, .jwmrc at shutdown.
#110918 call new script 'shutdownconfig'.
#120216 offer to set default language in initrd.
#130221 return code 255 to abort shutdown.
#141206 remove PUPMODE==5 test, /initrd test.
#141206 fix if only have busybox ps.
#150327 for frugal install, optional offer to save session.
#151107 ditto 150327, for live-cd with saved session.
#161009 /etc/xdg/templates/_root_.jwmrc now has Shutdown -> Reboot to commandline
#180128 easy linux, kill containers, moved from rc.shutdown. see also wmpoweroff, wmexit, restartwm
#200203 for 180128 add 2>/dev/null
#200726 shutdown menu now has "Reboot, with filesystem check". see also: /etc/xdg/templates/_root_.jmwrc, /etc/rc.d/rc.shutdown, init in initrd
#200727 shutdown menu now has "Reboot, with rollback". see also: /etc/xdg/templates/_root_.jmwrc
#200804 fix path.

. /etc/rc.d/PUPSTATE

[ "${1}" == "nox" ] && touch /root/bootcnt.txt #161009 see /root/.profile at bootup.

if [ "${1}" == "fscheck" ];then #200726 see /etc/xdg/templates/_root_.jmwrc
 echo -n "${WKG_DEV},${WKG_FS},REQUEST" > /.fsckme.flg #200804
fi

if [ "${1}" == "rollback" ];then #200726 see /etc/xdg/templates/_root_.jmwrc
  exec easy-rollback
fi

if [ $DISPLAY ];then #paranoid precaution, check X still running
 #popup 'placement=center background=#ffa000 level=top fontname="DejaVu Sans 14"|Please wait...'
 true
 
 #180128 easy linux, kill containers...
 for aCONT in `ls -1 /mnt/${WKG_DEV}/${WKG_DIR}containers 2>/dev/null | tr '\n' ' '`
 do
  [ "$(mount | grep "/containers/${aCONT}/container")" == "" ] && continue
  /usr/local/easy_containers/stop-container ${aCONT}
 done

 #150327 for frugal install, offer to save session:
 touch /etc/rc.d/install.log
 . /etc/rc.d/install.log #has INSTALLED_MODE=livecd, frugal, drive (full), partition (full)
 if [ "$INSTALLED_MODE" == "frugal" -o "$INSTALLED_MODE" == "livecd" ];then #151107
  if [ -s /var/local/frugal-save-ask-flag ];then
   [ "$(cat /var/local/frugal-save-ask-flag)" == "true" ] && savesession
  fi
 fi
 
fi

#/sbin/pup_event_frontend_d will quit if this file exists...
echo -n "reboot" > /tmp/wmexitmode.txt

#v403 give time for /sbin/pup_event_frontend_d to quit.
#w478 testing 2.6.18.1 kernel, pup_event_frontend_d did not exit when it
#detected /tmp/wmeximode.txt, but became 'defunct'...
KCNT=0
#PSPEFD="`ps -C pup_event_frontend_d | grep 'pup_event_front' | grep -v 'defunct'`"
#141206 may only have busybox ps, does not support -C ...
PSPEFD="`busybox ps | grep -v 'grep' | grep 'pup_event_front' | grep -v 'defunct'`"
#while [ "`pidof pup_event_frontend_d`" != "" ];do
while [ "$PSPEFD" != "" ];do
 sleep 0.5
 KCNT=`expr $KCNT + 1`
 [ $KCNT -gt 60 ] && break #30 secs.
 #PSPEFD="`ps -C pup_event_frontend_d | grep 'pup_event_front' | grep -v 'defunct'`"
 PSPEFD="`busybox ps | grep -v 'grep' | grep 'pup_event_front' | grep -v 'defunct'`" #141206
done
sleep 0.2

sync

#w003
if [ "`pidof xfce4-panel`" != "" ];then
 #killall xfce4-panel
 kill `pidof xfce4-panel`
 exit
fi

#w003
CURRENTWM="`cat /etc/windowmanager`"
kill -9 `pidof $CURRENTWM`

#killall X #default is SIGTERM.
#sleep 1           #v1.0.7
#exec killall -9 X #v1.0.7 make sure X is killed.

#after exit from X, there is a return to /usr/X11R7/bin/xwin,
#which reads /tmp/wmexitmode.
