#!/bin/sh
#
#
#       HBAnyware Remote Host Bus Adapter Manager
#
#        COPYRIGHT 2007-2010, 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: elxdiscoveryd - Discovery Service for OCManager
#
# Suse config info
#
#
### BEGIN INIT INFO
# Provides:       elxdiscoveryd
# 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 Discovery Service for OCManager
### END INIT INFO
#
#
RETVAL=0
prog="OneCommand Manager Discovery Daemon"
prog_bin="elxdiscoveryd"
prog_install_dir="usr/sbin/ocmanager"
progpath="/$prog_install_dir/$prog_bin"

# Source function library.
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    GO=daemon
    STATUS=status
    SHOW_STATUS=""
else
    . /etc/rc.status
    rc_reset
    GO="start_daemon -f"
    STATUS=checkproc
    SHOW_STATUS="rc_status -v"
fi

start ()
{
    if [ -f /etc/emulexDiscConfig ]; then
        /bin/grep "AUTOSTART:true" /etc/emulexDiscConfig > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            # Start daemon.
            echo -n $"Starting $prog: "
            $GO $progpath $OPTIONS
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog_bin
            $SHOW_STATUS
            return $RETVAL
        fi
    fi
}
 
stop ()
{
    running=`ps -ef | grep elxdisco | grep -v grep | grep -v stop | grep -v "init.d"`
    if [ -n "$running" ]; then
        echo -n $"Stopping $prog"
        killproc "$prog_bin"
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog_bin
        $SHOW_STATUS
    fi
    if [ -f /var/lock/subsys/$prog_bin ]; then
        rm -f /var/lock/subsys/$prog_bin
    fi
    return $RETVAL
}


forcestart ()
{
    # Start daemon.
    echo -n $"Starting $prog: "
    $GO $progpath $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog_bin
    $SHOW_STATUS
    return $RETVAL
}


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

exit $RETVAL
