#!/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

LOGFILE=/var/spool/compaq/cma.log
NAME="Storage Agents"
ETCROOT="/opt/compaq/storage/etc"
PNAME="cmastor"
PNAMES="cmastorpeerd cmaeventd cmaidad cmafcad cmaided cmascsid"
USAGE="Usage: $PNAME {start|stop|restart|status} [cmastorpeerd|cmaeventd|cmaidad|cmafcad|cmaided|cmascsid]..."

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

cmausage () {
  cmaecho $USAGE
}


vendorversion() {
  # $1 contains the ENV for Vendor
  # $2 contains the ENV for Version
  # Return 0 when succeed
  if [ ! -f /etc/redhat-release -a ! -f /etc/UnitedLinux-release -a ! -f /etc/issue ]; then
    echo "Release file does not exist!" 1>&2
    return 1
  fi
  #echo "ARG1=$1"
  #echo "ARG2=$2"
  if [ -z "$1" -o -z "$2" ]; then
     return 2
  fi
  egrep "Red *Hat " /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
  else
    egrep "UnitedLinux " /etc/UnitedLinux-release >/dev/null 2>&1
    if [ "$?" = 0 ]; then
      eval $1="UnitedLinux"
    else
      egrep "SuSE " /etc/issue >/dev/null 2>&1
      if [ "$?" = 0 ]; then
        eval $1="SuSE"
      fi
    fi
  fi
  egrep "Red *Hat .*Advanced Server.* 2\.1AS" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="AS2.1"
    return 0
  fi
  egrep "Red *Hat .*Enterprise Linux.* 2\.1" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="EL2.1"
    return 0
  fi
  egrep "Red *Hat .*Enterprise Linux.* 3" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="EL3"
    return 0
  fi
  egrep "Red *Hat .* 9\.0 |Red *Hat .* 8\.0\.[0-9]" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="9.0"
    return 0
  fi
  egrep "Red *Hat .* 8\.0 |Red *Hat .* 7\.3\.[0-9]" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="8.0"
    return 0
  fi
  egrep "Red *Hat .* 7\.3 |Red *Hat .* 7\.2\.[0-9]" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.3"
    return 0
  fi
  egrep "Red *Hat .* 7\.2 |Red *Hat .* 7\.1\.[0-9]" /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.2"
    return 0
  fi
  grep "Red *Hat .* 7\.1 " /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.1"
    return 0
  fi
  grep "Red *Hat .* 6\.2 " /etc/redhat-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="6.2"
    return 0
  fi
  grep "SLES-7 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="SLES-7"
    return 0
  fi
  grep "SLES.8 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="SLES-8"
    return 0
  fi
  grep "SuSE .* 7\.2 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="7.2"
    return 0
  fi
  grep "SuSE .* 7\.0 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="7.0"
    return 0
  fi
  grep "UnitedLinux.* 1\.0" /etc/UnitedLinux-release >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="UnitedLinux"
    eval $2="1.0"
    return 0
  fi
  grep "VMware" /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="VMware"
    eval $2="2.0"
    return 0
  fi
  return 9
}

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


# Identify OS Vendor
VENDOR="Unknown"
vendorversion "VENDOR" "VERSION"
if [ "$VENDOR" != "RedHat" -a "$VENDOR" != "SuSE" -a "$VENDOR" != "UnitedLinux" -a "$VENDOR" != "VMware" ]; then
  echo "ERROR: the Linux OS running on your system is not supported!"
  exit 1
fi

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

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

case "$VENDOR" in
  RedHat)
        RETVAL=0
        # source function library
        . /etc/rc.d/init.d/functions
        PATH=/opt/compaq/storage/bin:$PATH
        case "$OPCODE" in
          start)
                  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
        	;;
          stop)
                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
        	;;
          restart)
                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
                ;;
          status)
                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=$?
                ;;
          *)
        	cmausage
        	exit 1
        esac

        exit $RETVAL
        ;;
  VMware)
        RETVAL=0
        # source function library
        . /etc/rc.d/init.d/functions
        PATH=/opt/compaq/storage/bin:$PATH
        case "$OPCODE" in
          start)
                  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
        	;;
          stop)
                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
        	;;
          restart)
                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
                ;;
          status)
                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=$?
                ;;
          *)
        	cmausage
        	exit 1
        esac

        exit $RETVAL
        ;;
  SuSE|UnitedLinux)
        RETVAL=0
        [ -f /etc/rc.config ] && . /etc/rc.config
        PATH=/opt/compaq/storage/bin:$PATH
        case "$OPCODE" in
          start)
                  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
        	;;
          stop)
                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
        	;;
          restart)
                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
                ;;
          status)
                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=$?
                ;;
          *)
        	cmausage
        	exit 1
        esac

        exit $RETVAL
        ;;
     *)
        cmaecho "ERROR: Can't identify Linux OS vendor $VENDOR"
        exit 1
esac
