#!/bin/sh
#*******************************************************************************
#
# NAME:         amdemon
# SUMMARY:      %description%
# COMPONENT:    solsysd
# VERSION:      rm6_28
# UPDATE DATE:  %date_modified: Thu Aug  6 08:11:42 1998 %
# PROGRAMMER:   %created_by:    bmyers %
#
#               Copyright 1996, 1997 by Symbios Logic Inc.
#
# DESCRIPTION:
# The Array Monitor Start/Stop Script is invoked by the system startup/
# shutdown processing.  It has three main functions:  (1) perform SNMP-
# related startup/shutdown processing (entails sending LINK UP/LINK DOWN
# traps), (2) start/stop the array monitor daemon, and (3) start/stop the
# RDAC (Redundant Disk Array Controller) daemons.  There is a single input
# argument, "start" or "stop" to indicate whether we are starting up or
# shutting down.
#
# NOTES:
#
# REFERENCE:
#
# CODING STANDARD WAIVERS:
#
#*******************************************************************************

#*******************************************************************************
# PROCEDURE:	amdemon (main)
# SUMMARY:	Control SYMsm startup/shutdown operations
#
# DESCRIPTION:
# Below is the amdemon main procedure which implements the start/stop
# commands SYMsm startup and shutdown.  As described above, startup
# issues a LINK UP trap and launches the SYMsm daemons and shutdown
# issues a LINK DOWN trap and stops the SYMsm daemons.
#
# SYNTAX:
# amdemon <action>
# <action> = start | stop
# start -> initialize SNMP and daemons for SYMsm
# stop -> "close" SNMP link and stop SYMsm daemons
#
# NOTES:
#
# RETURNS:
# Nothing


# A M D E M O N   M A I N   P R O C E D U R E

PATH=/bin:/usr/sbin

WARNING_MSG="\nWARNING:  The system is configured to ALLOW unprotected offline download of RAID\ncontrollers, AND the root device is a RAID logical unit - do NOT attempt to\nperform an offline firmware upgrade on the RAID module containing your root\ndevice while in this configuration, as it is likely cause the system to crash.\n"

RMPARAMS_FILE=/etc/raid/rmparams
RM_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmHomeDirectory | cut -d= -f2`
RM_BOOT_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmBootHomeDirectory | cut -d= -f2`

LAD=$RM_BOOT_HOME/lad

if grep -v "^#" $RMPARAMS_FILE | grep "Rdac_SupportDisabled.*=.*TRUE" >/dev/null 2>&1
then
	RDAC_DISABLED=TRUE
fi

ISALIST_EXIST=`which isalist | grep no`
if [ -z "${ISALIST_EXIST}" ]
then
        ISALIST="isalist"
else
        ISALIST="echo not"
fi

umask 0002
case $1 in

'start')
	if [ -z "`modinfo | grep sd | grep SCSI`" ]
	then
        	drvconfig -i sd 2>/dev/null
		if [ $? != 0 ]
		then
			exit 1
		fi
	fi
#########################
# send the LINK UP trap #
#########################

	$RM_BOOT_HOME/nvutil -v -f 2>/dev/null
	if [ -s "/etc/resolv.conf" ]
	then
		hostName="`uname -n`"
		if [ -x /usr/bin/getent ]
		then
			hostIP="`getent hosts $hostName | awk '/^[0-9][0-9]*[.]/ {print $1}'`"
		else
			hostIP="`nslookup $hostName | grep Address | tail -1 | awk '{print $2 }'"
		fi
	$RM_HOME/bin/trapcntl -m "" 1 "" $hostIP $hostName 
	fi

###########################
# start the array monitor #
###########################

	pid=`ps -e | grep arraymon | sed -e 's/^  *//' -e 's/ .*//'`
	if [ "${pid}" = "" ]
	then
		if [ -x $RM_HOME/bin/arraymon ]
		then
		        $RM_HOME/bin/arraymon >/dev/null 2>&1
			echo "Array Monitor initiated"
		fi
	fi

##########################
# start the RDAC daemons #
##########################

	if [ -z "${RDAC_DISABLED}" ] # if RDAC *not* disabled ...
	then
	        nbr_dmns=`ps -e | grep rdaemon | grep -v grep | wc -l`
	        if test $nbr_dmns -ne 2
		then
			modnum=`modinfo | grep rdriver | cut -c1-3`
			majnum=`modinfo | grep rdriver | cut -c20-23`
			if [ -n "`$ISALIST | grep sparcv9`" ]
			then
				priocntl -e -c RT $RM_BOOT_HOME/sparcv9/rdaemon $modnum $majnum &
			else
				priocntl -e -c RT $RM_BOOT_HOME/rdaemon $modnum $majnum &
			fi
			echo "RDAC daemons initiated" 
		fi
	fi

   	;;


'stop')

###########################
# send the LINK DOWN trap #
###########################

	hostName="`uname -n`"
	if [ -x /usr/bin/getent ]
	then
		hostIP="`getent hosts $hostName | awk '/^[0-9][0-9]*[.]/ {print $1}'`"
	else
		hostIP="`nslookup $hostName | grep Address | tail -1 | awk '{print $2 }'"
	fi
	$RM_HOME/bin/trapcntl -m "" 2 "" $hostIP $hostName 

##########################
# stop the array monitor #
##########################

	pid=`ps -e | grep arraymon | sed -e 's/^  *//' -e 's/ .*//'`
	if [ "${pid}" != "" ]
	then
		kill ${pid}
		echo "Array Monitor stopped"
	fi

#########################
# stop the RDAC daemons #
#########################

	if [ -z "${RDAC_DISABLED}" ] # if RDAC *not* disabled ...
	then
		# force flush while we still have RDAC protection
		sync; sync; sync
		sleep 3
	
	        nbr_dmns=`ps -e | grep rdaemon | grep -v grep | wc -l`
		if [ $nbr_dmns -eq 2 ]
		then
	        	$RM_BOOT_HOME/Signalrdm kill_rdaemon
	        	echo "RDAC daemons stopped"
		fi
	fi
	;;


*)
	echo "usage: amdemon {start|stop}"
	;;
esac
