#!/bin/sh -
# configs/Xclients.  Generated from Xclients.in by configure.
#
# ~/.Xclients * WDM/FreeBSD * modified by martinko [23-sep-2006]

########################################################################
# -*- 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
}

# output startup message
Starting()
{
	echo "Starting $1 on `date`" >| "$xwm_msgs"
}


wm_style="$HOME/.wm_style"
xwm_msgs="$HOME/.xwm.msgs"
xrootenv="$HOME/Xrootenv.0"


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

# check for and set X-resources

sysresources="/usr/local/lib/X11/xinit/.Xresources"
sysmodmap="/usr/local/lib/X11/xinit/.Xmodmap"
userresources="$HOME/.Xresources"
usermodmap="$HOME/.Xmodmap"

# merge in defaults and keymaps

if [ -f "$sysresources" ]; then
	xrdb -merge "$sysresources"
fi

if [ -f "$sysmodmap" ]; then
	xmodmap "$sysmodmap"
fi

if [ -f "$userresources" ]; then
	xrdb -merge "$userresources"
fi

if [ -f "$usermodmap" ]; then
	xmodmap "$usermodmap"
fi


# First thing - check the user preferences
if [ -f "$wm_style" ]
then
	WMSTYLE="`cat "$wm_style"`"
	case "$WMSTYLE" in

	wmaker*|WMaker*|WindowMaker*)
		# startup Window Maker
		WMAKER_PATH=wmaker
		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
			Starting "Window Maker"
			#env >| "$xrootenv"
			exec $WMAKER_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	afterstep*|Afterstep*|AfterStep*)
		# we have to start up AfterStep
		AFTERSTEP_PATH=afterstep
		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 >| "$xrootenv"
			# if this works, we stop here
			eval "exec $AFTERSTEP_PATH" >| "$HOME"/.AfterStep-errors 2>&1
		fi
		;;

	blackbox*|Blackbox*|BlackBox*)
		# startup Blackbox
		BLACKBOX_PATH=blackbox
		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
			Starting "Blackbox"
			#env >| "$xrootenv"
			exec $BLACKBOX_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	fluxbox*|Fluxbox*|FluxBox*)
		# startup Fluxbox
		FLUXBOX_PATH=startfluxbox
		if ! test -x $FLUXBOX_PATH ; then
			FindInPath $FLUXBOX_PATH
			if test -n "$result" -a -x "$result"; then
				FLUXBOX_PATH="$result"
			fi
		fi
		if [ -x $FLUXBOX_PATH ] ; then
			Starting "Fluxbox"
			#env >| "$xrootenv"
			exec $FLUXBOX_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	fvwm*|FVWM*)
		# startup FVWM
		FVWM_PATH=fvwm
		if ! test -x $FVWM_PATH ; then
			FindInPath $FVWM_PATH
			if test -n "$result" -a -x "$result"; then
				FVWM_PATH="$result"
			fi
		fi
		if test -x $FVWM_PATH ; then
			Starting "FVWM"
			#env >| "$xrootenv"
			exec $FVWM_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	fvwm2*|FVWM2*)
		# startup FVWM2
		FVWM2_PATH=fvwm2
		if ! test -x $FVWM2_PATH ; then
			FindInPath $FVWM2_PATH
			if test -n "$result" -a -x "$result"; then
				FVWM2_PATH="$result"
			fi
		fi
		if test -x $FVWM2_PATH ; then
			Starting "FVWM2"
			#env >| "$xrootenv"
			exec $FVWM2_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	fvwm95*|FVWM95*)
		# startup FVWM95
		FVWM95_PATH=fvwm95
		if ! test -x $FVWM95_PATH ; then
			FindInPath $FVWM95_PATH
			if test -n "$result" -a -x "$result"; then
				FVWM95_PATH="$result"
			fi
		fi
		if test -x $FVWM95_PATH ; then
			Starting "FVWM95"
			#env >| "$xrootenv"
			exec $FVWM95_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	icewm*|IceWm*|IceWM*)
		# startup IceWM
		ICEWM_PATH=icewm
		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
			Starting "IceWM"
			#env >| "$xrootenv"
			exec $ICEWM_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	twm*|TWM*)
		# startup TWM
		TWM_PATH=twm
		if ! test -x $TWM_PATH ; then
			FindInPath $TWM_PATH
			if test -n "$result" -a -x "$result"; then
				TWM_PATH="$result"
			fi
		fi
		if test -x $TWM_PATH ; then
			Starting "TWM"
			#env >| "$xrootenv"
			exec $TWM_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	xfce*|Xfce*|XFce*|XFCE*)
		# startup Xfce
		XFCE_PATH=startxfce4
		if ! test -x $XFCE_PATH ; then
			FindInPath $XFCE_PATH
			if test -n "$result" -a -x "$result"; then
				XFCE_PATH="$result"
			fi
		fi
		if test -x $XFCE_PATH ; then
			Starting "Xfce"
			#env >| "$xrootenv"
			exec $XFCE_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	gnome*|Gnome*)
		# startup Gnome
		GNOME_PATH=gnome-session
		if ! test -x $GNOME_PATH ; then
			FindInPath $GNOME_PATH
			if test -n "$result" -a -x "$result"; then
				GNOME_PATH="$result"
			fi
		fi
		if test -x $GNOME_PATH ; then
			Starting "Gnome"
			#env >| "$xrootenv"
			exec $GNOME_PATH >> "$xwm_msgs" 2>&1
		fi
		;;

	startkde*|kde*|KDE*)
		# startup KDE
		STARTKDE_PATH=startkde
		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
			Starting "KDE"
			#env >| "$xrootenv"
			exec $STARTKDE_PATH -console >> "$xwm_msgs" 2>&1
		fi
		;;

	xsession*|Xsession*)
		# startup xsession
		XSESSION_PATH=xsession
		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
			Starting "xsession"
			#env >| "$xrootenv"
			exec $XSESSION_PATH -console >> "$xwm_msgs" 2>&1
		fi
		;;

	# last, a general purpose entry:
	*)
		# try and startup a window manager
		WINMGR_NAME="$WMSTYLE"
		WINMGR_PATH="$WMSTYLE"
		if ! test -x $WINMGR_PATH ; then
			FindInPath $WINMGR_PATH
			if test -n "$result" -a -x "$result"; then
				WINMGR_PATH="$result"
			fi
		fi
		if test -x "$WINMGR_PATH" ; then
	        	Starting "$WINMGR_NAME"
			#env >| "$xrootenv"
			exec $WINMGR_PATH >> "$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 >| "$xrootenv"
        # 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
