#!/bin/sh
#
#
#  COPYRIGHT 2003-2006, 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.

discoveryDaemonStopped=0

# Stop all web launch daemons
platform_os=`uname -s`
if [ "$1" != "-g" ]; then
   if [ "$platform_os" = "Linux" ];then
      #  cr 27336 for core kit, don't call out
      if [ -f "/usr/sbin/hbanyware/stop_weblaunch" ]; then
         /usr/sbin/hbanyware/stop_weblaunch
      fi
   else
      /opt/HBAnyware/stop_weblaunch
   fi
fi

# Kill parent elxhbamgr
# If -n argument was passed to script, then don't kill the elxhbamgr
if [ "$1" != "-n" ]; then
  ppid=`ps -ea | grep elxhbamg | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
  if [ ! -z "$ppid" ];then
        echo "Stopping HBAnyware HBA Management Server"
    kill $ppid > /dev/null 2>&1
    newppid="$ppid"
    trycount=0;
    while [ "$newppid" = "$ppid" ] && [ $trycount -lt  5 ]
    do
        sleep 1
        newppid=`ps -ea | grep elxhbamg | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
        trycount=`expr $trycount + 1`
    done
  fi

  # Kill any remaining child of elxhbamgr
  for ppid in `ps -ea | grep elxhbamg | grep -v grep | awk '{ print $1 }' | sort -n`
  do
	kill -9 $ppid > /dev/null 2>&1
  done
fi

# Kill HBAnyware and SSC GUIs
if [ "$1" != "-g" ]; then
   #  Get pid of script that started HBAnyware GUI
   script_pid=`ps -eaf | grep hbanyware | grep "/bin/sh" | grep -v grep | head -n 1 | awk '{ print $2 }'`
   #  echo "script pid of hbanyware is $script_pid"
   if [ "$script_pid" != "" ];then
      gui_pid=`ps -eaf | grep java | grep -v grep | grep "HBAnyware.jar" | awk '{ print $2 }'`
      if [ "$gui_pid" != "" ];then
         echo "Stopping HBAnyware GUI"
         kill -9 $gui_pid > /dev/null 2>&1
      fi
   fi
fi

# Get pid of script that started SSC GUI
script_pid=`ps -eaf | grep ssc | grep "/bin/sh" | grep -v grep | head -n 1 | awk '{ print $2 }'`
if [ "$script_pid" != "" ];then
   #  Get pid of GUI, child process of script_pid
   gui_pid=`ps -eaf | grep java | grep -v grep | grep "ssc.jar" | awk '{ print $2 }'`
   if [ "$gui_pid" != "" ];then
      echo "Stopping HBAnyware SSC GUI"
#     Kill HBAnyware SSC GUI
      kill -9 $gui_pid > /dev/null 2>&1
   fi
fi

# Kill parent elxdiscoveryd
ppid=`ps -ea | grep elxdisc | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
if [ ! -z "$ppid" ];then
    echo "Stopping HBAnyware Discovery Server"
	kill $ppid > /dev/null 2>&1
	discoveryDaemonStopped=1
fi

# Kill any remaining child of elxdisc
for ppid in `ps -ea | grep elxdisc | grep -v grep | awk '{ print $1 }' | sort -n`
do
	kill -9 $ppid > /dev/null 2>&1
	discoveryDaemonStopped=1
done

# Verify that the discovery daemon has indeed gone away
ppid=`ps -ea | grep elxdisc | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
trycount=0;
while [ ! -z "$ppid" ] && [ $trycount -lt 60 ]
do
    sleep 1
    ppid=`ps -ea | grep elxdisc | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
    trycount=`expr $trycount + 1`
done
if [ $trycount -eq 60 ]; then
    echo "Unable to stop the HBAnyware Discovery Server"
else
    if [ $discoveryDaemonStopped -eq 1 ];then
	    echo "HBAnyware Discovery Server stopped"
    fi
fi

# Kill fcauth daemon
if [ -x "/etc/init.d/fcauthd" ]; 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
          echo "Stopping HBAnyware Fibre Channel Authentication Server"
          /etc/init.d/fcauthd stop 1>/dev/null
          # Verify that the fcauth daemon has indeed gone away
          ppid=`ps -ea | grep fcauth | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
          trycount=0;
          while [ ! -z "$ppid" ] && [ $trycount -lt 60 ]
          do
             sleep 1
             ppid=`ps -ea | grep fcauth | grep -v grep | awk '{ print $1 }' | sort -n | head -n 1`
             trycount=`expr $trycount + 1`
          done
          if [ $trycount -eq 60 ]; then
             echo "Unable to stop the fibre channel authentication server"
          fi
	   fi
   fi
fi

