#!/bin/sh

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

# Desktop addition.  If there is a Xsession.dt file, execute that instead
if /etc/chkconfig desktop ;
then
	if [ -x $0.dt -a \! -f $HOME/.disableDesktop ]
	then
		exec $0.dt $*
	fi
fi

# # Uncomment this for X11R5 redirection of Xsession errors.
#
# # 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"
#                 exec > "$errfile" 2>&1
#                 break
#         fi
# done

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 $xsession
	else
		exec /bin/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
	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

screens=`/usr/bin/X11/xlistscrns`

# Gives anyone on any host access to this display
#/usr/bin/X11/xhost +

#
# Note: Output from $HOME/.sgisession, 4Dwm, workspace and toolchest
# is redirected to /dev/console. All other output is written to 
# /usr/adm/SYSLOG by default (or where the X11R5 redirection makes it go
# if the above X11R5 redirection lines above are uncommented).
#

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

/usr/bin/X11/4Dwm >/dev/console 2>&1 &
/usr/bin/X11/wait4wm

/usr/sbin/startconsole -iconic

if [ -x $sgisession ]; then
	$sgisession >/dev/console 2>&1 &
elif [ -r $sgisession ]; then
	/bin/sh $sgisession >/dev/console 2>&1 &
fi

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

for screen in $screens
  do 
  DISPLAY=$screen /usr/bin/X11/toolchest -name ToolChest /usr/lib/X11/nodesktop.chestrc >/dev/console 2>&1 &
  done

exec /usr/bin/X11/reaper
