#!/bin/bash

# (c) Copyright 2002-2005 Hewlett-Packard Development Company, L.P.
#
# See "man chkconfig" for information on next two lines (Red Hat only)
# chkconfig: 2345 91 1
# description: HP Lights-Out Drivers and Agents for Linux
#
#
# Following lines are in conformance with LSB 1.2 spec
### BEGIN INIT INFO
# Provides:            hprsm
# Required-Start:      snmp hpasm
# Required-Stop:
# Default-Start:       2 3 4 5
# Default-Stop:        0 1 6
# Description:         starts hprsm (HP Lights-Out Drivers and Agents)
### END INIT INFO

# Source function library
. /etc/rc.status

LOGFILE=/var/spool/compaq/cma.log
HPRSM_LOGFILE=/opt/compaq/hprsm/hprsm_boot.log
NAME="HP Lights-Out Drivers and Agents"
ETCROOT="/opt/compaq/server/etc"
PNAME="hprsm"
SNMPD="snmpd"
PNAMES="cpqci cpqrid cpqriisd cmasm2d cmarackd"
AGENTS="cpqriisd cmasm2d cmarackd"
EXPNAMES=`egrep "^exclude" /opt/compaq/cma.conf | cut -b 8- 2> /dev/null`
TAINT=`egrep "notaint" /opt/compaq/cma.conf 2> /dev/null`
USAGE="Usage: $PNAME {start|stop|restart|status} [cpqci|cpqrid|cpqriisd|cmasm2d|cmarackd]..."

rsmecho () {
	echo "  " $*
	echo "  " $* >>$LOGFILE 2>&1
}

rsmechon () {
	echo -n "  " $*
	echo -n "  " $* >>$LOGFILE 2>&1
}

log_msg() {
	echo $* >> $HPRSM_LOGFILE
}


rsmusage () {
	rsmecho $USAGE
}

if [ "$#" -lt 1 ]; then
	rsmusage
	exit 1
fi

#first of all, I do not want any file handles
#open other than 0, 1,2
for FILE in /proc/$$/fd/*; do
        FD=`basename $FILE`
	#exempt the standard file descriptor
        [ "$FD" = "0" ] && continue
        [ "$FD" = "1" ] && continue
        [ "$FD" = "2" ] && continue
	# this is a file to the script at hand
        [ "$FD" = "255" ] && continue
        LINKTO=`ls -o $FILE 2> /dev/null | awk '{print $10}'`
        if [ -e "$LINKTO" ]; then
#                echo "Closing $FD pointing to $LINKTO" 
		eval "exec $FD>&-"
        fi
done
# At this point, no input interaction is needed, 
# so, I shut stdin down
exec 0<&-	

OPCODE=$1
shift
TEMP=$*

if [ -z "$TEMP" ]; then
	TEMP=$PNAMES
elif [ "$TEMP" == "agents" ]; then
	TEMP=$AGENTS
else 
	if [ "$OPCODE" == "stop" ]; then
		if [ ! -z "`echo $* | grep cpqci`" ]; then
			TEMP="$TEMP cpqriisd cmasm2d cmarackd"
		fi
		if [ ! -z "`echo $* | grep cpqrid`" ]; then
			TEMP="$TEMP cmasm2d"
		fi
		if [ ! -z "`echo $* | grep cpqriisd`" ]; then
			TEMP="$TEMP cmarackd"
		fi
	fi
	if [ "$OPCODE" == "start" ]; then
		if [ ! -z "`echo $* | grep cmasm2d`" ]; then
			TEMP="$TEMP cpqrid cpqci"
		fi
		if [ ! -z "`echo $* | grep cmarackd`" ]; then
			TEMP="$TEMP cpqci cpqriisd"
		fi
		if [ ! -z "`echo $* | grep cpqriisd`" ]; then
			TEMP="$TEMP cpqci"
		fi
	fi
fi

AGENTS=""
RAGENTS=""
for i in $PNAMES; do
	if [ ! -z "`echo $TEMP| grep $i`" ]; then
		if [ -z "`echo $EXPNAMES | grep $i`" ]; then
			AGENTS="$AGENTS $i"
			RAGENTS="$i $RAGENTS"
		fi
	fi
done

check_taint() {
	if [ "$TAINT" = "notaint" ]; then
		rsmecho "Warning: Can not perform requested operation for $PNAME!"
		rsmecho "Please run /etc/init.d/hpasm reconfigure"
		rsmecho "to allow kernel tainting."
		exit 1
	fi
}

start_daemon() {
        [ -f /sbin/$1 ] || return
        if [ -f /var/lock/subsys/$1 ]; then
        	checkproc /sbin/$1
        	if [ $? -eq 0 ]; then
                	rsmecho "Already started $1." 
                	return
		else
        		rm -f /var/lock/subsys/$1
		fi
        fi
	CIUP=`lsmod | fgrep cpqci`
	if [ ${#CIUP} -eq 0 ]; then
		rsmecho "$1 requires cpqci." 
		return
	else
        	rsmechon "Starting $1: "
        	startproc -u root -g root /sbin/$1 $2
        	rc_status
#		rc_status -v
		sleep 6
	fi
}

stop_daemon() {
        checkproc /sbin/$1 
       	if [ $? -ne 0 ]; then
               	rsmecho "Already stopped $1." 
               	return
	else
        	[ -f /sbin/$1 ] || return
        	rsmechon "Stopping $1: "
        	killproc -TERM /sbin/$1 2> /dev/null
        	rc_status -v
		sleep 6
        	case "$1" in
        	cpqriisd)
               		SHMID=`ipcs -m | grep 0x0000bbbd | cut -d ' ' -f 2`
               		if [ "$SHMID" != "" ]; then
               			ipcrm shm $SHMID 2> /dev/null > /dev/null
               	 	fi
               		;;
        	esac
        	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$1
        	return $RETVAL
	fi
}

start_svr_agent() {
	snmppid=`pidof $SNMPD`
	peerpid=`pidof cmapeerd`
	if [ -z "$snmppid" ]; then
		rsmecho "Can not start $1! ($SNMPD is not running)"
		RETVAL=1
	else
		if [ -z "$peerpid" ]; then
			rsmecho "WARNING: cmapeerd is not running;"
			rsmecho "without cmapeerd you will be"
			rsmecho "unable to access $1 MIB data."
		fi
		log_msg "Starting $1..."
		[ -x $ETCROOT/$1 ] && $ETCROOT/$1 start
	fi
}

stop_svr_agent() {
	log_msg "Stopping $1..."
	[ -x $ETCROOT/$1 ] && $ETCROOT/$1 stop
}

restart_svr_agent() {
	log_msg "Restarting $1..."
	[ -x $ETCROOT/$1 ] && $ETCROOT/$1 restart
}

status_svr_agent() {
	[ -x $ETCROOT/$1 ] && $ETCROOT/$1 status
}

start_driver() {
	RUNNING=`lsmod | fgrep $1`
	if [ ${#RUNNING} -ne 0 ]; then
               	rsmecho  "Already started $1." 
	else
        	rsmechon "Starting $1: " 
		sh /opt/compaq/hprsm/hprsm start silent $1
        	rc_status
        	rc_status -v
	fi
}

stop_driver() {
	RUNNING=`lsmod | fgrep $1`
	if [ ${#RUNNING} -ne 0 ]; then
        	rsmechon $"Stopping $1: " 
        	sh /opt/compaq/hprsm/hprsm stop silent $1
        	rc_status
        	rc_status -v
	else
               	rsmecho "Already stopped $1." 
	fi
}

status_driver() {
	RUNNING=`lsmod | fgrep $1`
	if [ ${#RUNNING} -ne 0 ]; then
		echo "$1 is loaded"
	else
		echo "$1 is not loaded"
	fi
}

start_case () {
	echo "Starting $NAME ($PNAME): $AGENTS"
	for AGENT in $AGENTS; do
		case "$AGENT" in
			cpqrid|cpqci)
				start_driver $AGENT
				;;
			cpqriisd)
				start_daemon $AGENT -F
				;;
			cmasm2d|cmarackd)
		    		start_svr_agent $AGENT 
				;;
		esac
	done
	rsmecho
}

stop_case () {
	echo "Stopping $NAME ($PNAME): $RAGENTS"
	for AGENT in $RAGENTS; do
		case "$AGENT" in
			cpqrid|cpqci)
				stop_driver $AGENT
				;;
			cpqriisd)
				stop_daemon $AGENT -F
				;;
			cmasm2d|cmarackd)
		    		stop_svr_agent $AGENT 
				;;
		esac
	done
	rsmecho
}

status_case () {
	echo "Status of $NAME ($PNAME): $AGENTS"
	for AGENT in $AGENTS; do
		case "$AGENT" in
			cpqrid|cpqci)
				status_driver $AGENT
				;;
			cpqriisd)
				/sbin/checkproc /sbin/$AGENT && echo "$AGENT is loaded" || echo "$AGENT is not loaded"
				;;
			cmasm2d|cmarackd)
		    		status_svr_agent $AGENT 
				;;
		esac
	done
}

RETVAL=0
PATH=/opt/compaq/server/bin:$PATH

case "$OPCODE" in
  start)
	check_taint
	start_case
	;;
  stop)
	check_taint
	stop_case
	;;
  restart)
	check_taint
	stop_case
	start_case
	;;
  status)
	status_case
	RETVAL=$?
	;;
  *)
	rsmusage
	exit 1
esac

exit $RETVAL
