#!/bin/sh
# chkconfig: 35 50 50
# description: hmad is a tool to start or stop agent for hma.
#****************************************************************
# *	Copyright (C) 2002, Wistron Inc.  All Rights Reserved.
# *	File Name:		esmsnmpd
# *	Author:			Jerry Zhou
# *	Create Time: 	2002-03-18 20:11:32
# *	Last Update: 	2002-04-04 14:49:14 by Jerry Zhou 
#****************************************************************
# source function library
. /etc/rc.d/init.d/functions
MODDETECT="/usr/local/share/asmagent/wsmbdrv/smbiosinfo -t"

case "$1" in
  start)
	echo ""
	if $MODDETECT | grep "PNPBIOS" 1>/dev/null 2>/dev/null; then
	        cd /usr/local/share/asmagent/whma/hmpdrv/
	        ./hmp_load 1>/dev/null 2>/dev/null
	
	        cd /usr/local/share/asmagent/whma/smbevt/
	        ./smbevtd &
	        sleep 1
	else
		if $MODDETECT | grep "IPMI" 1>/dev/null 2>/dev/null; then
	        cd /usr/local/share/asmagent/whma/imbdrv/
	        ./imb_load 1>/dev/null 2>/dev/null
	
	        cd /usr/local/share/asmagent/whma/imbevt/
	        ./ipmevtd &
	        sleep 1
	    fi
	fi
	echo ""
    ;;
  stop)
	if $MODDETECT | grep "PNPBIOS" 1>/dev/null 2>/dev/null; then
		killall smbevtd
		sleep 1
		cd /usr/local/share/asmagent/whma/hmpdrv/
		./hmp_unload
	else
		if $MODDETECT | grep "IPMI" 1>/dev/null 2>/dev/null; then
			killall  ipmevtd

			while true  ;   do
				Used=$( echo `lsmod | grep imb ` | cut -d' ' -f3 )
				: $((sleeped++))
				[ -z "$Used" ] || [ "$Used" -eq 0 ] || [ $sleeped -gt 15 ] &&  break
				sleep 1
			done	

			cd /usr/local/share/asmagent/whma/imbdrv/
			./imb_unload
		fi
	fi
	echo ""
	;;
  status)
	if $MODDETECT | grep "PNPBIOS" 1>/dev/null 2>/dev/null; then
  		if /sbin/lsmod | grep hmpdrv 1>/dev/null 2>/dev/null ; then
  			echo "health monitor driver installed ..."
  		else
  			echo "health monitor driver not installed ..."
  		fi
	else
		if $MODDETECT | grep "IPMI" 1>/dev/null 2>/dev/null; then
			if /sbin/lsmod | grep ^imb 1>/dev/null 2>/dev/null ; then
		  		echo "health monitor driver installed ..."
	  		else
	  			echo "health monitor driver not installed ..."
	  		fi
		fi
  	fi
	
	if $MODDETECT | grep "PNPBIOS" 1>/dev/null 2>/dev/null; then
  		if ps -A|grep smbevtd 1>/dev/null 2>/dev/null ; then
  			echo "health monitor event daemon is running ..."
  		else
  			echo "health monitor event daemon is stopped ..."
		fi
	else
		if $MODDETECT | grep "IPMI" 1>/dev/null 2>/dev/null; then
		  	if ps -A | grep ipmevtd 1>/dev/null 2>/dev/null ; then
		  		echo "health monitor event daemon is running ..."
	  		else
	  			echo "health monitor event daemon is stopped ..."
			fi
		fi
  	fi
  	;;
  *)
	echo "Usage: hmad {start|stop|status}"
	exit 1
esac

exit 0

