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

SUNWUTETC=/etc/opt/SUNWut
SUNWUTTSC_LOC="`$SUNWUTETC/basedir/lib/utprodinfo -r SUNWuttsc`/SUNWuttsc"
SUNWUTTSCLIB=$SUNWUTTSC_LOC/lib

umask 0022

NAWK=/usr/bin/nawk

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

startproc() {

	if [ -x ${SUNWUTTSCLIB}/uttscpd ]; then
	    echo "Starting Sun Ray Connector proxy"
	    ${SUNWUTTSCLIB}/uttscpd  < /dev/null 2>&1 &
	fi
}

stopproc() {
	echo "Stopping Sun Ray Connector proxy.."
	pkill uttscpd
}

case "$1" in

'start')
	PID=`pidproc uttscpd`
	if [ -n "$PID" ]
	then
	        #
                # service is running - ignore
	        #
	        echo "Sun Ray 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 Proxy.."
                kill $PID
        fi
	startproc	
	;;

'stop')
	# Kill all uttscpd processes 
	stopproc
	;;

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