#!@SHELL_LOGIN@
# @configure_input@

########################################################################
# -*- sh -*-                                                           #
#                                                                      #
# ~/.Xclients: used by startx (xinit) to start up a window manager and #
# any other clients you always want to start an X session with.        #
#                                                                      #
# This version of Xclients is intended for use with wdm.  This         #
# separate file is used rather than the Red Hat Linux Xclients         #
# file located as /etc/X11/xinit/Xclients because it has been          #
# extensively modified to handle starting many different window        #
# or session managers and do that on systems other than Red Hat Linux. #
#                                                                      #
# Systems other than RedHat Linux will like need editing of this       #
# file to tailor the file for the particular system.                   #
#                                                                      #
########################################################################

# try to find executable in PATH
FindInPath()
{
	prog=$1
	shift
	result=
	for p in `echo $PATH|tr ':' ' '`; do
		if test -e $p/$prog; then
			result=$p/$prog
			break;
		fi
	done
}



# these files are left sitting around by TheNextLevel.
rm -f $HOME/Xrootenv.0
rm -f /tmp/fvwmrc* 2>/dev/null

# check for and set x-resources
if [ -f $HOME/.Xresources ] ; then
	@XRDB_PATH@ -merge $HOME/.Xresources
fi

if [ -f $HOME/.Xdefaults ] ; then
	@XRDB_PATH@ -merge $HOME/.Xdefaults
fi

WINMGR_NAME=@WINMGR_NAME@
if test -z "$WINMGR_NAME"; then
	WINMGR_NAME="UserDefined"
fi

# First thing - check the user preferences
if [ -f $HOME/.wm_style ] ; then
	WMSTYLE=`cat $HOME/.wm_style`
	case "$WMSTYLE" in 
#
# first, a general purpose entry:
	$WINMGR_NAME*)
		# startup a window manager
		WINMGR_PATH=@WINMGR_PATH@
		if test -x "$WINMGR_PATH" ; then
	        	echo Starting $WINMGR_NAME >$HOME/.xwm.msgs
			exec $WINMGR_PATH >>$HOME/.xwm.msgs 2>&1
		fi
		;;

	wmaker*|WindowMaker*)
		# startup WindowMaker
		WMAKER_PATH=@WMAKER_PATH@
		if ! test -x $WMAKER_PATH ; then
			FindInPath $WMAKER_PATH
			if test -n "$result" -a -x "$result"; then
				WMAKER_PATH="$result";
			fi
		fi
	
		if test -x $WMAKER_PATH ; then
			echo Starting WindowMaker >$HOME/.xwm.msgs
			env > "$HOME"/Xrootenv.0
			exec $WMAKER_PATH >>$HOME/.xwm.msgs 2>&1
		fi
		;;

	afterstep*|Afterstep*|AfterStep*)
		# we have to start up afterstep
		AFTERSTEP_PATH=@AFTERSTEP_PATH@
		if ! test -x $AFTERSTEP_PATH ; then
			FindInPath $AFTERSTEP_PATH
			if test -n "$result" -a -x "$result"; then
				AFTERSTEP_PATH="$result";
			fi
		fi
		if [ -x $AFTERSTEP_PATH -a -f /usr/share/afterstep/wmconfig.conf ] ; then
			mkdir -p $HOME/GNUstep/Library/AfterStep
			wmconfig --output=afterstep --directories /usr/share/afterstep/wmconfig.conf 2>/dev/null
			env > "$HOME"/Xrootenv.0
			# if this works, we stop here
			eval "exec $AFTERSTEP_PATH" > "$HOME"/.AfterStep-errors 2>&1
		fi
		;;

	blackbox*|BlackBox)
		# startup blackbox
		BLACKBOX_PATH=@BLACKBOX_PATH@
		if ! test -x $BLACKBOX_PATH ; then
			FindInPath $BLACKBOX_PATH
			if test -n "$result" -a -x "$result"; then
				BLACKBOX_PATH="$result";
			fi
		fi
		if [ -x $BLACKBOX_PATH ] ; then
			echo Starting BlackBox >$HOME/.xwm.msgs
			exec $BLACKBOX_PATH >>$HOME/.xwm.msgs 2>&1
		fi
		;;

	icewm*|IceWm)
		# startup icewm
		ICEWM_PATH=@ICEWM_PATH@
		if ! test -x $ICEWM_PATH ; then
			FindInPath $ICEWM_PATH
			if test -n "$result" -a -x "$result"; then
				ICEWM_PATH="$result";
			fi
		fi
		if [ -x $ICEWM_PATH ] ; then
			echo Starting icewm >$HOME/.xwm.msgs
			exec $ICEWM_PATH >>$HOME/.xwm.msgs 2>&1
		fi
		;;

	xsession*|Xsession*)
		# startup xsession
		XSESSION_PATH=@XSESSION_PATH@
		if ! test -x $XSESSION_PATH ; then
			FindInPath $XSESSION_PATH
			if test -n "$result" -a -x "$result"; then
				XSESSION_PATH="$result";
			fi
		fi
		if [ -x $XSESSION_PATH ] ; then
			echo Starting xsession >$HOME/.xwm.msgs
			exec $XSESSION_PATH -console >>$HOME/.xwm.msgs 2>&1
		fi
		;;

	startkde*|kde*|KDE*)
		# startup kde
		STARTKDE_PATH=@STARTKDE_PATH@
		if ! test -x $STARTKDE_PATH ; then
			FindInPath $STARTKDE_PATH
			if test -n "$result" -a -x "$result"; then
				STARTKDE_PATH="$result";
			fi
		fi
		if [ -x $STARTKDE_PATH ] ; then
			echo Starting kde >$HOME/.xwm.msgs
			exec $STARTKDE_PATH -console >>$HOME/.xwm.msgs 2>&1
		fi
		;;
    esac
fi

# first, find an M4-enabled config file (such as 
# the one from AnotherLevel) for fvwm2 or fvwm95.
RCFILE=""
for tryfile in "$HOME/.fvwm2rc.m4" "/etc/X11/AnotherLevel/fvwm2rc.m4"; do
    if [ -f "$tryfile" ]; then
        RCFILE="$tryfile"
        break
    fi
done

# if it really exists, use it; if not, fvwm2 or fvwm95 will
# automagically look for a config file in the regular places.
if [ -n "$RCFILE" ]; then
    FVWMOPTIONS="-cmd 'FvwmM4 -debug $RCFILE'"
else
    FVWMOPTIONS=""
fi

# TheNextLevel is supposed to work
# with both fvwm95 and fvwm2
# (try fvwm95 first, then fvwm2).
for FVWMVER in 95 95-2 2; do
    if [ -n "$(type -path fvwm${FVWMVER})" ]; then
        env > "$HOME"/Xrootenv.0
        # if this works, we stop here
        eval "exec fvwm${FVWMVER} ${FVWMOPTIONS}" > "$HOME"/.FVWM${FVWMVER}-errors 2>&1
    fi
done

# gosh, neither fvwm95 nor fvwm2 is available;
# let's try regular fvwm (AnotherLevel doesn't work with fvwm1).
if [ -n "$(type -path fvwm)" ]; then
    # if this works, we stop here
    exec fvwm
fi

# No other window or session manager found.
# Use twm as a last resort.
xterm &
exec twm
