#
# ident "@(#)Xstartup.sh	1.16 04/06/11 SMI"
#
# Copyright 1999-2002,2004 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:	Stop inactive session reaper and inform Device Manager
#		about this session's change in status.
#
#

export LC_ALL=C

PKG_OPT_INFO="$(/bin/pkginfo -r SUNWuto)"
DEVICE_PKG_LOC="$PKG_OPT_INFO/SUNWut"

#DEVICE_BIN=$DEVICE_PKG_LOC/bin
DEVICE_LIB=$DEVICE_PKG_LOC/lib
UTDMSESSION="$PKG_OPT_INFO/SUNWut/lib/utdmsession"
IDLEDIR=/var/opt/SUNWut/idle
DPYDIR=/var/opt/SUNWut/displays

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

if [ "$DPY" != "" -a -f $DPYDIR/$DPY -a "$DTXSERVERLOCATION" = "local" ]
then
	/bin/rm -f $IDLEDIR/$DPY.info
	if [ -f $IDLEDIR/$DPY.pid ]
	then
		# If something earlier in session startup has set
		# UT_REAPER_MAY_DEFER to "1" to indicate that this
		# system is able to kill the reaper at a later time
		# then we examine the detached-greeter reaper
		# configuration parameters to see whether we should
		# kill it now or let it live and trust something
		# else to kill it later.
		#
		# 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 to the variable we
		# care about and emits the RHS of the final assignment.
		#
		REAPERCONF=/etc/opt/SUNWut/reaper.conf
		if [ "1" = "$UT_REAPER_MAY_DEFER" ] && \
				[ -f $REAPERCONF ] ; then
		    REAPERCONF_DEFER=`
			sed -n '/^REAPER_DEFER_DISARM[ 	]*=/ {
			    h
			    }
			    $ {
			    x
			    s/^REAPER_DEFER_DISARM[ 	]*=[ 	]*//p
			    }' $REAPERCONF`
		fi

		case "$REAPERCONF_DEFER" in
		    0 | 1)
			;;
		    *) REAPERCONF_DEFER=0
			;;
		esac

		# If REAPERCONF_DEFER ended up with any value other
		# than "1" then we kill the reaper now.  If the value
		# is "1" then we let the reaper live, something else
		# will kill it later.
		#
		if [ "1" != "$REAPERCONF_DEFER" ] ; then

		    # Stop the utaction that would reap this session.
		    #
		    UTPID=$(/bin/cat $IDLEDIR/$DPY.pid)
		    /bin/rm -f $IDLEDIR/$DPY.pid
		    kill $UTPID
		fi
	fi

	if [ -f $DPYDIR/$DPY ]
	then
		# Update the timestamp on the display file so that GM can
		# recognise this session as currently logged-in.
		touch $DPYDIR/$DPY
	else
		MSG="error: no Sun Ray display file for display $DPY"
		echo $MSG 2>&1
		logger -t Xstartup -ip user.error "$MSG"
	fi

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

fi

#
