#!/bin/sh
# called from shutdown menu
# config files: ~/.jwmrc, etc
# script: wmexit -- symlinks: wmreboot wmpoweroff restartwm

export TEXTDOMAIN=wmexit
export OUTPUT_CHARSET=UTF-8

script=${0##*/}
script2=$1

case $script in wmexit|wmreboot|wmpoweroff|restartwm) OK=1 ;; esac
case $script2 in wmexit|wmreboot|wmpoweroff|restartwm) script=$script2 ; shift ; OK=1 ;; esac
[ ! "$OK" ] && echo "ERROR: $script is not valid" && exit 1
echo $script

[ -f /etc/wmexit.conf ] && . /etc/wmexit.conf
if [ "$WMEXIT_SPLASH" = "yes" ] ; then
	[ $WMEXIT_SPLASH_BG ] && BG="-bg $WMEXIT_SPLASH_BG"
	[ $WMEXIT_SPLASH_FG ] && FG="-fg $WMEXIT_SPLASH_FG"
	case $script in
		restartwm)  SMSG="$(gettext 'Restarting window manager....')" ;;
		wmexit)     SMSG="$(gettext 'Exiting to console....')" ;;
		wmreboot)   SMSG="$(gettext 'Rebooting....')" ;;
		wmpoweroff) SMSG="$(gettext 'Powering down....')" ;;
	esac
	yaf-splash -placement top $BG $FG -timeout 10 -text "$SMSG" &
fi

if pidof lxpanel >/dev/null 2>&1 ; then #probably running LXDE
	#http://wiki.lxde.org/en/LXPanel#Fix_empty_menu_in_LXPanel
	find ~/.cache/menus -name '*' -type f -delete
fi

#--------------
case $script in
   restartwm)
      ## Exit from window manager ##
      # will cause return to xwin, which reads
      # /tmp/wmexitmode.txt and will relaunch w.m.
      NEXTWM="$1"
      [ $NEXTWM ] || NEXTWM="`cat /etc/windowmanager`" #precaution.
      echo -n "$NEXTWM" > /etc/windowmanager #this makes change permanent.
      EXITMODE="$NEXTWM"
      ;;
   wmexit)
      ## Exit from X, will cause return to xwin ##
      EXITMODE="exit"
      ;;
   wmreboot|wmpoweroff)
      ## reboot/shutdown ##
      . /etc/rc.d/PUPSTATE
      if [ $PUPMODE -eq 5 ] && [ -n "$DISPLAY" -o -n "$WAYLAND_DISPLAY" ] ; then #first shutdown.
         shutdownconfig #dlgs for creating save-file.
         #...writes results to /tmp/shutdownconfig_results, which /etc/rc.d/rc.shutdown reads.
         [ $? -eq 255 ] && exit #abort shutdown.
      elif [ $PUPMODE -eq 13 ] ; then
         . /etc/eventmanager
         [ "$ASKTOSAVE" = "true" ] && asktosave_session --file
         #...writes results to /tmp/session_ramdisk_result, which /etc/rc.d/rc.shutdown reads.
      elif [ $PUPMODE -eq 66 ] ; then
         asktosave_session --file
      fi
      [ "$script" = 'wmreboot' ] && EXITMODE="reboot"
      [ "$script" = 'wmpoweroff' ] && EXITMODE="poweroff"
      ;;
esac

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

###########################################################

while [ "`pidof snapmergepuppy`" ] ; do sleep 1 ; done
sleep 0.2

sync

         #xfce       #lxde     #LXQt        #window managers
for i in xfce4-panel lxsession lxqt-session `cat /etc/windowmanager` jwm icewm openbox X Xorg
do
	for pid in `pidof $i` ; do
		kill $pid || kill -9 $pid
	done
done

# workaround for dwl: before b47c79d7, libseat leaks a socket to children of dwl
# and dwl deadlocks during shutdown; the first SIGTERM makes dwl start the
# shutdown sequence and the second one unblocks it when it uses the other end of
# the leaked socket
if [ -n "$WAYLAND_DISPLAY" ]; then
	dwl=`pidof dwl`
	[ -n "$dwl" ] && nohup sh -c "while kill $dwl 2>/dev/null; do sleep 1; done" > /dev/null &
fi

### END ###
