#!/bin/sh
#Xsession.kdm version 1.1. 14.6.97, mb
#Xsession for kdm, which makes use of different sessions,
#the session which is used last is saved for each user,
#and one special session allows to start this saved session.

# xdm (and kdm) don't provide checking for /etc/nologin, as 'login' does,
# so we do it here. you need Xmessage for this to work, or some
# other messaging mechanism if you want to show the contents 
#of /etc/nologin to the user.

# check for /etc/nologin, but only if we are not root

if [ "$USER" != "root" ]; then
    if [ -e /etc/nologin ]; then
    	exec xmessage -center `cat /etc/nologin`
    fi
fi

#this is standard Xsession code, as it comes with xdm
# redirect errors to a file in user's home directory if we can
for errfile in "$HOME/.xsession-errors" "/tmp/xses-$USER"
do
        if ( cp /dev/null "$errfile" 2> /dev/null )
        then
                chmod 600 "$errfile"
		xconsole -file $errfile -geometry 273x133+112+0 -daemon -notify -verbose -fn fixed -exitOnFail -tail &# /var/X11/lib/X11/xdm/GiveConsole &
                exec > "$errfile" 2>&1 
                break
        fi
done
# exec > /dev/console 2>&1

# here comes the fun:
# first we check if we have exactly one argument:
case $# in
1)
     # if it's true, we check for the session the user chose:
     case $1 in
     last)
           #if he/she choose a session called 'last' this means, our user
           #want's the session he had at last login.
           #since 'last' is normaly the default session, we check
           #if the user has a '.last.xsession' in his/her home.
           if [ -e "$HOME/.last.xsession" ]
           then
              exec $HOME/.last.xsession
           else
              # if not, we execute a default session.
              exec /etc/X11/xdm/Xsession.normal
           fi
     ;;
     failsafe)
           #this is a fallback, just in case none of the other sessions
           #do work.
           #please be sure, that 'Xsession.failsafe' really is 'failsafe'.
           exec /etc/X11/xdm/Xsession.failsafe
     ;;
     *)
           #for any other session besides 'last', we create a file
           #'Xsession.<session-name>'
           #we link it to the users home, so we can remember it
           #the next time, and then we execute.
           ln -sf /etc/X11/xdm/Xsession.$1 $HOME/.last.xsession
           exec /etc/X11/xdm/Xsession.$1
     ;;
     esac
esac

#if something went wrong, with choosing the right session,
#we execute Xsession.failsafe anyway.

exec /etc/X11/xdm/Xsession.failsafe
