#!/bin/ksh -p 

#
# ident "@(#)loginGUI.start.sh	1.17 03/04/23 SMI"
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
#
# Generic start script for NSCloginGUI and SCloginGUI. This
# script takes two arguments:
#
#	loginGUI.start <login_GUI_program> <fulltoken>
#

#exec 2>&1 2>/var/tmp/loginGUI.start.$$ 	# Debug
#set -x

set -u

OPENWINHOME=/usr/openwin
export PATH=/bin:/sbin:/usr/bin:$OPENWINHOME/bin
MOD="`basename $0`"

KILL="/bin/kill -s KILL"

# command-line arguments
LOGIN_GUI_PROG=${1}
FULLTOKEN=${2}

# syslog tag
export LOGNAME=__${LOGIN_GUI_PROG}__

# Name of X resource file
LOGIN_GUI_RESOURCE_FILE=${LOGIN_GUI_PROG}.res

# temporary directories
TMPDIR=/var/opt/SUNWut/tmp
TMPHOME=/$TMPDIR/.$LOGIN_GUI_PROG/$$

UTLIB=$(/usr/bin/pkginfo -r SUNWuto)/SUNWut/lib
UNCONFIG_DONE=false

# various process PIDs
LOGIN_GUI_PID=0
UTACTION_PID=0

    # unconfigure any tokens
    function unconfigure {
	if ! $UNCONFIG_DONE
	then
		UNCONFIG_DONE=true
		${UTLIB}/utdtsession -t $FULLTOKEN delete <<-!
		!
	fi
    }

    # cleanup any running processes and temporary directories
    function cleanup {

	cd $TMPDIR

	# try to cleanup any running GUI and utaction
	[ "$LOGIN_GUI_PID" != "0" ] && $KILL $LOGIN_GUI_PID

	# try to cleanup any running GUI and utaction
	[ "$UTACTION_PID" != "0" ] && $KILL $UTACTION_PID

	rm -fr $TMPHOME
	unconfigure
    }

    #
    # Handle locale fonts
    #
    ADDFONTPATH() {
	# Combine lines together to make arguments for the xset command
	FP=`awk '
	BEGIN { fp="fp+ " }
	/^[ 	]*$/ { fp=" +fp " ; continue }	
	{ printf("%s%s", fp, $0) ; fp="," } ' $1`
	if [ -n "$FP" ]; then
		eval "$OPENWINHOME/bin/xset $FP"
	fi
	$OPENWINHOME/bin/xset fp rehash
    }

    #
    # Start main code here
    #

    # setup a trap handler to cleanup if we had any errors
    trap "cleanup" 0 1 2 15

    # cleanup any previous temp home dir and create new instance of home dir
    rm -rf $TMPHOME
    umask 077
    mkdir -p $TMPHOME
    export HOME=$TMPHOME
    cd $HOME

    # setup X server root window properties
    $OPENWINHOME/bin/xsetroot -solid black -cursor_name left_ptr

    # set the language for message catalogs
    #
    # Read /etc/default/init and grab all variables there and set them in
    # the environment.
    # There will be a htt_server(XIM) run for Japanese, Korean, Simplified
    # Chinese and Traditional Chinese locales only, as these are the only
    # Multibyte locales supported for loginGUI (NSCloginGUI and SCloginGUI).
    #
    if [ -r /etc/default/init ]; then
	eval $(awk '/^#/ {next} NF == 1 && /[^=]=/ {print "export",$1}' /etc/default/init)
	[[ -n "${LANG:-}" ]] && LOCALE=$LANG
	[[ -n "${LC_CTYPE:-}" ]] && LOCALE=$LC_CTYPE
	[[ -n "${LC_ALL:-}" ]] && LOCALE=$LC_ALL
	if [[ -n "${LOCALE:-}" &&
		-r $OPENWINHOME/lib/locale/$LOCALE/OWfontpath ]]; then
			ADDFONTPATH $OPENWINHOME/lib/locale/$LOCALE/OWfontpath
	fi
        unset LOCALE
    fi

    # setup X environment variables to find various X resources

    # Localized message catalogs path
    export NLSPATH=${UTLIB}/locale/%L/LC_MESSAGES/%N.cat

    # X resources file
    export XENVIRONMENT=${UTLIB}/app-defaults/${LOGIN_GUI_RESOURCE_FILE}

    # pass the basename of the token to the GUI
    # 4852961 We want to pass the raw token not the translated
    #         token, since the raw token is used by the login
    #         GUI as part of the client name it registers as with
    #         PAM when handling authenticated smartcards.
    if [ -n "$RAW_TOKEN_ID" ]
    then
	GUItoken="$RAW_TOKEN_ID"
    else
	GUItoken="$FULLTOKEN"
    fi

    baseTOKEN="`echo $GUItoken | awk -F. '{print $1}'`"

    # start the GUI and note its PID
    ${UTLIB}/$LOGIN_GUI_PROG -t "$baseTOKEN" &
    LOGIN_GUI_PID=$!

    # setup a utaction that will kill an inactive GUI after the
    # GUI session is disconnected from the DTU
    ${UTLIB}/utaction -i -d "$KILL $LOGIN_GUI_PID" &
    UTACTION_PID=$!
    wait $LOGIN_GUI_PID
    $KILL $UTACTION_PID

    exit 0
