#!/bin/bash
# 
# Copyright 2003-2016 Avago Technologies. All rights reserved.
# 
# 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 OCManager GUI        |  N  |  Y  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Remove Semaphore dir      |  N  |  Y  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|
# | Stop SNMP daemon          |  N  |  N  |  N  |  N  |  Y   |
# |---------------------------|-----|-----|-----|-----|------|

discoveryDaemonStopped=0
sles_major_version="0"
systemd_elxhbamgr=etc/systemd/system/elxhbamgr.service
systemd_elxdisco=etc/systemd/system/elxdiscovery.service
systemd_elxsnmp=etc/systemd/system/elxsnmp.service
init_elxhbamgr=etc/init.d/elxhbamgrd
init_elxdisco=etc/init.d/elxdiscoveryd
init_elxsnmp=etc/init.d/elxsnmpd

# 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
        if [ -f /${systemd_elxhbamgr} ]; then
            echo "Stopping elxhbamgrd (via systemctl):"
            systemctl stop elxhbamgr.service
        elif [ -x /${init_elxhbamgr} ]; then
            /${init_elxhbamgr} stop
        else
            echo "Unable to stop elxhbamgrd!"
        fi
    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 /${systemd_elxdisco} ]; then
            echo "Stopping elxdiscoveryd (via systemctl):"
            systemctl stop elxdiscovery.service
        elif [ -x /${init_elxdisco} ]; then
            /${init_elxdisco} 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

# if no arguments were used
if [ $# -eq 0 ]; then
    #  stop the SNMP Daemon
    ppid=`ps -ea | grep '[BE2|ELX]SNMP' | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
    if [ -f /${systemd_elxsnmp} ]; then
        echo "Stopping elxsnmpd (via systemctl):"
        systemctl stop elxsnmp.service
    elif [ -x /${init_elxsnmp} ]; then
        if [ ! -z "$ppid" ]; then
            /${init_elxsnmp} stop
        else
            /${init_elxsnmp} stop > /dev/null 2>&1
        fi
    fi

    if [ "$platform_os" = "Linux" ]; then
        # 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
        # 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
