#!/bin/bash
#                 by Jun Nishii <jun@vinelinux.org>
#                 by Ryoichi INAGAKI <ryo1@bc.wakwak.com>
#            Time-stamp: <2010-02-27 21:41:08 inagaki>

msgfile="$HOME/.xwm.msgs"
startWM(){
    WM=$1
    shift
    CMD="$(type -p $1)"
    shift
    if [ -n "$CMD" ]; then
        echo Starting $WM >>$msgfile
	env > "$HOME"/Xrootenv.0
	exec ${XSESSION_WRAPPER} $CMD $@ >>$msgfile 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 
	gnome*|Gnome*|GNOME*)
	    startWM Gnome gnome-session
	    ;;
	startkde*|kde*|KDE*)
	    startWM KDE startkde
	    ;;
	startxfce4*|xfce*|XFce*|XFCE*)
	    startWM XFce startxfce4
	    ;;
	startlxde*|lxde*|LXDE*)
	    startWM lxde startlxde
	    ;;
	startmate*|mate*|MATE*)
	    startWM Mate mate-session
	    ;;
	wmaker*|WindowMaker*)
	    startWM WindowMaker RunWM --WindowMaker
	    ;;
 	afterstep*|Afterstep*|AfterStep*)
	    startWM AfterStep RunWM --AfterStep
 	    ;;
 	blackbox*|BlackBox*|BLACKBOX*)
	    startWM BlackBox blackbox
 	    ;;
 	fluxbox*|FluxBox*|FLUXBOX*)
	    startWM FluxBox startfluxbox
 	    ;;
 	icewm*|IceWm*|ICEWM*)
	    startWM icewm icewm
 	    ;;
        enlightenment*|Enlightenment*|ENLIGHT*)
	    startWM Enlightenment enlightenment
            ;;
	sawfish*)
	    startWM sawfish sawfish
	    ;;
	sawmill*)
	    startWM sawmill sawmill
	    ;;
        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
            ;;
	default*)
	    startWM Gnome gnome-session
	    ;;
    esac
fi

GSESSION="$(type -p gnome-session)"
STARTKDE="$(type -p startkde)"

# check to see if the user has a preferred desktop
PREFERRED=
if [ -f /etc/sysconfig/desktop ]; then
    . /etc/sysconfig/desktop
    if [ "$DESKTOP" = "GNOME" ]; then
	PREFERRED="$GSESSION"
    elif [ "$DESKTOP" = "KDE" ]; then
	PREFERRED="$STARTKDE"
    elif [ -n "`grep -i AnotherLevel /etc/sysconfig/desktop`" ]; then
	PREFERRED=AnotherLevel
    elif [ -x /usr/bin/gnome-session ]; then
        PREFERRED=gnome-session
    fi
fi

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

# We should also support /etc/X11/xinit/Xclients.d scripts
XCLIENTS_D=/etc/X11/xinit/Xclients.d
if [ "$#" -eq 1 ] && [ -x "$XCLIENTS_D/Xclients.$1.sh" ]; then
    exec -l $SHELL -c "$SSH_AGENT $XCLIENTS_D/Xclients.$1.sh"
fi

# Failsafe.

# these files are left sitting around by TheNextLevel.
rm -f $HOME/Xrootenv.0

# Argh! Nothing good is isntalled. Fall back to fvwm2 (win95-style) or twm
{
    # fall back to failsafe settings
    [ -x /usr/bin/xsetroot ] && /usr/bin/xsetroot -solid '#222E45'

    if [ -x /usr/bin/xclock ] ; then
	/usr/bin/xclock -geometry 100x100-5+5 &
    fi
    if [ -x /usr/bin/kterm ] ; then
	/usr/bin/kterm -geometry 80x50-50+150 &
    fi
    if [ -x /usr/bin/firefox -a -f /usr/share/doc/HTML/index.html ]; then
	/usr/bin/firefox /usr/share/doc/HTML/index.html &
    fi
    startWM fvwm2 fvwm2 || startWM twm twm
}
