#
# ident "@(#)Xsetup.sh	1.42 04/06/11 SMI"
#
# Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
#
# WARNING: This configuration is automatically generated by
#	   scripts.
#
#	   Changes made here may be lost.
#
#
# Product:	Sun Ray server	2.0
#
# Purpose:	Manage pre-dtgreet activities specific to Sun Ray
#
#

DPY=${DISPLAY#*:}
DPY=${DPY%.*}

#
# Clean-up any left-overs
#
if [ "$DPY" != "" ]
then
	export LC_ALL=C

	IDLEDIR=/var/opt/SUNWut/idle
	DPYDIR=/var/opt/SUNWut/displays

	UTOPT=`/bin/pkginfo -r SUNWuto`/SUNWut
	UTSWITCH="$UTOPT/bin/utswitch"
	UTSELECT="$UTOPT/bin/utselect"
	UTDETACH="$UTOPT/bin/utdetach"
	UTACTION="$UTOPT/lib/utaction"
	XCLEANUP="$UTOPT/lib/Xcleanup"
	UTDMSESSION="$UTOPT/lib/utdmsession"
	DETACHABLE_IDLE_TIMEOUT="60"
	DISC_IDLE_TIMEOUT="900"

	TOKEN_SET=`sed -n '/^TOKEN_SET=/s///p' $DPYDIR/$DPY`
	case "$TOKEN_SET" in
	    *mobile.*|*escape.*|*asc.*)
		UTISDETACHABLE=true ;;
	    *)
		UTISDETACHABLE=false ;;
	esac
	# If we're an idle session then try to figure out whether Xsetup
	# is being run again because of a new language selection.  If so
	# then we want to stay at the dtgreet screen on this server -- we
	# don't want to take the opportunity to load-balance or revert to
	# an already-logged-in session on some other host (via 'utswitch'),
	# we don't want to jump back to the non-smartcard login GUI (via
	# 'utdetach'), and we don't want to pop up a utselect-before-login
	# GUI (via 'utselect').  Base this decision on the content of the
	# LANG line in the idle file.  If the idle file contains a LANG
	# entry that's different to our current $LANG then this is a
	# language selection change and we skip the utswitch, utdetach and
	# utselect activities.
	#
	# If the idle info file contains no LANG entry then we're a fresh
	# idle session, and if it contains a LANG entry that matches our
	# current $LANG then we're here because the user chose "Reset Login
	# Screen" from the dtgreet menu.  In either of those cases we do
	# want to detach, rebalance and/or reselect.
	#
	# Unfortunately there's a case where we'll rebalance when we really
	# shouldn't (choose the current language again) and another case
	# where we won't rebalance when we really should (use 'reset' after
	# selecting a non-default language on a machine where dtlogin has
	# been explicitly configured with a default language and it'll just
	# revert your language selection to that default.  Use 'reset' again
	# and you'll get rebalanced that time.)  This happens because we
	# have no way of knowing why we're being re-run and are forced to
	# guess.  Still, it's better than the old method which _always_
	# rebalanced every tme Xsetup ran.

	REBALANCE=1		# assume that we will want to rebalance

	if [ -f $IDLEDIR/$DPY.info ]
	then
		OLDLANG=`sed -n 's/^LANG=//p' $IDLEDIR/$DPY.info`
		if [ -n "$OLDLANG" ]
		then
			# We've run at least once before and OLDLANG contains
			# the language selection we used the last time around.
			#
			if [ "$LANG" != "$OLDLANG" ]
			then
				# This time we're using a different language,
				# so don't detach or rebalance.
				#
				REBALANCE=0
			fi
		fi

		if [ $REBALANCE -ne 0 ] ; then

			# This is a fresh or deliberately-reset session.
			# Try to find a logged in session on another server
			# by running utswitch -t. Idle sessions are biased
			# against by touching the modified time on the
			# display file back a year here and in Xreset.

			if [ -f $DPYDIR/$DPY ]
			then
				let y=`date +%Y`-1
				touch -t $y`date +%m%d%H""%M` $DPYDIR/$DPY
			else
				MSG="error: no Sun Ray display file for display $DPY"
				echo $MSG 2>&1
				logger -t Xsetup -ip user.error "$MSG"
			fi

			# If this session is using a mobile, escape or asc
			# (SC) token, kick back out to the login GUI

			if $UTISDETACHABLE
			then
				$UTDETACH
			else
				$UTSWITCH -t
			fi
		fi

		/bin/rm -f $IDLEDIR/$DPY.info
	fi

	# If a reaper already exists for this session we need to kill it
	# before we start another one

        if [ -a $IDLEDIR/$DPY.pid ]
        then
             UTPID=$(/bin/cat $IDLEDIR/$DPY.pid)
             /bin/rm -f $IDLEDIR/$DPY.pid
             kill $UTPID
	     if [ -f $DPYDIR/$DPY ]
	     then
             	touch $DPYDIR/$DPY
	     else
		MSG="error2: no Sun Ray display file for display $DPY"
		echo $MSG 2>&1
		logger -t Xsetup -ip user.error "$MSG"
	     fi
        fi

	#
	# If lock files already exists for this session we need to
	# remove them before we create another one
	#
	if [ -a $IDLEDIR/$DPY.lock.* ]
	then
	    /bin/rm -f $IDLEDIR/$DPY.lock.*
	fi	

	# Figure out whether this is a local session by seeing whether the
	# local hostname matches the hostname in the Sun Ray session
	# ID string.  The session ID string is in the _SUN_SUNRAY_SESSION
	# property of the root window

	XDIR=${XDIR:-/usr/openwin/bin}

	DEVICE_SESSION=`$XDIR/xprop -root _SUN_SUNRAY_SESSION \
				2> /dev/null | sed -e 's/.*"\(.*\)"/\1/'`
	
	if [ -n "$DEVICE_SESSION" ]
	then
		LOCAL_HOST=`echo $DEVICE_SESSION | \
			/bin/grep '^'$(/bin/uname -n)':' 2> /dev/null`
	fi

	if [ -n "$LOCAL_HOST" ]
	then
		#
		# Disable the DPMS extension for Sun Ray
		#
		[ -x $XDIR/xset ] && $XDIR/xset -dpms 2> /dev/null

		TOKEN=$(/bin/grep '^TOKEN=' $DPYDIR/$DPY)
		TOKEN=${TOKEN#*=}
		if [ "$TOKEN" != "" ]
		then
			SUN_SUNRAY_TOKEN="$TOKEN"
			export SUN_SUNRAY_TOKEN
			CORONA_TOKEN="$TOKEN"
			export CORONA_TOKEN

			if [ -x $UTACTION -a -d $IDLEDIR/. ]
			then
                            # Get the detached-greeter reaper timeout parameters.
                            # It's way too dangerous to just source the conf file
                            # so this 'sed' script reads the file, matches the
                            # lines that look like assignments and emits the RHS
                            # of the final assignment.
                            #
                            REAPERCONF=/etc/opt/SUNWut/reaper.conf
                            if [ -f $REAPERCONF ] ; then
                                REAPERCONF_TIMEOUT=`
                                    sed -n '/^REAPER_TIMEOUT[ 	]*=/ {
					h
					}
					$ {
					x
					s/^REAPER_TIMEOUT[ 	]*=[ 	]*//p
					}' $REAPERCONF`
				case "$REAPERCONF_TIMEOUT" in
				    [0-9]*)
					if [ 9999999 -ge "$REAPERCONF_TIMEOUT" ] ; then
					    DISC_IDLE_TIMEOUT="$REAPERCONF_TIMEOUT"
					fi
					;;
				    
				    "never")
					DISC_IDLE_TIMEOUT="$REAPERCONF_TIMEOUT"
					;;
				esac
			    fi

			    if [ "never" != "$DISC_IDLE_TIMEOUT" ] ; then
				
				# Set the session to be terminated if it
				# remains detached at the login greeter for
				# the timeout duration
				#
				$UTACTION -i -t ${DISC_IDLE_TIMEOUT} \
				    -d "$XCLEANUP" < /dev/null &
				
				# Remember the reaper's pid so it can be killed
				# by Xstartup following a successful login
				#
				UTPID=$!
				echo $UTPID > $IDLEDIR/$DPY.pid
			    fi
			fi

			if $UTISDETACHABLE
                        then
                                #
                                # Set the session to be reaped if it's
                                # a idle escape session, waiting in the
                                # dtgreet login screen for 60 seconds
                                #
				( touch $IDLEDIR/$DPY.lock.$$;
                                  sleep ${DETACHABLE_IDLE_TIMEOUT};
                                  if [[ -f $IDLEDIR/$DPY.info && -f $IDLEDIR/$DPY.lock.$$ ]];
                                  then
                                          $XCLEANUP;
                                  fi
				  /bin/rm -f $IDLEDIR/$DPY.lock.$$; 
                                ) &
                        fi

			echo TOKEN=$SUN_SUNRAY_TOKEN > $IDLEDIR/$DPY.info
			echo "LANG=$LANG" >> $IDLEDIR/$DPY.info

			#
			# Store the session info for session management
			#
			SESSION_PROC=/tmp/SUNWut/session_proc/$DPY
			echo XID=$DPY > $SESSION_PROC
			echo pid=$PPID >> $SESSION_PROC
			/bin/ps -p $PPID -o comm | sed -n '$s/^/program=/p' \
							>> $SESSION_PROC
			/bin/chmod 644 $SESSION_PROC

			if [ $REBALANCE -ne 0 ] ; then

				# If configured, bring up a utselect dialogue
				# before presenting 'dtgreet'

				AUTHPROPS=/etc/opt/SUNWut/auth.props
				if [ "`sed -n '/^selectAtLogin=/s///p' \
							$AUTHPROPS`" = "true" ]
				then
					$UTSELECT -L
				fi
			fi

			# Tell the Device Manager about this session.
			# Setup the UID for the session_proc file as well.
			$UTDMSESSION -c $DPY

		fi
	fi
fi

