#!/bin/ksh -p
#
# ident "@(#)bbcontrol.src	1.11 05/02/17 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# expect $1 to be the unix user number to construct 
# user and ruser

. $BBROOT/etc/opt/SUNWbb/blackbox.rc   

export USER=$BBUSER 

# exec all the applictaion scripts installed into $BB_APPS_RC
# those will set up per user config needed to run the
# application
for SCRIPT in `ls $BB_APPS_RC/*`; do
    if [ -x $SCRIPT ]; then
        echo "sourcing $SCRIPT..."
        . $SCRIPT
    fi
done

# start the browser start deamon. It expects a named pipe
# in $BBROOTPATH/doors (owned by the bbuser)
# It waits in a loop for the user to write something
# to the other end.
DOOR=$BBROOTPATH/doors/$BBUSER
$BBPATH/bin/bbstartd $DOOR &
echo > $BBTMPPATH/${BBUSER}.session_ok

if [ -x /opt/SUNWut/kiosk/bin/utstartkiosk ]; then 
	/opt/SUNWut/kiosk/bin/utstartkiosk $BBHOME $BBUSER $BBDISPLAY $SUN_SUNRAY_TOKEN
else
	export LANG=$BB_KIOSK_LANG
	export LC_ALL=$BB_KIOSK_LANG
	# next startup a browser and the window manager
	if [ -x $BBPATH/bin/bbstartbrowser ]; then
        	$BBPATH/bin/bblock -n \
			$BROWSER_LOCK \
			$BBPATH/bin/bbstartbrowser &
	fi

	$BBPATH/bin/bbchroot $BBROOTPATH \
		"/bin/wm -display $BBDISPLAY -f /config/wmrc"
fi

rm -f $BBTMPPATH/${BBUSER}.session_ok

RET=0
PID=`cat  $BROWSER_LOCK 2>/dev/null`
while [ $? = 0 -a "$PID" != "" -a $RET != 2 ]; do
        $BBPATH/bin/bbkillproc 15 $PID bbstartbrowser
	RET=$?
	sleep 1
	PID=`cat $BROWSER_LOCK 2>/dev/null`
done

$BBPATH/bin/bbkillproc 0 0 bbstartd
if [ $? = 0 ]; then 
	echo "stop" > $DOOR
fi

