#!/bin/bash
# 
# Copyright 2003-2016 Avago Technologies. All rights reserved.
# 

platform_os=`uname -s`
systemd_elxhbamgr=etc/systemd/system/elxhbamgr.service
systemd_elxsnmp=etc/systemd/system/elxsnmp.service
init_elxhbamgr=etc/init.d/elxhbamgrd
init_elxsnmp=etc/init.d/elxsnmpd

if [ "$platform_os" = "Linux" ]; then
    INSTALL_DIR="usr/sbin/ocmanager"
else
    INSTALL_DIR="opt/ELXocm"
fi

if [ "$platform_os" = "Linux" ]; then
    
    # for rhel5 and SLES10 SP1 installs, lpfcdfc does not exist
    ioctl_driver="lpfcdfc"
    
    let vmware=`rpm -q vmware-release 2>/dev/null 1>/dev/null ; echo $?`
    let vmware820=`rpm -qa |grep -i vmware-esx-drivers-scsi-lpfc820 2>/dev/null 1>/dev/null ; echo $?`
    
    if [ $vmware820 -eq 0 ] ; then
        ioctl_driver="lpfc820"
    elif [ $vmware -eq 0 ] ; then
        ioctl_driver="lpfc_740"
    else
        ioctl_driver="lpfc"
    fi
    
    if [[ `lsmod | grep be2net > /dev/dnull 2>&1` -eq 0 ]] || [[ `lsmod | grep be2iscsi > /dev/null 2>&1` -eq 0 ]]; then
        be2="be2drivers"
    fi	

    #  Start the server daemon if module loaded
    if [[ $vmware -eq 0 || $vmware820 -eq 0 ]]; then
        lpfcdfc_loaded=`vmkload_mod -l | awk '{ print $1 }' | grep "$ioctl_driver"`
    else
        lpfcdfc_loaded=`lsmod | awk '{ print $1 }' | grep "$ioctl_driver"`
    fi
    
    if [ "$1" != "-w" ]; then
        # start the MILI Daemon
        if [ "$platform_os" = "Linux" ];then
            elxtmp=0
        else
            # Solaris
            svcadm enable -st application/elxmilid
        fi
        
        # start the SNMP Daemon
        if [ -f /${systemd_elxsnmp} ]; then
            echo "Starting elxsnmpd (via systemctl):"
            systemctl start elxsnmp.service
        elif [ -x /${init_elxsnmp} ]; then
            /${init_elxsnmp} start
        fi
        
        if [[ $vmware820 -eq 0 ]]; then
            # Start the Event Daemon if not already started
            if [[ `ps -eaf | grep elxeventd | grep -v grep | wc -l`  -eq 0 ]]; then
                /etc/init.d/elxeventd start
            fi
        fi
   
        # start the ocv sensor daemons if necessary
        if [ -x "/usr/local/elx/ocvsensor/start-sensor" ]; then
            /usr/local/elx/ocvsensor/start-sensor
        fi
    fi
    # start the OneCommand Management Server last
    if [ ! -z "$lpfcdfc_loaded" ] || [ ! -z "$be2" ];then
        if [ -f /${systemd_elxhbamgr} ]; then
            echo "Starting elxhbamgrd (via systemctl):"
            systemctl start elxhbamgr.service
        elif [ -x /${init_elxhbamgr} ]; then
            /${init_elxhbamgr} start
        else
            echo "Unable to start elxhbamgrd!"
        fi
    #else
        # do not try to start OCManager Remote Management daemon because driver not loaded
        # CR 83380; do not display message because this could be linux guest install on vmware
        #echo ""
        #echo "Not starting OCManager Remote Management daemon because $ioctl_driver driver module not loaded."
        #echo "Load $ioctl_driver driver module and retry."
    fi
else
    # [Solaris]
    if [ "$1" != "-w" ]; then
       # start the ocv sensor daemons if necessary
       if [ -x "/opt/elx/OCVsensor/start-sensor" ]; then
          /opt/elx/OCVsensor/start-sensor
       fi
    fi

    # Start the elxhbamgrd daemons
    svcadm enable -t application/elxhbamgrd
fi

# support for management mode changes
if [ "$1" = "-w" ]; then
    if [ -f "/${INSTALL_DIR}/start_weblaunch" ]; then
        /${INSTALL_DIR}/start_weblaunch > /dev/null 2>&1
    fi
fi


exit 0
