#!/sbin/sh
#
# start - start the master SAM-FS daemon, sam-initd.
#   Verify the /opt/SUNWsamfs/sbin/sam-initd file exists and the
#   /etc/opt/SUNWsamfs/mcf file exists.
#   Start sam-initd if it is not already running.
#
# config - send a HUP to sam-fsd to cause (re)configuration of file systems.
#
# stop - stop the master SAM-FS daemon sam-initd.
#
#	$Id: samd,v 1.9.2.6 2003/02/27 16:46:35 ta129963 Exp $

#
# Return pid of named process in variable "pid"
#   ps -e only returns 8 characters, so match on first 8.
#
pidproc() {
	pid2find=`echo $1 | cut -c 1-8`
	pid=`/usr/bin/ps -e |
		/usr/bin/grep $pid2find |
		/usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
}

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

usage() {
	echo "Usage: /opt/SUNWsamfs/sbin/samd { start | config | stop }"
	exit 1
}

if [ $# != 1 ] ; then
	usage
fi

case "$1" in

'config')

	pidproc sam-fsd
	echo "Configuring SAM-FS"
	if [ "${pid}" = "" ]; then
		/opt/SUNWsamfs/sbin/sam-fsd -C
		sleep 5
		/opt/SUNWsamfs/sbin/sam-fsd > /dev/null
		sleep 2
		pidproc sam-fsd
	fi
	if [ "${pid}" = "" ]; then
		echo "Failed to start sam-fsd; check logs and inittab"
		exit 1
	fi
	kill -HUP ${pid}
	sleep 2
	/opt/SUNWsamfs/sbin/sam-fsd > /dev/null
	;;

'start')

# The following checks for the mcf file and sam-initd already running.
	if [ ! -f /opt/SUNWsamfs/sbin/sam-initd ]; then
		echo "SAM-FS daemon file (/opt/SUNWsamfs/sbin/sam-initd) not found"
		exit 1
	fi
	if [ ! -f /var/opt/SUNWsamfs/mcf.bin ]; then
		echo "SAM-FS configuration file (/var/opt/SUNWsamfs/mcf.bin) not found"
		exit 0
	fi
	pidproc sam-initd
	if [ "${pid}" != "" ]; then
		echo "SAM-FS sam-initd daemon already running"
		exit 0
	fi

	pidproc sam-fsd
	if [ "${pid}" = "" ]; then
		echo "Configuring SAM-FS"
		/opt/SUNWsamfs/sbin/sam-fsd -C
		sleep 5
		/opt/SUNWsamfs/sbin/sam-fsd > /dev/null
		sleep 2
		pidproc sam-fsd
	fi
	echo "Starting SAM-FS sam-initd daemon"
	if [ "${pid}" = "" ]; then
		echo "Failed to start sam-fsd; check logs and inittab"
		exit 1
	fi
	kill -USR1 ${pid}
	;;

'stop')

	killproc sam-initd
	;;

*)
	usage
	;;
esac
exit 0
