#!/bin/bash 
#
# 
# description: hostagent provides an easy way of starting and stopping the
#              navisphere agent at bootup time.
# processname: agent
# pidfile: /var/log/agent.pid
# config: /etc/Unisphere/agent.config
# chkconfig: 345 25 35
#
#
# for SLES 8.0
### BEGIN INIT INFO
#   Provides: hostagent
#   Required-Start: $all
#   Should-Start: multipathd
#   Required-Stop: $all
#   Should-Stop: multipathd
#   Default-Start: 3 4 5
#   Default-Stop: 0 1 2 6
#   Description: Navisphere agent
### END INIT INFO
AGENTCFG=/etc/Unisphere/agent.config
AGENTBIN=/opt/Unisphere/bin/hostagent

# source function library
# For RedHat
if [ -f "/etc/rc.d/init.d/functions" ]; then
	. /etc/rc.d/init.d/functions

	StartCommand="daemon $AGENTBIN -f $AGENTCFG"
	StopCommand="killproc $AGENTBIN"
	StatusCommand="status $AGENTBIN"
fi
# For SuSE
if [ -f "/etc/rc.status" ]; then
	. /etc/rc.status

# For SUSE all versions
grep -q "VERSION =" /etc/SuSE-release
        if [ $? -eq 0 ]; then
          sleep 5
        fi
# Required For 64 bit machine
        UNAME_M=$(uname -m)
        IA64="ia64"
        X86_64="x86_64"
        SUSEREL_9="SL9.0"
        # This fix needs to be implemented for SLES 10 & 11 as well.
        #Hence checking for VERSION 10 & 11 as well and setting
        # OS_REL = SL9.0 if it is SLES 11 or 10 or 9
        grep -q "VERSION = \(9\|10\|11\)" /etc/SuSE-release
        if [ $? -eq 0 ]; then
           OS_REL="SL9.0"
        else
           OS_REL="NSL9.0"
        fi
	StartCommand="startproc $AGENTBIN -f $AGENTCFG"
	StartCommand2="rc_status -v"
	StopCommand="killproc $AGENTBIN"
	StopCommand2="rc_status -v"
	StatusCommand="echo -n Navisphere Agent:"
	StatusCommand2="checkproc $AGENTBIN"
	StatusCommand3="rc_status -v"
fi

case "$1" in
  start)
	echo -n "Starting Navisphere agent: "

        # Make sure sg driver is loaded. If not load it in.
        lsmod|awk '{print $1}'|fgrep sg > /dev/null

        if [ $? -eq 1 ];then
                # not loaded. Load it
                modprobe sg
        fi

        if [ -f "$AGENTCFG" ]; then
	     # For SuSE
          if [ -f "/etc/rc.status" ]; then
             # Naviagnet is compiled in 32 bit machine, thats why SUSE Linux
	     # with 64 bit machine needs this modification
            if [ $UNAME_M == $IA64 -a $SUSEREL_9 == $OS_REL ]; then
	      PID=`/sbin/pidof $AGENTBIN`
	      if [ $PID ]; then
		_rc_status=1
	      else
                $StartCommand
		# Since startproc command is not reporting correct status
		PID=`/sbin/pidof $AGENTBIN`
	      fi
              $StartCommand2
            else
              # SUSE 32 bit machine 
                $StartCommand
                $StartCommand2
            fi
          else
            # For RedHat
              $StartCommand
	      $StartCommand2
          fi

        else
          echo "(No agent config file found.)"
	       
	fi

	echo
	;;
  stop)
	echo -n "Shutting down hostagent: "
        # For SuSE
        if [ -f "/etc/rc.status" ]; then
          if [ $UNAME_M == $IA64 -a $SUSEREL_9 == $OS_REL ]; then
            # For 64 Bit
	    # Since killproc is not reporting proper status
	    PID=`/sbin/pidof $AGENTBIN`
	    if [ $PID ]; then
              kill $PID
	    else
	      _rc_status=1
	    fi
            $StopCommand2
          else
            # For 32 Bit
	    $StopCommand
            $StopCommand2
          fi
        else
          # For RedHat
          $StopCommand
          $StopCommand2
        fi

	echo
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  status)
        # For SuSE
        if [ -f "/etc/rc.status" ]; then
          if [ $UNAME_M == $IA64 -a $SUSEREL_9 == $OS_REL ]; then
            # For 64 Bit
            $StatusCommand
	    PID=`/sbin/pidof $AGENTBIN`
	    if [ $PID ]; then
	      _rc_status=0
	    else
              _rc_status=3
	    fi
            $StatusCommand3
          else
            # For 32 Bit 
	    $StatusCommand
	    $StatusCommand2
	    $StatusCommand3
          fi
        else
          # For RedHat
 	  $StatusCommand
 	  $StatusCommand2
 	  $StatusCommand3
        fi

	;;
  *)
	echo "Usage: hostagent {start|stop|status|restart|reload}"
	exit 1
esac

