#!/bin/sh
#
#
# "$Date$ $Revision"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
PATH=/bin:/usr/bin
export PATH

SUNWUTETC=/etc/opt/SUNWut
SUNWUTBASE="`$SUNWUTETC/basedir/lib/utprodinfo -r SUNWuto`/SUNWut"
SUNWUTLIB=$SUNWUTBASE/lib
SUNWUTTSC_LOC="`$SUNWUTETC/basedir/lib/utprodinfo -r SUNWuttsc`/SUNWuttsc"
SUNWUTTSCLIB=$SUNWUTTSC_LOC/lib
SUNWUTLOG=/var/opt/SUNWut/log
SUNWUTLOGGER=$SUNWUTLIB/utlog

#To enable debugging uncomment the following line and comment out
# the current definition of USB_DEBUG_ON
#USB_DEBUG_ON="-D 5"
USB_DEBUG_ON=

umask 0022

AWK=""

if [ -x /usr/bin/nawk ]; then
        AWK=/usr/bin/nawk
elif [ -x /usr/bin/gawk ]; then
        AWK=/usr/bin/gawk
fi 

#
# Return pid(s) of process named in argument
#
pidproc() {
	ps -u root -o pid -o args |
		grep -w "$1" |
		${AWK} '($2 ~ PROCNAME) { print $1 }' PROCNAME=$1
}

startproc() {

        if [ -x ${SUNWUTTSCLIB}/uttscpd ]; then
            echo "Starting Sun Ray connector for windows proxy daemon"
            ${SUNWUTTSCLIB}/uttscpd  $USB_DEBUG_ON 2>&1 \
                | $SUNWUTLOGGER -o ${SUNWUTLOG}/uttscpd.log &
        fi
}

stopproc() {
	echo "Stopping Sun Ray connector for windows proxy daemon"
	pkill -x uttscpd
}

case "$1" in

'start')
	PID=`pidproc uttscpd`
	if [ -n "$PID" ]
	then
	        #
                # service is running - ignore
	        #
	        echo "Sun Ray connector for windows proxy daemon is already running"
		exit 0
	fi
	startproc	
	;;
'restart')
	# stop and start the parent daemon process only
        PID=`pidproc uttscpd`
        if [ -n "$PID" ]
        then
                #
                # service is running - stop it
                #
                echo "Stopping Sun Ray connector for windows proxy daemon"
                kill $PID
        fi
	startproc	
	;;

'stop')
	# Kill all uttscpd processes 
        PID=`pidproc uttscpd`
        if [ -n "$PID" ]
        then
                stopproc
        else
                echo "Sun Ray connector for windows proxy daemon is already stopped"
                exit 0
        fi
	;;

*)
	echo "Usage: $0 { start | restart | stop }"
	;;
esac
exit 0
