#!/bin/ksh -p
#
# ident "@(#)utstartkiosk.ksh	1.12 01/06/12 SMI"
#
# Copyright (c) 2001 Sun Microsystems, Inc.
# All rights reserved.
#


#
# utstartkiosk
#

#this is code from kiosk.start general to kiosk mode

#
# Where the package gets loaded
#
KIOSKBASE=/opt/SUNWut/kiosk

#
# Read the configuration file
#
. /var/opt/SUNWut/kiosk/kiosk.conf

if [ "$debug" = "1" ]
then
        exec 2>/var/tmp/kiosk.$2.$$ 1>&2  # Debug
        set -x
        set -u
	DBG=""
	print -u2 "$# args = $*"
	env 
else
	DBG=":"
fi

export PATH=/bin:/sbin:/usr/bin:/usr/openwin/bin:/usr/dt/bin

#
# Setup
#

#
# $SCRIPTDIR is the directory where the scripts that start the window
# manager, browser, etc. that define the kiosk session are found
#
SCRIPTDIR=$KIOSKBASE/wrappers

#
# $BINDIR is the directory where package-defined commands are 
#
BINDIR=$KIOSKBASE/bin


UTLIB=$(/usr/bin/pkginfo -r SUNWuto)/SUNWut/lib
UTJ=$(/usr/bin/pkginfo -r SUNWutj)/SUNWut/jre
XInputMethod="/usr/openwin/bin/htt_server"
export OPENWINHOME=/usr/openwin

#
# See if the token is registered and, if so, what is the user name
#
#USER=$(/opt/SUNWconn/bin/ldapsearch -L -b "o=utdata" "utid=${1#user.}" | \
#	grep utname | grep -v utid | awk '{print $2}')
export USER=$2
export DISPLAY=$3
export BBHOME=$1
export audiodev=$BBAUDIODEV


#
# setup dtsession defaults
#

LANG=C /usr/dt/bin/dtsession_res -merge -system

export LANG="$BB_KIOSK_LANG"
export LC_ALL="$BB_KIOSK_LANG"

/usr/dt/bin/dtsession_res -merge -system

#
# font setup from Xsetup
#
XDIR=/usr/openwin/bin
OPENWINHOME=/usr/openwin

# ###########################################################################
#
# Append desktop font aliases to font path
#
# ###########################################################################

ADDFONTPATH_LOCAL() {
        # 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 "$XDIR/xset $FP"
        fi
}

#
# Reset Xserver font path to its default values
#

$XDIR/xset fp default

#
# Switch Sun's Alt and Meta mod mappings to work with Motif
#
if $XDIR/xmodmap | /bin/grep mod4 | /bin/grep Alt > /dev/null 2>/dev/null
then
    $XDIR/xmodmap -e "clear Mod1" \
            -e "clear Mod4" \
            -e "add Mod1 = Alt_L" \
            -e "add Mod1 = Alt_R" \
            -e "add Mod4 = Meta_L" \
            -e "add Mod4 = Meta_R"
fi

# Append desktop font paths. Note: these directories should be
# accessable by the X server. The file precedence is:
#
#   /etc/dt/config/xfonts/C
#   /usr/dt/config/xfonts/C
#   /etc/dt/config/xfonts/$LANG
#   /usr/dt/config/xfonts/$LANG
#
# Since X server is local, optimize by checking local desktop
# font directories and making one call to xset.
#

if [ -r $OPENWINHOME/lib/locale/$LANG/OWfontpath ]; then
    ADDFONTPATH_LOCAL $OPENWINHOME/lib/locale/$LANG/OWfontpath
fi

if [ -f /etc/dt/config/xfonts/C/fonts.dir ]; then
    fontpath=/etc/dt/config/xfonts/C
fi

if [ -f /usr/dt/config/xfonts/C/fonts.dir ]; then
  if [ -z "$fontpath" ]; then
    fontpath=/usr/dt/config/xfonts/C
  else
    fontpath=$fontpath,/usr/dt/config/xfonts/C
  fi
fi

if [ "$LANG" != "C" ]; then
  if [ -f /etc/dt/config/xfonts/$LANG/fonts.dir ]; then
    if [ -z "$fontpath" ]; then
      fontpath=/etc/dt/config/xfonts/$LANG
    else
      fontpath=$fontpath,/etc/dt/config/xfonts/$LANG
    fi
  fi
fi

if [ "$LANG" != "C" ]; then
  if [ -f /usr/dt/config/xfonts/$LANG/fonts.dir ]; then
    if [ -z "$fontpath" ]; then
      fontpath=/usr/dt/config/xfonts/$LANG
    else
      fontpath=$fontpath,/usr/dt/config/xfonts/$LANG
    fi
  fi
fi

if [ ! -z "$fontpath" ]; then
  $XDIR/xset fp+ $fontpath
fi

#
# end font setup
#

fulltoken=${4}
shorttoken=${fulltoken#*.*.}
SUN_SUNRAY_TOKEN=$shorttoken
shift 4
options="${@:-}"
umask 077
unconfig_done=false

#
# PIDs that need to be stopped/frozen when session is disconnected.
# Usually the browser.
#
freezePIDs=""

#
# The set of pids for "critical" applications. If any of them exit,
# the session is terminated. Usually the window manager and browser
#
waitPIDs=""

#
# The list of pids for processes that should be terminated when the
# session exits (other than waitipids)
#
killPIDs=""

tmphome=""

export LD_LIBRARY_PATH=${UTLIB}:${UTJ}/lib


#
# Launch an application as the kiosk user, setting up the environment
# variables appropriately. The command can either be specified as the
# parameters to launchApp, or via the $applaunch shell variable
#
function launchApp {
	#
	# Take command from command line or in $applaunch variable
	#
	$DBG set -x
	if [ ! -z "$*" ]
	then
		applaunch="$*"
	fi
	typeset envs=""
	envs="$envs LD_LIBRARY_PATH=/usr/dt/lib:/usr/openwin/lib:/usr/lib"
	envs="$envs LD_PRELOAD=libc_ut.so"
	envs="$envs DISPLAY=$DISPLAY"
	envs="$envs SUN_SUNRAY_TOKEN=$shorttoken"
	envs="$envs HOME=$HOME"
	envs="$envs AUDIODEV=$audiodev"
	envs="$envs UTAUDIODEV=$audiodev"
	envs="$envs USER=$USER"
	envs="$envs USERID=$USERID"
	envs="$envs LOGNAME=$USER"
	envs="$envs XAUTHORITY=$HOME/.Xauthority"
        ( cd $HOME; exec /usr/bin/env $envs $applaunch ) &
        lPID=$!
}


#
# Normal kiosk session start. If there is a script in $SCRIPTDIR it is
# consisered a wrapper and should run LaunchApp itself.
# Otherwise, we run launchApp on the path here.
#
function runConfiguredScripts {
	#
	# Step through each configured app
	#
	$DBG set -x 
	SCRIPTDIR=/opt/SUNWut/kiosk/wrappers

	n=0
	while (( $n < $numApps ))
	do
		#
		# BugID 4462909 - fix upgrade problem
		#
		if [ ${appName[$n]} != "001.netscape" ]
		then
			if [ ${appEnable[$n]} = 1 -a ${appAttribute[$n]} != 1 ]
			then
				if [ -x $SCRIPTDIR/${appName[$n]} ]
				then
					. $SCRIPTDIR/${appName[$n]}
				else
					launchApp ${appPath[$n]}
					echo $lPID
					ps -fp $lPID
					if [ ${appAttribute[$n]} = 0 ]
					then
						waitPIDs="$waitPIDs $lPID"
					fi
				fi
			fi
		fi
	   ((n+=1))
	done
}

#
# Run a kiosk session. 
#

function kiosk {
        $DBG set -x
	set -u

	export SESSION_TYPE=Default


	#export USER=$1
	#export DISPLAY=$2
	export GROUP=bbuser
	export USERID=$(/usr/xpg4/bin/id -u $USER)

        tmphome=$BBHOME

	export HOME=$tmphome
	export SUN_SUNRAY_TOKEN=$shorttoken
	export LOGNAME=$USER

	runConfiguredScripts

	launchApp /usr/openwin/bin/xsetroot -cursor_name left_ptr

	#
	# If configured, kill the session after timeout
	#
	if [ "$action" = "1" ]
	then
		/opt/SUNWut/lib/utaction -t $timeout \
		   -d "/usr/proc/bin/prun $waitPIDs ; /bin/kill -9 $waitPIDs"&
	fi

}


#
# Start the kiosk application(s)
#
kiosk

#
# Wait for any critical application to exit then tear it all down
#
DPY=${DISPLAY#*:}
DPY=${DPY%.*}

echo "KILLPIDS=$killPIDs $waitPIDs" >> /var/opt/SUNWut/session/kiosk/$DPY

/opt/SUNWut/lib/waitany $waitPIDs
