#!/bin/sh
#
#
#       OCManager Remote Host Bus Adapter Manager
#
#        COPYRIGHT 2007-2014, 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: ELXSNMP
# 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"
lock_dir="var/lock/subsys"

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

start ()
{
    flag_controller=0;
    controller_list="19A2:* 10DF:*";
    
    for controller in $controller_list
    do
        FLAG_CHIP=`lspci -d $controller | awk -F " " '{print $1}' | head -1`
        if [[ -n $FLAG_CHIP ]]
        then
            flag_controller=1;
        fi
    done
    
    if [ $flag_controller = 0 ]
    then
        #Bug 140778 - silently succeed
        RETVAL=0
        #echo
        #echo "Emulex HBA/UCNAs not found."
        exit $RETVAL;
    fi
    
    # Start daemon.
    echo -n $"Starting $prog: "
    $GO $progpath $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /$lock_dir/$prog_bin
    $SHOW_STATUS
    return $RETVAL
}
 
stop ()
{
    if [ -f /$lock_dir/$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
                killall --wait -q "$progpath"
                killproc "$prog_bin"
            elif [ -f /etc/redhat-release ]; then
                # Check if XenServer
                Server=`grep XenServer /etc/redhat-release | cut -d' ' -f1`
                rhel_major_version=`cat /etc/redhat-release | awk -F'.' '{print $1}' | awk '{print $NF}'`
                
                if [ "$Server" = "XenServer"  ]; then
                    killall --wait -q "$progpath" 
                elif [ -f /etc/oracle-release ]; then
                    killall --wait --quiet "$progpath" 
                elif [ "$rhel_major_version" = "5" ]; then
                    killproc "$prog_bin"
                elif [ "$rhel_major_version" = "6" ]; then
                    killall --wait -q "$progpath"
                elif [ "$rhel_major_version" = "7" ]; then
                    killall --wait -q "$progpath"
                else
                    # rhel_major_version_7
                    killall --wait -q "$progpath"
                fi
            else
                killproc "$prog_bin"
            fi
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && rm -f /$lock_dir/$prog_bin
            $SHOW_STATUS
        else
            rm -f /$lock_dir/$prog_bin
        fi
        if [ -f /$lock_dir/$prog_bin ]; then
            rm -f /$lock_dir/$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 /$lock_dir/$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 /$lock_dir/$prog_bin ]; then
        stop
    fi
    start
    RETVAL=$?
    ;;
condrestart)
    if [ -f /$lock_dir/$prog_bin ]; then
        stop
        start
        RETVAL=$?
    fi
    ;;
*)
    echo $"Usage: $0 {start|stop|status}"
    exit 1
esac

exit $RETVAL
