#!/bin/sh
#
# $Revision: 1.3 $

# Based on: $XConsortium: Xsession,v 1.7 92/08/06 11:08:14 gildea Exp $

SU=/sbin/su
SH=/sbin/sh
CONSOLE=/dev/null
DISPLAY=:0
export DISPLAY

USER_NAME=`cat /usr/CMW/CMWuser-name`
USER_CAP=`cat /usr/CMW/CMWuser-cap`
#USER_MAC=`cat /usr/CMW/CMWuser-mac`

case $# in
1)
	case $1 in
	failsafe)
	    dim="`xdpyinfo | awk ' /dimensions/ {print $2}' | grep '1024x768'`"
	    if [ "$?" = 0 ]; then
		geom="80x40+190+102"
		font="-sgi-terminal-*-*-*--12-*"
	    else
		geom="80x40+150+172"
		font="-sgi-terminal-*-*-*--17-*"
	    fi
	    exec xwsh -geom $geom -fn $font -xrm "XWsh.autoFork: off" -nomenu
	    ;;
	esac
esac

xsession=$HOME/.xsession
resources=$HOME/.Xresources
sgisession=$HOME/.sgisession
autoworkspace=$HOME/.workspace/autostart
userenv=/usr/bin/X11/userenv
lang=$HOME/.lang

if [ -r $xsession -a "$0" != $xsession ]; then
	if [ -x $xsession ]; then
		exec $SU $USER_NAME -C $USER_CAP -c $xsession
	else
		exec $SU $USER_NAME -C $USER_CAP -c "$SH $xsession"
	fi
fi

# Set the users language
if [ -r $lang -a -f $lang ] ; then LANG=`cat $lang`; export LANG; fi

if [ -r $resources ]; then
	$SU $USER_NAME -C $USER_CAP -c "xrdb -load -quiet $resources"
fi

# This is the pre-IRIX 5.3 setting of XUSERFILESEARCHPATH
# XUSERFILESEARCHPATH="$HOME/%N:`/usr/bin/X11/appdefpath`"
# export XUSERFILESEARCHPATH
# In IRIX 5.3, we don't set XUSERFILESEARCHPATH anymore.

if [ -x $userenv ]; then
	#
	# Find user's environment as set up during a login
	#  shell, and add that to current environment.
	#
	eval `$userenv`
fi

#
# Restricts this display to this host.
/usr/bin/X11/xhost -

#
# Do language dependent stuff
intlenv=`gettxt uxsgidesktop:452 ':'`
eval ${intlenv}

/usr/bin/X11/4Dwm > $CONSOLE 2>&1 &

#
# The Access controlled 4Dwm can take a little longer than
# the vanilla version.
#
/usr/bin/X11/wait4wm
/usr/bin/X11/wait4wm
/usr/bin/X11/wait4wm

$SU $USER_NAME -C $USER_CAP -c "/usr/sbin/startconsole -iconic" > $CONSOLE 2>&1 &

if [ -x $sgisession ]; then
	$SU $USER_NAME -C $USER_CAP -c $sgisession > $CONSOLE 2>&1 &
elif [ -r $sgisession ]; then
	$SU $USER_NAME -C $USER_CAP -c "$SH $sgisession" > $CONSOLE 2>&1 &
fi

if [ -r $autoworkspace ]; then
	if [ "`cat $autoworkspace`" = "on" ]; then
		/usr/sbin/workspace > $CONSOLE 2>&1 &
	fi
fi

$SU $USER_NAME -C $USER_CAP -c "/usr/bin/X11/toolchest -name ToolChest /usr/lib/X11/nodesktop.chestrc" > $CONSOLE 2>&1 &

#
# Set the session property
#
/usr/bin/X11/reaper
#
# Wait until the session property goes away
#
$SU $USER_NAME -C $USER_CAP -c "exec /usr/bin/X11/xprop -root -exists _SGI_SESSION_PROPERTY" > $CONSOLE 2>&1
