#!/bin/bash
# Copyright 2002 Compaq Information Technologies Group, L.P.
#
# cmastor init file for Storage Agents
#
# See "man chkconfig" for information on next two lines (Red Hat only)
# chkconfig: - 99 1
# description: Storage Agents - Linux
#
# See "man insserv" for information on next eight lines (SuSE only)
### BEGIN INIT INFO
# Provides:            cmastor 
# Required-Start:      snmp hpasm
# Required-Stop:
# Default-Start:       2 3 4 5
# Default-Stop:        0 1 6
# Description:         starts cmastor (Storage Agents)
### END INIT INFO
#
# Other HP Insight Management Agents for Servers
#    cmafdtn : Foundation Agents - Linux
#    cmasvr  : Server Agents - Linux
#    cmanic  : NIC Agents - Linux

OCS=/opt/compaq/storage
. $OCS/etc/cmalib

NAME="Storage Agents"
ETCROOT="$OCS/etc"
PNAME="cmastor"
PNAMES="cmastorpeerd cmaeventd cmaidad cmafcad cmaided cmascsid"
USAGE="Usage: $PNAME {start|stop|restart|status} [cmastorpeerd|cmaeventd|cmaidad|cmafcad|cmaided|cmascsid]..."

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

OPCODE=$1
shift
AGENTS=$*

for i in $AGENTS; do
  FOUND=0
  for j in $PNAMES; do
    if [ $i = $j ]; then
      FOUND=1
      break;
    fi
  done
  if [ $FOUND = "0" ]; then
    cmausage
    exit 1
  fi
done

#Check if either rpm is installed

if [ ! -f /etc/init.d/hpasm ]; then
    if [ ! -f /etc/init.d/cmafdtn ]; then
            echo "Either cmafdtn or hpasm rpm should be installed"
    fi
fi

set_vendor_version

# echo "OPCODE = $OPCODE, AGENTS = $AGENTS"
# echo "VENDOR = $VENDOR"

cmastor_start()
{
	local AGENTS="$1"
	local NAME="$2"
	local PNAME="$3"
	local PNAMES="$4"
	local ETCROOT="$5"
	local VARLOCKSUBSYS="$6"	
	if [ -z "$AGENTS" ]; then #start all agents
		cmaecho "Starting $NAME ($PNAME): All agents"
		for AGENT in $PNAMES; do
			[ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT start
		done
	else
		cmaecho "Starting $NAME ($PNAME): $AGENTS"
		for AGENT in $AGENTS; do
			[ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT start
		done
	fi
	[ "$VARLOCKSUBSYS" = "1" ] && touch /var/lock/subsys/$PNAME
	cmaecho
}

cmastor_stop()
{
	local AGENTS="$1"
	local NAME="$2"
	local PNAME="$3"
	local PNAMES="$4"
	local ETCROOT="$5"
	local VARLOCKSUBSYS="$6"	
	if [ -z "$AGENTS" ]; then #stop all agents
		cmaecho "Shutting down $NAME ($PNAME): All agents"
		for AGENT in $PNAMES; do
			AgentExist=`ps -ef | grep $AGENT`
			if [ "$AgentExist" != "" ]; then
				 [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT stop
			fi
		done
	else
		cmaecho "Shutting down $NAME ($PNAME): $AGENTS"
		for AGENT in $AGENTS; do
			AgentExist=`ps -ef | grep $AGENT`
			if [ "$AgentExist" != "" ]; then
				 [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT stop
			fi
		done
	fi
	if  [ "$VARLOCKSUBSYS" = "1" ]; then
		REMOVELOCK=1
		for AGENT in $PNAMES; do
			pidlist=`pidof -o $$ $AGENT`
			if [ ! -z "$pidlist" ]; then
				REMOVELOCK=0
				break;
			fi
		done
		[ $REMOVELOCK = "1" ] && rm -f /var/lock/subsys/$PNAME
	fi
	cmaecho
}

cmastor_restart()
{
	local AGENTS="$1"
	local NAME="$2"
	local PNAME="$3"
	local PNAMES="$4"
	local ETCROOT="$5"
	if [ -z "$AGENTS" ]; then #restart all agents
		cmaecho "Re-starting $NAME ($PNAME): All agents"
		for AGENT in $PNAMES; do
			AgentExist=`ps -ef | grep $AGENT`
			if [ "$AgentExist" != "" ]; then
				 [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT restart
			else
				 [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT start                      
			fi
		done
	else
		cmaecho "Re-starting $NAME ($PNAME): $AGENTS"
		for AGENT in $AGENTS; do
			AgentExist=`ps -ef | grep $AGENT`
			if [ "$AgentExist" != "" ]; then
				 [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT restart
			else
				 [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT start                      
			fi
		done
	fi
	cmaecho
}

cmastor_status()
{
	local AGENTS="$1"
	local PNAMES="$2"
	local ETCROOT="$3"
	if [ -z "$AGENTS" ]; then #status all agents
		for AGENT in $PNAMES; do
			[ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT status
		done
	else
		for AGENT in $AGENTS; do
			[ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT status
		done
	fi
	RETVAL=$?
	return $RETVAL
}

[ -d /var/lock/subsys ] && VARLOCKSUBSYS=1 || VARLOCKSUBSYS=0

case "$VENDOR" in
  RedHat|VMware|SuSE|UnitedLinux)
        RETVAL=0
        PATH=$OCS/bin:$PATH
        case "$OPCODE" in
          start) cmastor_start "$AGENTS" "$NAME" "$PNAME" "$PNAMES" \
		"$ETCROOT" "$VARLOCKSUBSYS"
		RETVAL=$? ;; 
          stop) cmastor_stop "$AGENTS" "$NAME" "$PNAME" "$PNAMES" \
		"$ETCROOT" "$VARLOCKSUBSYS"
		RETVAL=$? ;; 
          restart) cmastor_restart "$AGENTS" "$NAME" "$PNAME" "$PNAMES" "$ETCROOT" ;; 
          status) cmastor_status "$AGENTS" "$PNAMES" "$ETCROOT" ; RETVAL=$? ;;
          *) cmausage ; exit 1 ;;
        esac
        exit $RETVAL
        ;;
     *)
        cmaecho "ERROR: Can't identify Linux OS vendor $VENDOR"
        exit 1
esac
