#!/bin/sh
#
# Xsession:	Setup the xsession and start the xsession
#		script of the user.
#		
# Copyright (c) 1998-2000 SuSE GmbH Nuernberg, Germany.
# please send bugfixes or comments to feedback@suse.de.
#
# Author: Werner Fink,   <werner@suse.de>
#

# Stop Models@Home screen saver
/usr/cluster/scrsavco -exit

#
# What we do if we are signaled or do not leave this
# script with an appropriate exec call.
#
: ${failsafe="xterm -ls -T Failsave -geometry 80x24-0-0"}
readonly failsafe
trap "exec $failsafe" EXIT SIGHUP SIGINT SIGPIPE SIGTERM SIGIO

#
# Save our arguments, maybe some of users call
# `set' in their profile files.
#
argv="$@"
readonly argv

#
# Disable graphical login if normal login is disabled
#
login=false
while read sh ; do
    if test "$sh" = "$SHELL" ; then
	login=true
	break
    fi
done < /etc/shells

if test "$login" != "true" -o "$SHELL" = "/bin/false" ; then
    trap "exec xmessage -timeout 10 -button okay:1 -center	\
	\"${0##*/}: Login for $USER is disabled.\""		\
	EXIT SIGHUP SIGINT SIGPIPE SIGTERM SIGIO
    exit 1
fi
unset sh login

#
# Some bash (1 and 2) settings to avoid trouble with
# personal profile or bashrc files and failed exec calls.
#
test -n "$BASH" && set +o posix
no_exit_on_failed_exec=1
type shopt &> /dev/null && shopt -s execfail
set +e     &> /dev/null 

#
# Redirect errors to the standard user log files.
#
: ${TMPDIR=/tmp}
for errfile in	"$HOME/.xsession-errors" \
		"$TMPDIR/xses-$USER"     \
		"/tmp/xses-$USER"
do
    #
    # Avoid bad symbolic links
    #
    case "$errfile" in
	/tmp/*|$TMPDIR/*) rm -f $errfile ;;
    esac
    if (> "$errfile") 2> /dev/null ; then
	chmod 0600 "$errfile"
	exec > "$errfile" 2>&1
	break
    fi
done

#
# Danger: What will the user do with some of the used variables?
# I do not like that ... but even if we source user's files
# we want to find our files and use correct variables.
#
      __HOME="$HOME"         # Users HOME determined by xdm
   __DISPLAY="$DISPLAY"      # The DISPLAY set by xdm for this session
__XAUTHORITY="$XAUTHORITY"   # Should be empty in the most cases
readonly __HOME __DISPLAY __XAUTHORITY

#
# Read the default system settings.
#
test -r /etc/profile     && { . /etc/profile     > /dev/null 2>&1 ; }

#
# Some system defaults
#
: ${XLIBDIR=/usr/X11R6/lib/X11}
: ${OPENWINHOME=/usr/openwin}
readonly XLIBDIR

#
test -r ${HOME}/.bashrc  && { . ${HOME}/.bashrc  > /dev/null 2>&1 ; }
test -r ${HOME}/.profile && { . ${HOME}/.profile > /dev/null 2>&1 ; }
#
# Restore
#
set +u > /dev/null 2>&1
      HOME="$__HOME"
   DISPLAY="$__DISPLAY"
XAUTHORITY="$__XAUTHORITY"
      TERM=xterm
#
export OPENWINHOME TERM DISPLAY HOME
test -z "$XAUTHORITY" && unset XAUTHORITY || export XAUTHORITY

#
# Check path (paranoid)
#
type -p xdm > /dev/null 2>&1 || PATH=$PATH:/usr/X11R6/bin

#
# Handle arguments given by xdm.
#
set -- $argv
if test $# -ge 1 ; then
    case "$1" in
	failsafe)
	    shift
	    exec $failsafe
	    ;;
	*)
	    #
	    # KDM may overwrites this
	    #
	    WINDOWMANAGER=$1 ; export WINDOWMANAGER
	    if test $# -ge 2 && test -n "$2" ; then
		LANG=$2 ; export LANG
	    fi
	    shift $#
	    ;;
    esac
fi

#
# Reset default background and cursor.
#
xsetroot -def
xsetroot -cursor_name top_left_arrow

#
# Some common user and system files,
# do not use `: ${xx=yy}' here.
#
  xdefaults=$HOME/.Xdefaults
 xresources=$HOME/.Xresources
    xmodmap=$HOME/.Xmodmap
    session=$HOME/.xsession
    xinitrc=$HOME/.xinitrc
    openwin=$OPENWINHOME/bin/openwin
    errinit=$XLIBDIR/xinit/xinitrc
    errsess=$XLIBDIR/xdm/sys.xsession

#
# Handle the users Xmodmap and Xresources
#
test -r "$xmodmap"    && xmodmap "$xmodmap"
if   test -r "$xdefaults"  ; then
     xrdb -I$HOME -load -retain "$xdefaults"
     test -r "$xresources" && xrdb -I$HOME -merge "$xresources"
elif test -r "$xresources" ; then
     xrdb -I$HOME -load -retain "$xresources"
fi

# User session
# If the user doesn't have their own xsession, then run
# xinitrc if they exist

XSESSION_IS_UP=yes
export XSESSION_IS_UP

if   test -f $session ; then
     test -x $session && exec $session
     exec /bin/sh $session
elif test -f $xinitrc ; then
     test -x $xinitrc && exec $xinitrc
     exec /bin/sh $xinitrc
elif test -f $errsess; then
     test -x $errsess && exec $errsess
     exec /bin/sh $errsess
elif test -f $errinit ; then
     test -x $errinit && exec $errinit
     exec /bin/sh $errinit
elif test -f $openwin ; then
     test -x $openwin && exec $openwin
     exec /bin/sh $openwin
else
     test -n "$WINDOWMANAGER" && exec $WINDOWMANAGER
fi

#
# Call failsafe
#
exit 1
