#!/bin/bash
#
#
#  COPYRIGHT 2003-2014, EMULEX CORPORATION
#  3333 Susan St., 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.
# 
# 
platform_os=`uname -s`

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
           if [ -f /etc/init.d/elxmilid ];then
               /etc/init.d/elxmilid start
           fi
       else
           # Solaris
           svcadm enable -st application/elxmilid
       fi
    
       # start the SNMP Daemon
       if [ -f /etc/init.d/elxsnmpd ];then
           /etc/init.d/elxsnmpd start
       fi
    
       # start the FC authentication Daemon, if it is not running
       ppid=`ps -eaf | grep fcauthd | grep -v grep | awk '{ print $2 }' | sort -n | head -1`
       if [ -z "$ppid" ];then
           if [ ! -z "$lpfcdfc_loaded" ]; then
               if [ -f /etc/init.d/fcauthd ]; then
                   /etc/init.d/fcauthd start
               fi
           fi
       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 /${INSTALL_DIR}/elxhbamgrd ]; then
            /etc/init.d/elxhbamgrd start
        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 mili2d and wait for the start routine to finish before continuing.
       svcadm enable -st application/elxmilid
 
       # 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
