#!/sbin/sh
#
# ident "@(#)utstorage.sh	1.13	05/06/23 SMI"
#
# Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH=/bin:/usr/bin
export PATH

BASE=`/etc/opt/SUNWut/basedir/lib/utprodinfo -r SUNWuto`
SUNWUT=$BASE/SUNWut
SUNWUTLIB=$SUNWUT/lib
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
}

case "$1" in

'start'|'restart')
	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"
		(exec 1>&- ; \
		exec 2>&-; \
		${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"
		(exec 1>&- ; \
		exec 2>&-; \
		${SUNWUTLIB}/utstoraged -r $SRMS_DEBUG < /dev/null 2>&1 \
			| $SUNWUTLOGGER -o ${SUNWUTLOG}/utstoraged.log) &
	fi
	;;

'stop')
	pkill utstoraged
	pkill utmountd
	;;

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