#!/bin/sh
#
# ident "@(#)utsvc.sh	1.77 05/06/09 SMI"
#
# Copyright 1998-2005 Sun Microsystems, Inc.  All rights reserved.
#
###########################################
# chkconfig header info for RH
#
# chkconfig: 5 98 02
# description: Sun Ray server
#
###########################################
# insserv header info for JDS/SuSE
### BEGIN INIT INFO
# Provides:			utsvc
# Required-Start:		utsyscfg utacleanup
# X-UnitedLinux-Should-Start:
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start:		5
# Default-Stop:			1 2 3 4
# Short-Description:
# Description:			Sun Ray server
### END INIT INFO


PATH=/bin:/usr/bin:/sbin
export PATH

BASE=`/etc/opt/SUNWut/basedir/lib/utprodinfo -r SUNWuto`
SUNWUT=$BASE/SUNWut
SUNWUTLIB=$SUNWUT/lib
SUNWUTETC=/etc/opt/SUNWut
SUNWUTLOG=/var/opt/SUNWut/log
SUNWUTLOGGER=$SUNWUTLIB/utlog
SUNWUTTOKENS=/var/opt/SUNWut/tokens
SUNWUTXLOCK=/etc/dt/config/config.lock
UTCONF_FILE=$SUNWUTETC/utadmin.conf
UTPULLD=${SUNWUT}/srds/sbin/utpulld
TMPDIR=/var/opt/SUNWut/tmp

umask 0022

# Directory for the Prototype files for utrepair
#
PROTODIR=$SUNWUTLIB/prototype
UTREPAIR_FILES=""

#
# Set platform dependancies
#
OS=`uname -s`
case "$OS" in
	SunOS)
		UTREPAIR_FILES="/usr/dt/config/Xstartup /usr/dt/config/Xsetup /usr/dt/config/Xreset"
		UTREPAIR_FILES="$UTREPAIR_FILES /usr/openwin/server/etc/OWconfig"
		INETD_CONF="/etc/inet/inetd.conf"
		;;
	Linux)
		UTREPAIR_FILES="/etc/X11/xdm/Xstartup /etc/X11/xdm/Xsetup /etc/X11/xdm/Xreset"
		# Path for inetd.conf file on JDS and SLES
		INETD_CONF="/etc/inetd.conf"
		;;
esac

#
# inetd configuration: Solaris uses /etc/inet/inetd.conf
# configuration file.  Unfortunately, Linux RH uses a different approach,
# configuration files are stored under the /etc/xinetd.d directory.
# Both JDS and SLES 8 with Service Pack 3 uses /etc/inetd.conf,
# However the Service Pack 3 also creates the /etc/xinetd.d directory.
# Hence the approach used is to setup files for both inetd and xinetd.
# 
XINETD_DIR=/etc/xinetd.d
INETCONV=/usr/sbin/inetconv
XINETD=xinetd
INETD=inetd
XINETD_HUP_PROG=""
INETD_HUP_PROG=""
if [ -d $XINETD_DIR ]; then
	# for RH server
	UTREPAIR_FILES="$UTREPAIR_FILES $XINETD_DIR/sunray_utrcmdd"
fi
if [ -f $INETD_CONF ]; then
	# for Solaris , JDS & SLES servers
	UTREPAIR_FILES="$UTREPAIR_FILES $INETD_CONF"
fi

# These paths are OK for Solaris, RHEL3, and SLES8.
# If we find a platform that needs a different path,
# We'll have to use an @(KEYWORD) replacment with something
# defined in Makeinc.master.<platform> when building utsvc
GREP=/bin/grep
PS=/bin/ps
SED=/bin/sed
KILL=/bin/kill
NOHUP=/usr/bin/nohup
FIND=/usr/bin/find
XARGS=/usr/bin/xargs
RM=/bin/rm
TOUCH=/bin/touch

if [ -x /bin/priocntl ]; then
    PRIOCNTL="/bin/priocntl -c TS -p 30 -m 30 -e"
else
    PRIOCNTL=
fi

if [ `uname` = "Linux" ]
then
	NAWK=/usr/bin/gawk
else
	NAWK=/usr/bin/nawk
fi


#
# 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
}

#
# 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} ${UTREPAIR_FILES}"

	      # make sure they exist, first
	      ${TOUCH} ${UTREPAIR_FILES}
              ${SUNWUTLIB}/utrepair ${repairargs} > /dev/null 2>&1
	      
	      # Check if it's Trusted Solaris Environment by checking
	      # the existence of trusted network remote-host database and
	      # add "trusted" keyword to utrcmd service in /etc/inet/inetd.conf
	      # This is to specify that the trusted path attribute should
	      # be passed to the server by inetd.
	      if [ -f /etc/security/tsol/tnrhdb ]; then
	      		if grep "^utrcmd" $INETD_CONF | grep -v "trusted" >/dev/null 2>&1; then
				sed -e "/^utrcmd/{
				s/nowait/nowait,trusted/
				}" $INETD_CONF > ${TMPDIR}/utsvc.$$
				cp -f ${TMPDIR}/utsvc.$$ $INETD_CONF
				rm -f ${TMPDIR}/utsvc.$$
			fi
	      fi

	      # HUP the inetd, xinetd daemon if needed
	      XINETD_HUP_PROG=`ps -e -o comm | grep -w $XINETD`
	      INETD_HUP_PROG=`ps -e -o comm | grep -w $INETD`
	      if [ -n "$XINETD_HUP_PROG" ]; then
              	pkill -HUP $XINETD
	      fi
              if [ -n "$INETD_HUP_PROG" ]; then
              	pkill -HUP $INETD
	      fi

	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

	# Call inetconv for Solaris 10
	if [ -x $INETCONV ]; then
		$INETCONV -i $INETD_CONF 1>/dev/null 2>&1
	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
	#
	${FIND} $SUNWUTTOKENS -type f \
		-name '*.lock' -print | ${XARGS} ${RM} -f
	${RM} -f $SUNWUTXLOCK
        #
        # Synchronize LDAP
        #

	SRDS_LDAP_CURRENT="${SUNWUTETC}/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
	        if [ -x ${UTPULLD} ]
		then
			${UTPULLD} -o > /dev/null 2>&1 &
		fi
        fi
	
	if [ -f $UTCONF_FILE -a -x ${SUNWUT}/sbin/utkiosk ]; then
		${SUNWUT}/sbin/utkiosk -e kiosk > /dev/null 2>&1 &
	fi

	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"
			${PRIOCNTL} ${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
		echo "starting authentication manager"
		(exec 1>&- ; \
		exec 2>&- ; \
		${NOHUP} \
		${PRIOCNTL} ${SUNWUTLIB}/utauthd -b 2>&1 </dev/null \
		 | $SUNWUTLOGGER -o ${SUNWUTLOG}/auth_log) & 
	fi

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

	if [ -f /etc/opt/SUNWut/http/auto.start ]; then
		APACHECTL=`cat /etc/opt/SUNWut/http/auto.start` 
		APACHECHK="$APACHECTL"
		echo $APACHECHK | grep "httpd " > /dev/null 2>&1
		if [ $? = "0" ]; then
			APACHECHK="${APACHECHK% -k}"
		fi
		if [ -x "${APACHECHK}" ]; then
			echo "starting Apache web server"
			${APACHECTL} start < /dev/null > /dev/null 2>&1 &
		fi
	fi

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

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