#!/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.
# 
# 
# This script may be executed as follows:
#
#    stop_ocmanager [-r | -n | -g | -w ]
#
# The following table describes the affect each of these arguments
# has on the the various OCManager related components.
#
#                             |-----|-----|-----|-----|------|
#                             | -r  | -n  | -g  | -w  | none |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop Web Launch server    |  Y  |  Y  |  N  |  Y  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop Discovery daemon     |  N  |  Y  |  Y  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop elxhbamgrd (rmserver)|  Y  |  N  |  Y  |  Y  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | stop fcauth daemon        |  N  |  N  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop OCManager GUI        |  N  |  Y  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Remove Semaphore dir      |  N  |  Y  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop MILI daemon          |  N  |  N  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop SNMP daemon          |  N  |  N  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|

discoveryDaemonStopped=0
sles_major_version="0"

# Stop all web launch daemons
platform_os=`uname -s`

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


if [ "$1" != "-g" ]; then
    if [ "$platform_os" = "Linux" ];then
        # CR 27336 for core kit, don't call out
        if [ -f "/${INSTALL_DIR}/stop_weblaunch" ]; then
            /${INSTALL_DIR}/stop_weblaunch -all
        fi
    else
        /${INSTALL_DIR}/stop_weblaunch > /dev/null 2>&1
    fi
fi


# Kill parent elxhbamgrd
# If -n argument was passed to script, then don't kill the elxhbamgrd
if [ "$1" != "-n" ]; then
  if [ "$platform_os" = "Linux" ];then
    /etc/init.d/elxhbamgrd stop
  else
    # Solaris
    # Temporarily (until the next reboot) stop the elxhbamgrd daemons
    svcadm disable -st application/elxhbamgrd
  fi
fi

# If request was to stop elxhbamgrd only, then exit now. 
if [ "$1" = "-r" ] || [ "$1" = "-w" ]; then
    exit
fi


# Kill OneCommand Manager GUI
if [ "$1" != "-g" ]; then
    #  Get pid of script that started the OneCommand Manager GUI
    script_pid=`ps -eaf | grep ocmanager | grep "/bin/bash" | grep -v grep | head -n 1 | awk '{ print $2 }'`
    #  echo "script pid of ocmanager is $script_pid"
    if [ "$script_pid" != "" ];then
        gui_pid=`ps -eaf | grep java | grep -v grep | grep "OCManager.jar" | awk '{ print $2 }'`
        if [ "$gui_pid" != "" ];then
            echo "Stopping the OneCommand Manager GUI"
            if [ "$platform_os" = "Linux" ];then
                 kill -9 $gui_pid > /dev/null 2>&1
            else
                 kill $gui_pid > /dev/null 2>&1
            fi
        fi
    fi
fi

if [ "$platform_os" = "Linux" ];then
    if [ "$1" != "-r" ] && [ "$1" != "-w" ]; then
        # Kill parent elxdiscoveryd
        if [ -f /etc/init.d/elxdiscoveryd ]; then
            /etc/init.d/elxdiscoveryd stop
        fi
    fi
else
    # Solaris
    # Stop the start-elxdiscoveryd service.  No need to restart it in start_ocmanager 
    # since this service doesn't do anything after boot.
    svcadm disable -t application/start-elxdiscoveryd > /dev/null 2>&1
    # Temporarily (until the next reboot) stop the elxdiscoveryd daemon
    svcadm disable -st application/elxdiscoveryd > /dev/null 2>&1
fi

# remove HBA semaphore (POSIX named semaphores)
if [ "$platform_os" = "Linux" ];then
    if [ "$1" != "-g" ]; then
        rm -f /dev/shm/sem.ElxHbaSem*
    fi
else
    /opt/ELXocm/freesem > /dev/null 2>&1
fi


# get sles11 indicator
if [ -f /etc/SuSE-release ]; then
    sles_major_version=`cat /etc/SuSE-release | grep VERSION | awk '{ print $3 }'`
fi

# Kill fcauth daemon
# if no arguments were used
if [ $# -eq 0 ];then
    if [ -x "/etc/init.d/fcauthd" ]; then
       if [ "$sles_major_version" -ne 11 ];then
          if [ "$1" != "-g" ]; then
              ppid=`ps -ea | grep fcauth | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
              if [ ! -z "$ppid" ];then
                 /etc/init.d/fcauthd stop
              fi
          fi
       fi
    fi
fi

# if no arguments were used
if [ $# -eq 0 ];then
    #  stop the SNMP Daemon
    ppid=`ps -ea | grep BE2SNMP | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
    if [ ! -z "$ppid" ];then
        /etc/init.d/elxsnmpd stop
    else
        /etc/init.d/elxsnmpd stop > /dev/null 2>&1
    fi

    # Kill the MILI Daemon
    if [ "$platform_os" = "Linux" ];then
        ppid=`ps -ea | grep mili2d | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
        if [ ! -z "$ppid" ];then
            /etc/init.d/elxmilid stop
        else
            /etc/init.d/elxmilid stop > /dev/null 2>&1
        fi
        # stop the ocv sensor daemons if necessary
        if [ -x "/usr/local/elx/ocvsensor/stop-sensor" ]; then
            /usr/local/elx/ocvsensor/stop-sensor
        fi
    else
        # Solaris
        # Temporarily (until the next reboot) stop the elxmilid process
        svcadm disable -st application/elxmilid
        # stop the ocv sensor daemons if necessary
        if [ -x "/opt/elx/OCVsensor/stop-sensor" ]; then
            /opt/elx/OCVsensor/stop-sensor
        fi
    fi
fi

exit 0
