#! /bin/bash
#
# elxmilid     Start/Stop Mili Daemon
#
# chkconfig: 2345 90 60
# description: mili2d is the service required to manage \
#              Emulex OneConnect hardware.
# processname: mili2d

# Source function library.
. /etc/init.d/functions

# See how we were called.
prog_name="OneCommand MILI Management Daemon"
prog_bin="/usr/sbin/ocmanager/mili2d"
lock_file="/var/lock/subsys/mili2d"
mili_status=0

checkdriverstatus() {
    flag_benet=0;
    flag_iscsi=0;
    flag_controller=0;
    controller_list="19A2:* 10DF:E220 10DF:E228 10DF:0720 10DF:0728";
    
    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
        mili_status=0
        #echo
        #echo "Emulex OneConnect not found."
        exit $mili_status;
    fi
    
    lsmod | grep be2net > /tmp/grep.tmp
    if [ $? -eq 1 ]
    then
        flag_benet=1;
    fi
    
    lsmod | grep be2iscsi > /tmp/grep.tmp
    if [ $? -eq 1 ]
    then
        flag_iscsi=1;
    fi
    
    if [ $flag_benet = 1 ] && [ $flag_iscsi = 1 ] && [[ "$1" -eq "1" ]]; then
        # neither the iSCSI or NIC drivers are loaded
        # echo "Warning: be2iscsi and be2net drivers are not loaded."
        # do not start the daemon
        mili_status=1
        exit $mili_status
    fi
    rm -rf /tmp/grep.tmp
}

createdrivernodes() {
    rm -f /dev/be2iscsi_*
    rm -f /dev/be2_nic*

    iSCSI_COUNT=0
    for k in `cat /proc/devices | grep be2iscsi_ | awk -F " " '{print $1}'`
    do
        iSCSI_COUNT=`expr "$iSCSI_COUNT" + 1`
    done
    j=0
    while [ $j -lt $iSCSI_COUNT ]
    do
        iSCSI_MAJOR=`cat /proc/devices | grep be2iscsi_"$j" | awk -F " " '{print $1}'`
        if [ ! -z $iSCSI_MAJOR ] ; then
            mknod /dev/be2iscsi_"$j" c $iSCSI_MAJOR 0;
        fi
        j=`expr "$j" + 1`
    done

    NIC_COUNT=0
    k=0 
    j=0
    for k in `ls /sys/class/net`
    do
        if [[ $k != "lo" && $k != "sit0" ]]; then
            if `ethtool -i $k | grep be2net > /dev/null 2>&1`
            then
                NIC_COUNT=`expr "$NIC_COUNT" + 1`
            fi
        fi
    done
    while [ $j -lt $NIC_COUNT ]
    do
        NIC_MAJOR=`cat /proc/devices | grep be2nic | awk -F " " '{print $1}'`
        if [ $j -lt $NIC_COUNT ]; then
            mknod /dev/be2_nic"$j" c $NIC_MAJOR $j;
        fi
        j=`expr "$j" + 1`
    done

}

start() {
    if [ -e $lock_file ]; then
        echo "$prog_name already running"
        exit 0
    else
        checkdriverstatus "1"
        if [ $mili_status -eq 0 ]; then
            echo -n $"Starting $prog_name: "
            daemon $prog_bin
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && touch $lock_file
            sleep 2
            return $RETVAL
        fi
    fi
}

stop() {
    echo -n $"Stopping $prog_name: "
    killproc $prog_bin
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $lock_file
    return $RETVAL
}	

rhstatus() {
    echo -n $prog_name ""
    status $prog_bin
}	

restart() {
    stop
    start
}	

reload() {
    echo -n $"Reloading $0 configuration: "
    killproc $prog_name -HUP
    RETVAL=$?
    echo
    return $RETVAL
}	

if [ $# -gt 1 ];then
    echo "Syntax Error: Only 1 argument is allowed."
    exit 1
fi

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    status)
        rhstatus
        ;;
    condrestart)
        [ -f $lock_file ] && restart || :
        ;;
    *)
        echo "Error: Invalid Argument."
        echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
        exit 1
esac
