#!/bin/ksh
#
# ident "@(#)utrootkiosk.ksh	1.3 04/08/16 SMI"
#
# Copyright (c) 2001 Sun Microsystems, Inc.
# All rights reserved.
#


# $1 home dir
# $2 userid
# $3 display
# $4 token

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

USER=$2
export USER
HOME=$1
export HOME

if [ ! -d /var/opt/SUNWut/session/kiosk/$DPY ]
then
	mkdir /var/opt/SUNWut/session/kiosk/$DPY
fi

#
# Get the authorization cookie to use this display and
# put it in the users home directory
#
/usr/openwin/bin/xauth -f /tmp/SUNWut/config/xauth/$DPY/cookie:$DPY list |  \
	sed "s/^[^ ]*/add `uname -n`:$DPY/" | \
	/usr/openwin/bin/xauth -f $HOME/.Xauthority
/bin/chown $2 $HOME/.Xauthority
/bin/chmod 600 $HOME/.Xauthority

/bin/chown $2 /var/opt/SUNWut/session/kiosk/$DPY
/bin/chmod 600 /var/opt/SUNWut/session/kiosk/$DPY

#UID=`grep "^${USER}:" /etc/passwd | cut -d: -f3`
UID=$(/usr/xpg4/bin/id -u $USER)
echo "uid=$UID" >> /tmp/SUNWut/session_proc/$DPY



VPROTOS=/var/opt/SUNWut/kiosk/prototypes
PROTOS=/opt/SUNWut/kiosk/prototypes

CONFFILE=/var/opt/SUNWut/kiosk/kiosk.conf
. $CONFFILE

#
# Update the home directory based on a prototype home directory.
# Uses the HOME, USER, and GROUP environment variables
#
function updateHome {
        #
        # Copy in the prototype (if any) and fix the permissions
        #
	DBG=":"
        $DBG set -x
        typeset proto="$1"
        $DBG echo copy $proto to $HOME

        if [ ! -z "$proto" -a -d "$proto" ]
        then
                (cd $proto ; find . -depth -print | cpio -pdm -R $USER $HOME)
        fi
}


n=0
while (( $n < $numApps ))
do
	if [ ${appEnable[n]} = 1 ]
	then
		if [ -d $VPROTOS/${appName[n]} ]
		then
		     updateHome $VPROTOS/${appName[n]}
		elif [ -d $PROTOS/${appName[n]} ]
		then
                     updateHome $PROTOS/${appName[n]}
                fi
	fi
	((n+=1))
done
