#!/bin/sh
#
#pragma ident	"@(#)initpmf	1.12	01/03/14 SMI"
#
# Copyright (c) 1998-2001 by Sun Microsystems, Inc.
# All rights reserved.
#

# Start/stop processes required for init pmf

LIBSCDIR=/usr/cluster/lib/sc
BINDIR=/usr/cluster/bin
USRBIN=/usr/bin
SERVER=rpc.pmfd
svcfound=no


case "$1" in
'start')
	# test whether we are a cluster and exit if not a cluster
	/usr/sbin/clinfo > /dev/null 2>&1
	if [ $? != 0 ] ; then
		exit 0
	fi

	# Find if an instance of rpc.pmfd is already running and if yes exit
	# We only allow one rpc.pmfd at a time.
	pidlist=`${USRBIN}/ps -e | ${USRBIN}/grep ${SERVER} \
		| ${USRBIN}/awk '{print $1}'`
	if [ -n "${pidlist}" ] ; then
		${USRBIN}/logger -p local0.err -t INITPMF "Error: ${SERVER} is already running."
                exit 1;
	fi

	# Find out if host is running 32 or 64 bit Solaris
	# and start the appropriate server 
	version=`${USRBIN}/optisa sparcv9 sparc`
	if [ "${version}" = "sparcv9" ] ; then
		${LIBSCDIR}/sparcv9/${SERVER}
	else 
		${LIBSCDIR}/${SERVER}
	fi

	if [ $? -ne 0 ]; then
		${USRBIN}/logger -p local0.err -t INITPMF "Error: Can't start ${SERVER}."
		exit 1
	fi

	# sleep for 2 sec to give the server the chance to come up
	${USRBIN}/sleep 2

        # loop for 2 min checking that the server is up 
	# by doing a stat command
	for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; do
		${BINDIR}/pmfadm -L 2>/dev/null 1>/dev/null
		if [ $? -eq 0 ]; then
			svcfound=yes
			break
		fi
		${USRBIN}/sleep 5
	done

	if [ $svcfound = no ]; then
       		${USRBIN}/logger -p local0.err -t INITPMF "Error: Timed out waiting for ${SERVER} service to register."
		exit 1
	fi

	;;

'stop')
	# Find if rpc.pmfd is running and if yes kill it.
	# This works on a list, but there shouldn't be more than one
	# instance running (see start case).
	pidlist=`${USRBIN}/ps -e | ${USRBIN}/grep ${SERVER} \
		| ${USRBIN}/awk '{print $1}'`
	if [ -n "${pidlist}" ] ; then
	        # kill each process in list
	        for pid in ${pidlist}
	        do
	                kill -TERM ${pid}
			echo "Notice: ${SERVER} is being stopped."
	        done
	fi

	;;
*)
	echo "Usage: /etc/init.d/initpmf { start | stop }"
	;;
esac
