#!/bin/sh 

# Top level Xsession file for all users
#
# $Id: Xsession,v 1.8 2005-12-22 14:54:17 rbasch Exp $

trap "kdestroy; unlog; fsid -u -f $USER; exit 0" 1 2 15

initlib="/usr/lib/init"
defsession=$1
defsessioncmd="$2"

if [ -f .noignore ]; then
	defsession=1;
fi

if [ "`basename $SHELL`" = bash ]; then
	default_session=$initlib/xsession.bash
else
	default_session=$initlib/xsession
fi

case $defsession in
1|4|7)
	if [ -f .xsession ]; then
		session=.xsession
	else
		session=$default_session
	fi
	case $defsession in
	7)
		session="$session dash"
		;;
	esac
	;;
2)
	session="$default_session -nocalls"
	;;
3)
	session="xterm -ls -geometry 80x24+280+220 -display :0.0"
	;;
5)
	session="$defsessioncmd"
	;;
esac

# Set the tty characteristics of the parent (console window)
# Redirect stdin since some versions of stty use it instead of stdout.
if [ "${hosttype}" = "decmips" ]; then
  stty dec 0<&1 2>/dev/null
else
  stty sane 0<&1 2>/dev/null
fi

# Make random Athena modifications to keymap if required.

test -f /usr/lib/X11/keymaps/AthenaMods && \
  xmodmap /usr/lib/X11/keymaps/AthenaMods

# Set X screensaver to noblanking to avoid power saving.
xset s noblank

# Only attempt using bugme if packs are actually available.
if [ -x /usr/bin/bugme ]; then
	/usr/bin/bugme $session
else
	$session
fi

status=$?
if [ $status != 0 ]; then
		echo ERROR:
		echo "Xsession returned non-zero status."
		echo ""
		echo 'Select the login option to "Ignore your customizations"'
		echo 'to login and repair your dot files.'
		kdestroy			# destroy tickets.
		sleep 10
		exit 1
fi

# Kill mozilla before destroying tokens so that it removes its lock file.
$initlib/xkill-mozilla

# Make gconfd flush settings to disk before destroying credentials.
gconftool-2 --shutdown

# Kill xscreensaver, to keep it from trying to restart when we send
# it SIGHUP below.
xscreensaver-command -exit > /dev/null 2>&1

# Cannot detach homedir here, since this script is executed in the users
# homedir.

kdestroy				# destroy tickets.
cd /tmp
unlog
fsid -u -f $USER > /dev/null 2>&1

sleep 3					# let them see any messages (quickly)
exit 0
