#!/bin/sh
# Launch a nested session inside FVWM-Crystal using Xephyr
# Syntax: launchwm $[vp.width] $[vp.height] $[infostore.Your_Id] <wm>

# next free $DISPLAY, needed for nested wm in nested wm :)
XephyrDisplay=$((`echo $DISPLAY|cut -d: -f2|cut -d. -f1`+1))
# nested session dimension; you can change it
#XephyrWidth=$((`echo $(($1-200))`))
#XephyrHeight=$((`echo $(($2-100))`))
XephyrWidth=$1
XephyrHeight=$2

EXECWM=`grep Exec $4 | grep -v Try | sed -e 's:Exec=::'`


# launch Xephyr
Xephyr -ac -reset -title `basename $EXECWM` -screen "$XephyrWidth"x"$XephyrHeight" :$XephyrDisplay &
XEPHYR_PID=`echo $!`

# wait and bring Xephyr in full screen
sleep 0.2
FvwmCommand "All (Xephyr) Fullscreen-Start"

# launch the wm
# We dont want the fvwm and fvwm-themes files to be mixed with fvwm-crystal
ENVWM=`basename $EXECWM`
if [ "$ENVWM" = "fvwm-themes-start" ] ; then
    FVWM_USERDIR="/home/$3/.fvwm" DISPLAY=:$XephyrDisplay $EXECWM 2>/dev/null
else
    if [ "$ENVWM" = "fvwm" ] ; then
	FVWM_USERDIR="/home/$3/.fvwm" DISPLAY=:$XephyrDisplay $EXECWM 2>/dev/null
    else
	DISPLAY=:$XephyrDisplay $EXECWM 2>/dev/null
    fi
fi

# stop Xephyr
FvwmCommand "All (Xephyr) Fullscreen-Stop"
sleep 0.2
kill $!
