#!/bin/sh
#
#
#       OCManager Remote Host Bus Adapter Manager
#
#        COPYRIGHT 2007-2012, EMULEX CORPORATION
#        3333 Susan Street, Costa Mesa, CA 92626                           
#                                                                      
# All rights reserved.  This computer program and related documentation 
# is protected by copyright  and distributed under licenses restricting 
# its use,  copying,  distribution  and decompilation.    This computer 
# program  and its  documentation  are CONFIDENTIAL  and a TRADE SECRET 
# of EMULEX CORPORATION.   The receipt or  possession of  this  program 
# or its documentation does not  convey rights to reproduce or disclose 
# its  contents,  or to  manufacture, use, or sell anything that it may 
# describe, in whole or in part,  without the specific  written consent 
# of  EMULEX CORPORATION.   Any reproduction  of  this program  without 
# the express  written  consent  of EMULEX  CORPORATION  is a violation 
# of the  copyright laws  and may  subject you to  criminal prosecution.
# 
# 
# RedHat config info
#
# chkconfig: 2345 99 10
# description: elxhbamgrd - Remote Management Client Service for OCManager
#
# Suse config info
#
#
### BEGIN INIT INFO
# Provides:       elxhbamgrd
# Required-Start: mili2d BE2SNMP
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Description:    Loads or unloads Emulex Remote Management Client Service for OCManager
### END INIT INFO
RETVAL=0
prog="OneCommand Manager Management Daemon"
prog_bin="elxhbamgrd"
prog_install_dir="usr/sbin/ocmanager"
progpath="/$prog_install_dir/$prog_bin"

# Source function library.
if [ -f /etc/init.d/functions ]; then
    # RHEL5
    . /etc/init.d/functions
    GO=daemon
    STATUS=status
    SHOW_STATUS=""
else
    # RHEL6, SLES10, and SLES11
    . /lib/lsb/init-functions
    rc_reset
    GO="start_daemon -f"
    STATUS=checkproc
    SHOW_STATUS="rc_status -v"
fi

start ()
{
    # Start daemon.
    echo -n $"Starting $prog: "
    $GO $progpath $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog_bin
    $SHOW_STATUS
    return $RETVAL
}
 
stop ()
{
    if [ -f /var/lock/subsys/$prog_bin ];then
        running=`ps -ef | grep $prog_bin | grep -v grep | grep -v stop`
        if [ -n "$running" ];then
            echo -n $"Stopping $prog"
            
            if [ -f /etc/SuSE-release ]; then
                if [ 11 -eq `grep VERSION /etc/SuSE-release | cut -d' ' -f3` ]; then
                    killall --wait -q "$progpath" 
                    killproc "$prog_bin"
                else
                    killproc "$prog_bin"
                fi
            elif [ -f /etc/redhat-release ]; then
                # Check if XenServer
                Server=`grep XenServer /etc/redhat-release | cut -d' ' -f1`
                CentOS=`grep CentOS /etc/redhat-release | cut -d' ' -f1`
                if [ "$Server" = "XenServer"  ]; then
                    killall --wait -q "$progpath" 
                elif [ -f /etc/oracle-release ]; then
                    killall --wait --quiet "$progpath" 
                elif [ "$CentOS" = "CentOS" ]; then
                    centos=`cat /etc/redhat-release | cut -d' ' -f2`
                    if [ "$centos" = "Linux" ];then    # CentOS 6.x
                        killall --wait -q "$progpath" 
                    else                               # CentOS 5.x
                        killproc "$prog_bin"
                    fi
                elif [ 6 -eq `cat /etc/redhat-release | cut -d' ' -f7 | cut -d. -f1` ]; then
                    killall --wait -q "$progpath" 
                else
                    killproc "$prog_bin"
                fi
            else
                killproc "$prog_bin"
            fi
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog_bin
            $SHOW_STATUS
        else
            rm -f /var/lock/subsys/$prog_bin
        fi
        if [ -f /var/lock/subsys/$prog_bin ]; then
            rm -f /var/lock/subsys/$prog_bin
        fi
    fi
    return $RETVAL
}


# See how we were called.
case "$1" in
start)
    running=`ps -ef | grep $prog_bin | grep -v grep | grep -v start`
    if [ -f /var/lock/subsys/$prog_bin ]; then
        if [ -z "$running" ];then
            start
        else
            echo "$prog already running"
        fi
    else
        start
    fi
    ;;
stop)
    stop
    ;;
status)
    $STATUS $prog_bin
    $SHOW_STATUS
    ;;
restart)
    if [ -f /var/lock/subsys/$prog_bin ]; then
        stop
    fi
    start
    RETVAL=$?
    ;;
condrestart)
    if [ -f /var/lock/subsys/$prog_bin ]; then
        stop
        start
        RETVAL=$?
    fi
    ;;
*)
    echo $"Usage: $0 {start|stop|status}"
    exit 1
esac

exit $RETVAL
