#!/sbin/sh
#
# ident "@(#)utsvc.sh	1.51 02/11/19 SMI"
#
# Copyright 1998-2002 Sun Microsystems, Inc.  All rights reserved.
#

PATH=/bin:/usr/bin
export PATH

BASE=`pkginfo -r SUNWuto 2>/dev/null`
SUNWUT=$BASE/SUNWut
SUNWUTLIB=$SUNWUT/lib
SUNWUTETC=/etc/opt/SUNWut
SUNWUTLOG=/var/opt/SUNWut/log
SUNWUTTOKENS=/var/opt/SUNWut/tokens
SUNWUTXLOCK=/etc/dt/config/config.lock

umask 0022

# Directory for the Prototype files for utrepair
#
PROTODIR=$SUNWUTLIB/prototype
MODFILES="/usr/dt/config/Xstartup /usr/dt/config/Xsetup /usr/dt/config/Xreset /usr/openwin/server/etc/OWconfig  /etc/inet/inetd.conf"

#
# Return pid of named process in variable "pid"
#
pidproc() {
	/usr/bin/ps -u root -o pid -o args |
		/usr/bin/grep -w "$1" |
		/usr/bin/grep -v grep |
		/usr/bin/sed -e 's/^  *//' -e 's/ .*//'
}

#
# Kill named process(es)
#
killproc() {
	pid=`pidproc $1`
	[ "$pid" != "" ] && kill -9 $pid
}

case "$1" in

'start'|'restart')
	SLEEP=0
	PIDS=`pidproc utperf`
	if [ -n "$PIDS" ]
	then
		echo "stopping performance gathering"
		kill "$PIDS"
	fi
	echo "stopping authentication manager"
	${SUNWUTLIB}/utauthd -e
	SLEEP=5

        #
        # Enable file cleanup using utrepair
        #
        if [ -x ${SUNWUTLIB}/utrepair ]; then

              #
              # Build the argument list for utrepair
              #
              repairargs="-a -d ${PROTODIR} ${MODFILES}"

              ${SUNWUTLIB}/utrepair ${repairargs}
	fi

	if [ -x ${SUNWUTLIB}/utrepair_services ]; then

	      #
	      # For /etc/inet/services file , we use the special
	      # utrepair_services script so that we can cleanup the
	      # duplicate SunRay entries in the file.
	      #
	      ${SUNWUTLIB}/utrepair_services
        fi

	START_UTSESSIOND="True"
	PIDS=`pidproc utsessiond`
	if [ -n "$PIDS" ]
	then
		#
		# session manager is running - no need to start it again
		# (use "utsvc stop" to stop it)
		#
		START_UTSESSIOND="False"
	fi

	START_UTDEVMGRD="True"
	PIDS=`pidproc utdevmgrd`
	if [ -n "$PIDS" ]
	then
		#
		# device manager is running - no need to start it again
		# (use "utsvc stop" to stop it)
		#
		START_UTDEVMGRD="False"
	fi

	START_UTPARALLELD="True"
	PIDS=`pidproc utparalleld`
	if [ -n "$PIDS" ]
	then
		#
		# printer service is running - no need to start it again
		# (use "utsvc stop to stop it)
		#
		START_UTPARALLELD="False"
	fi

	START_UTSERIALD="True"
	PIDS=`pidproc utseriald`
	if [ -n "$PIDS" ]
	then
		#
		# serial service is running - no need to start it again
		# (use "utsvc stop to stop it)
		#
		START_UTSERIALD="False"
	fi

	sleep $SLEEP

	#
	# Clean up old lock files from utdtsession
	#
	/bin/find $SUNWUTTOKENS -type f \
		-name '*.lock' -print | /bin/xargs /bin/rm -f
	rm -f $SUNWUTXLOCK
        #
        # Synchronize LDAP
        #

	SRDS_LDAP_CURRENT="/etc/opt/SUNWut/srds/current"
        SRDS_DSSERV_CONF=$SRDS_LDAP_CURRENT/utdsd.conf

	# Only run dspulld if this machine is a secondary

        if grep '^pull_replica' $SRDS_DSSERV_CONF >/dev/null 2>&1
        then
                /opt/SUNWut/srds/lib/utpulld -o > /dev/null 2>&1 &
        fi
	
	/opt/SUNWut/sbin/utkiosk -e kiosk > /dev/null 2>&1 &

	if [ $START_UTSESSIOND = "True" -a -x ${SUNWUTLIB}/utsessiond ]; then
		(
			cd ${SUNWUTLOG}

			auth_permit=${SUNWUTETC}/auth.permit
			if [ -f ${auth_permit} ]
			then
				autharg="-c ${auth_permit}"
			else
				autharg=""
			fi

			echo "starting session manager"
			/bin/priocntl -e -c TS -p 30 -m 30 \
				${SUNWUTLIB}/utsessiond -r ${autharg} \
				< /dev/null > /dev/null 2>&1 &
		)
	fi

	if [ $START_UTDEVMGRD = "True" -a -x ${SUNWUTLIB}/utdevmgrd ]; then
		(
			cd ${SUNWUTLOG}

			auth_permit=${SUNWUTETC}/auth.permit
			if [ -f ${auth_permit} ]
			then
				autharg="-c ${auth_permit}"
			else
				autharg=""
			fi

			echo "starting device manager"
			${SUNWUTLIB}/utdevmgrd -r ${autharg} \
				< /dev/null > /dev/null 2>&1 &
		)
	fi

	if [ $START_UTPARALLELD = "True" -a -x ${SUNWUTLIB}/utparalleld ]; then
		echo "starting printer service"
		${SUNWUTLIB}/utparalleld -r < /dev/null > /dev/null 2>&1 &
	fi

	if [ $START_UTSERIALD = "True" -a -x ${SUNWUTLIB}/utseriald ]; then
		echo "starting serial service"
		${SUNWUTLIB}/utseriald -r < /dev/null > /dev/null 2>&1 &
	fi

	if [ -f ${SUNWUTETC}/auth.props -a -x ${SUNWUTLIB}/utauthd ]; then
		here=`pwd`
		cd ${SUNWUTLOG}
		${SUNWUTLIB}/utagelog auth_log 9
		echo "starting authentication manager"
		/usr/bin/nohup \
		/bin/priocntl -c TS -p 30 -m 30 -e \
			${SUNWUTLIB}/utauthd -b > auth_log 2>&1 </dev/null & 
		cd $here
	fi

	if [ -x ${SUNWUT}/sbin/utperf ]
	then
		echo "starting performance gathering"
		/usr/bin/nohup \
		${SUNWUT}/sbin/utperf >/dev/null 2>/dev/null </dev/null &
	fi

	;;
'stop')
	PIDS=`pidproc utperf`
	if [ -n "$PIDS" ]
	then
		kill "$PIDS"
	fi
	${SUNWUTLIB}/utauthd -e
	${SUNWUT}/sbin/utsession -r -a >/dev/null 2>&1
	killproc utsessiond
	killproc utseriald
	killproc utparalleld
	killproc utdevmgrd
	;;

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