#!/bin/sh
#
#
# "$Date$ $Revision"
#
# Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
#
#
###########################################
# chkconfig header info for RH
#
# chkconfig: 5 98 02
# description: Sun Ray Connector
#
###########################################
# insserv header info for JDS/SuSE
### BEGIN INIT INFO
# Provides:                     uttscp
# Required-Start:               
# X-UnitedLinux-Should-Start:
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start:                5
# Default-Stop:                 1 2 3 4
# Short-Description:
# Description:                   Sun Ray Connector
### END INIT INFO

PATH=/bin:/usr/bin
export PATH

SUNWUTETC=/etc/opt/SUNWut
SUNWUTBASE=/opt/SUNWut
SUNWUTLIB=$SUNWUTBASE/lib
SUNWUTTSC_LOC=/opt/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
            ${SUNWUTTSCLIB}/uttscpd  $USB_DEBUG_ON 2>&1 \
                | $SUNWUTLOGGER -o ${SUNWUTLOG}/uttscpd.log &
        fi
}

stopproc() {
	pkill -x uttscpd
}

case "$1" in

'start')
	# If the product isn't enabled yet, do nothing
	if [ ! -f $SUNWUTETC/utenabled ]; then
		exit 0
	fi

	PID=`pidproc uttscpd`
	if [ -n "$PID" ]
	then
	        #
                # service is running - ignore
	        #
		exit 0
	fi
	startproc	
	;;
'restart')
	# If the product isn't enabled yet, do nothing
	if [ ! -f $SUNWUTETC/utenabled ]; then
		exit 0
	fi

	# stop and start the parent daemon process only
        PID=`pidproc uttscpd`
        if [ -n "$PID" ]
        then
                #
                # service is running - stop it
                #
                kill $PID
        fi
	startproc	
	;;

'stop')
	# Kill all uttscpd processes 
        PID=`pidproc uttscpd`
        if [ -n "$PID" ]
        then
                stopproc
        else
                exit 0
        fi
	;;

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