#!/bin/sh -

XNEST=${XNEST-Xnest}
XWM_1ST=CTWM;  export XWM_1ST
XWM_EXIT=true; export XWM_EXIT
#XNEST=${XNEST-Xephyr}	# better but issues with xrdb loading ...

# xnest - personal startup front-end for nested X11 servers
# Steve Kinzler, steve@kinzler.com, Jun 12
# https://kinzler.com/me/home.html#x11

# WARNING: nested displays won't entirely work as desired
# * usable DISPLAY can't be propagated to ssh and xrsh* commands
# * main display window manager will still grab keys before the nested one
#   can use xremote, or Ctrl-Shift with Xephyr
# * some mouse controls/grabs won't work entirely properly
# * there can be apparent backing store display map issues (with Xnest)

case "$XNEST" in
*Xephyr*)	geom=screen;;
*)		geom=geometry;;
esac

desk=
for arg do shift
	case "$arg" in
	-bare)	set -- "$@" -name Xnest-bare;;
	-wuxga)	set -- "$@" -$geom 1920x1200;;
	-fhd)	set -- "$@" -$geom 1920x1080;;
	-uxga)	set -- "$@" -$geom 1600x1200;;
	-wxga)	set -- "$@" -$geom 1366x768;;
	-sxga)	set -- "$@" -$geom 1280x1024;;
	-xga+s)	set -- "$@" -$geom 1152x900;;
	-xga+)	set -- "$@" -$geom 1152x864;;
	-xga)	set -- "$@" -$geom 1024x768;;
	-user)	desk="${XINITRC-"$HOME/etc/xinitrc"}";;
	# `grep ^Exec /usr/share/xsessions/*` for other things to try here
	# but most seem not to work okay
	*:*)	display="$arg"; set -- "$@" "$arg";;
	*)			set -- "$@" "$arg";;
	esac
done

XNEST_DISPLAY="$display"; export XNEST_DISPLAY

auth="-auth ${XAUTHORITY-$HOME/.Xauthority}"
mkxauthority $display

# can't get Xnest geometries to work right with this, placed anywhere in cmd
#exec xinit $desk -- `which Xnest` $auth ${1+"$@"}
# actually this one works, but only without -screen so very small:
#	xinit `which startkde` -- `which Xephyr` $auth :100

$XNEST $auth ${1+"$@"} &

DISPLAY="$display"; export DISPLAY

case "$desk" in
?*)	exec $desk;;
esac
