#!/bin/sh
#
# ident "@(#)utstorage.sh	1.24	11/11/29 Oracle"
#
# Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
#
###########################################
# chkconfig header info for RH
#
# chkconfig: 5 98 02
# description: Sun Ray Core Services
#
###########################################
# insserv header info for JDS/SuSE
### BEGIN INIT INFO
# Provides:                     utstorage
# Required-Start:               utsyscfg
# X-UnitedLinux-Should-Start:
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start:                5
# Default-Stop:                 1 2 3 4
# Short-Description:
# Description:                  Sun Ray Core Services
### END INIT INFO

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

SUNWUTLIB=/opt/SUNWut/lib
SUNWUTETC=/etc/opt/SUNWut
SUNWUTLOG=/var/opt/SUNWut/log
SUNWUTLOGGER=$SUNWUTLIB/utlog

SRMS_DEBUG=""

umask 0022

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
}

#
# Emit a terse usage message to stdout.  The caller may redirect stdout
# to stderr for the function invocation ('usage 1>&2') if that's where
# the message should go.
#
usage() {
	echo "Usage: $0 {start|stop|status}"
}


if [ $# -ne 1 ] ; then
	usage 1>&2
	exit 1
fi

case "$1" in

'start'|'restart')
	if [ ! -f $SUNWUTETC/utenabled ]; then
		exit 0
	fi

	SLEEP=5
	START_UTMOUNTD="True"
	PIDS=`pidproc utmountd`
	if [ -n "$PIDS" ]
	then
		#
		# mounter service is running - no need to start it again
		# (use "utstorage stop" to stop it)
		#
		START_UTMOUNTD="False"
	fi

	START_UTSTORAGED="True"
	PIDS=`pidproc utstoraged`
	if [ -n "$PIDS" ]
	then
		#
		# storage service is running - no need to start it again
		# (use "utstorage stop" to stop it)
		#
		START_UTSTORAGED="False"
	fi

	sleep $SLEEP

	if [ $START_UTMOUNTD = "True" -a -x ${SUNWUTLIB}/utmountd ]; then
		echo "starting mounter service"
		${SUNWUTLIB}/utmountd $SRMS_DEBUG < /dev/null 2>&1 \
			| $SUNWUTLOGGER -o ${SUNWUTLOG}/utmountd.log &
	fi

	if [ $START_UTSTORAGED = "True" -a -x ${SUNWUTLIB}/utstoraged ]; then
		echo "starting storage service"
		${SUNWUTLIB}/utstoraged -r $SRMS_DEBUG < /dev/null 2>&1 \
			| $SUNWUTLOGGER -o ${SUNWUTLOG}/utstoraged.log &
	fi
	;;

'stop')
	pkill -KILL -x utstoraged
	pkill -KILL -x utmountd
	;;

'status')
	;;

*)
	usage 1>&2
	exit 1
	;;
esac
exit 0
