#!/bin/sh
#Exit from window manager will cause return to xwin, which reads
#/tmp/wmexitmode.txt and will relaunch w.m.

NEXTWM="$1"
PREVIOUSWM="`cat /etc/windowmanager`"
[ "$NEXTWM" = "" ] && NEXTWM="$PREVIOUSWM" #v1.0.7 precaution.
echo -n "$NEXTWM" > /etc/windowmanager #this makes change permanent.
#/sbin/pup_event_frontend_d will quit if this file exists...
echo -n "$NEXTWM" > /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'`"
#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'`"
done
sleep 0.2

sync

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

kill -9 `pidof $PREVIOUSWM` #w478

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