#!/bin/bash
#                 by Jun Nishii <jun@vinelinux.org>
#            Time-stamp: <2000-10-10 14:06:46 vine>

startWM(){
    WM=$1
    shift
    CMD="`which $1 2>/dev/null`"
    shift
    if [ -n "$CMD" ]; then
        echo Starting $WM >>$HOME/.xwm.msgs
	env > "$HOME"/Xrootenv.0
	exec $CMD $@ >>$HOME/.xwm.msgs 2>&1
	return 0
    fi
    return -1
}

# First thing - check the user preferences
if [ -f $HOME/.wm_style ] ; then
    WMSTYLE=`cat $HOME/.wm_style`
    case "$WMSTYLE" in 
	wmaker*|WindowMaker*)
	    startWM WindowMaker RunWM --WindowMaker
	    ;;
 	afterstep*|Afterstep*|AfterStep*)
	    startWM AfterStep RunWM --AfterStep
 	    ;;
 	blackbox*|BlackBox*|BLACKBOX*)
	    startWM BlackBox blackbox
 	    ;;
 	icewm*|IceWm*|ICEWM*)
	    startWM icewm icewm
 	    ;;
	gnome*|Gnome*|GNOME*)
	    startWM Gnome gnome-session
	    ;;
	startkde*|kde*|KDE*)
	    startWM KDE startkde
	    ;;
        enlightenment*|Enlightenment*|ENLIGHT*)
	    startWM Enlightenment enlightenment
            ;;
	sawmill*)
	    startWM sawmill sawmill
	    ;;
	sawfish*)
	    startWM sawfish sawfish
	    ;;
        qvwm*|QVWM*)
	    startWM qvwm qvwm
	    ;;
	fvwm2*|FVWM2*)
	    startWM fvwm2 fvwm2
	    ;;
	twm*|TWM*)
	    startWM twm twm &&\
                 exec kterm -C -title Login -n Login -sb -geometry 80x25-0-0 -ls
            ;;
    esac
fi

# check to see if the user has a preferred desktop
PREFERRED=
if [ -f /etc/sysconfig/desktop ]; then
    if [ -n "`grep -i GNOME /etc/sysconfig/desktop`" ]; then
	PREFERRED=gnome-session
    elif [ -n "`grep -i KDE /etc/sysconfig/desktop`" ]; then
	PREFERRED=startkde
    elif [ -n "`grep -i AnotherLevel /etc/sysconfig/desktop`" ]; then
	PREFERRED=AnotherLevel
    fi
fi

if [ -n "$PREFERRED" -a "$PREFERRED" != "AnotherLevel" ] && \
	which $PREFERRED >/dev/null 2>&1; then
    PREFERRED=`which $PREFERRED`
    exec $PREFERRED
fi

# Argh! Nothing good is isntalled. Fall back to fvwm2 (win95-style) or twm
/usr/X11R6/bin/RunWM --WindowMaker || {
    xclock -geometry 100x100-5+5 &
    kterm -geometry 80x50-50+150 &
    if [ -f /usr/bin/netscape -a -f /usr/doc/HTML/index.html ]; then
	netscape /usr/doc/HTML/index.html &
    fi
    startWM fvwm2 fvwm2 || startWM twm twm
}
