#!/bin/zsh -f

# set -x

# Start a full-screen xwindowmanager session but in a nested window.

###############################################################################
# Do some reality checks:

if [[ ! -x $(which Xnest) &&  ! -x  /usr/X11/bin/Xephyr  ]];then
    print "Xnest and Xephyr do not appear to be on this system"
    print "running the vanilla $0 command"
    command $0
    return 42
fi

if [[ $# != 1 ]];then
	print "Usage: $0 windowmanager-executable-name"
	print "where the argument is eg: enlightenment, twm, startkde, or a similar command."
	return 100
fi

# No point in using this unless we are on OS X:
if [[ $(uname) != Darwin ]];then
	print "This only works for OS X. This does not appear to be OS X."
	print "Don't use this.  Use the real $0"
	return 42
fi

# The window manager must be present.
if [[ ! -x $(which $1) ]]; then
	print "I cannot find the $1 window manager."
	print "Please make sure it is installed properly and in your PATH."
	return 43
fi

# The resolution will not be teleported.
if [[ -n $SSH_TTY ]]; then
	print "This won't work on a remote display"
	return 44
fi
###############################################################################


################ Make a ~/.xinitrc file for the specific wm on the fly ####################
################ unless one already exists in your home directory      ####################

if [[ ! -f ~/.x$1 ]]; then

cat >| ~/.x$1  << eof
#!/bin/zsh -f

# Check for fink
if [[ -d /sw/bin ]];then
	source /sw/bin/init.sh &
fi

# Check for rxvt ( or put your favorite xterminal here)

alias rxvt="" ; unalias rxvt

if [[ -x \$(which rxvt) ]];then
	command rxvt -T "\$PWD" -bg black -fg white -cr grey -ls -geometry 80X25+100+50 -colorBD yellow -troughColor grey -font 9x15 -sr -scrollColor RoyalBlue3 -internalBorder 5 & 
	command rxvt -T "\$PWD" -bg black -fg white -cr grey -ls -geometry 80X25+125+75 -colorBD yellow -troughColor grey -font 9x15 -sr -scrollColor RoyalBlue3 -internalBorder 5 & 
else
	command xterm -geometry 80X25+100+50 -bg black -fg white -sb -sl 1000 -rightbar  -T "\$PWD" &
	command xterm -geometry 80X25+125+75 -bg black -fg white -sb -sl 1000 -rightbar  -T "\$PWD" &
fi


exec $1


eof

fi
###############################################################################


# Find the resolution of the monitor:
res_array=($(system_profiler -detailLevel mini | grep Resolution | awk '{print $2 "  "  $4}'))
 
# Shave off 100 or 200 pixels in each dimension
adj_horiz=$((res_array[1]-200))
adj_vert=$((res_array[2]-100))

# Do this as a subprocess to avoid noise about the process finishing.

# set -x

if [[  -x  /usr/X11/bin/Xephyr   ]]; then
    ( xinit ~/.x$1 -- /usr/X11/bin/Xephyr :$$[3]$$[4]  -screen ${adj_horiz}x${adj_vert}+100+0 -br  -I  2>| /dev/null & )


elif [[  -x  /usr/X11/bin/Xnest   ]]; then
    ( xinit ~/.x$1 -- /usr/X11/bin/Xnest :$$[3]$$[4]  -br  -name "$1 nested session (initiated in $PWD on $HOST)" \
                      -geometry ${adj_horiz}x${adj_vert}+100+0 2>| /dev/null & )
  
elif [[ -x  /usr/X11R6/bin/Xnest  ]]; then
    ( xinit ~/.x$1 -- /usr/X11/bin/Xnest :$$[3]$$[4]  -br  -name "$1 nested session (initiated in $PWD on $HOST)" \
                      -geometry ${adj_horiz}x${adj_vert}+100+0 2>| /dev/null & )

else
	print "Could not find Xnest or Xephyr"
	return 1000
fi


 
