#!/sbin/sh

#    SAM-QFS_notice_begin
#
#      Solaris 2.x Sun Storage & Archiving Management File System
#
#      Copyright (c) 2004 Sun Microsystems, Inc.
#      All Rights Reserved.
#
#      Government Rights Notice
#      Use, duplication, or disclosure by the U.S. Government is
#      subject to restrictions set forth in the Sun Microsystems,
#      Inc. license agreements and as provided in DFARS 227.7202-1(a)
#      and 227.7202-3(a) (1995), DRAS 252.227-7013(c)(ii) (OCT 1988),
#      FAR 12.212(a)(1995), FAR 52.227-19, or FAR 52.227-14 (ALT III),
#      as applicable.  Sun Microsystems, Inc.
#
#    SAM-QFS_notice_end

#
# start - start the master SAM-FS daemon, sam-amld.
#   Verify the /opt/SUNWsamfs/sbin/sam-amld file exists and the
#   /var/opt/SUNWsamfs/mcf.bin file exists.
#   Start sam-amld 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-amld.
#
# For start and stop, if /opt/SUNWsamfs/sbin/sam-amld is not present,
# this must be a SUNWqfs package, so issue a usage message.
#
#	$Id: samd,v 1.20 2004/03/02 20:28:15 cp129962 Exp $

#
# Return pid of named process in variable "pid"
#
pidproc() {
	pid=`/usr/ucb/ps -ax |
		/usr/bin/awk '{print $1 " " $5}' |
		/usr/bin/sed -e 's/\/.*\///' |
		/usr/bin/grep " ${1}$" |
		/usr/bin/awk '{print $1}'`
}

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

usage() {
	if [ ! -f /opt/SUNWsamfs/sbin/sam-amld ]; then
		echo "Usage: /opt/SUNWsamfs/sbin/samd  config"
	else
		echo "Usage: /opt/SUNWsamfs/sbin/samd { start | config | stop }"
	fi
	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-amld already running.
	if [ ! -f /opt/SUNWsamfs/sbin/sam-amld ]; then
		usage
	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-amld
	if [ "${pid}" != "" ]; then
		echo "SAM-FS sam-amld 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-amld daemon"
	if [ "${pid}" = "" ]; then
		echo "Failed to start sam-fsd; check logs and inittab"
		exit 1
	fi
	kill -USR1 ${pid}
	;;

'stop')

	if [ ! -f /opt/SUNWsamfs/sbin/sam-amld ]; then
		usage
	fi
	killproc sam-amld
	;;

*)
	usage
	;;
esac
exit 0
