#!/bin/bash 
PRODUCT=ASPlite
export PRODUCT
 
PATH=$PATH:/usr/bin:/bin
export PATH
TOMCAT_DIR=/opt/egurkha/manager/tomcat
APACHE_DIR=/opt/egurkha/manager/apache
export TOMCAT_DIR 
export APACHE_DIR
EG_HOME=/opt/egurkha
export EG_HOME
EG_BIN=$EG_HOME/bin
export EG_BIN
LD_LIBRARY_PATH=$EG_HOME/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
TOMCAT_HOME=/opt/egurkha/manager/tomcat
export TOMCAT_HOME
PJA_HOME=$EG_HOME/manager/pja
export PJA_HOME
EASY_HOME=$EG_HOME/lib
export EASY_HOME
CLASSPATH=.:$EG_HOME/lib/eg_manager.jar:$EG_HOME/lib/eg_util.jar:$EG_HOME/lib/classes111.zip:$TOMCAT_HOME/lib:$PJA_HOME/pja.jar:$PJA_HOME/rtgraphics.jar:$EASY_HOME/chart.jar:$EASY_HOME/Acme.jar:$CLASSPATH
export CLASSPATH
#java com.eg.EgStartStopDisc stop
$TOMCAT_DIR/bin/shutdown.sh > /dev/null 2>/dev/null
sleep 15
#============================================================================
# make sure that tomcat stops
#============================================================================
if [ -r  $EG_BIN/.tomcat_pid ]
then
 pid=`grep "tomcat_pid" $EG_BIN/.tomcat_pid | cut -d '=' -f2`
        if [ "$pid" ]
        then
                kill -9 "$pid" > /dev/null 2>&1

                # after killing the agent remove the file .tomcat_pid
                rm -f $EG_BIN/.tomcat_pid
        fi
fi

#============================================================================
# make sure that EgThresholdManager stops
#============================================================================
if [ -r  $EG_BIN/.thresh_pid ]
then
 pid=`grep "thresh_pid" $EG_BIN/.thresh_pid | cut -d '=' -f2`
        if [ "$pid" ]
        then
                kill -9 "$pid" > /dev/null 2>&1

		# after killing the EgThresholdManager Process remove the file .thresh_pid
                rm -f $EG_BIN/.thresh_pid
        fi
fi

#============================================================================
# make sure that EgTrendManager stops
#============================================================================
if [ -r  $EG_BIN/.trend_pid ]
then
 pid=`grep "trend_pid" $EG_BIN/.trend_pid | cut -d '=' -f2`
        if [ "$pid" ]
        then
                kill -9 "$pid" > /dev/null 2>&1

		# after killing the EgTrendManager Process remove the file .trend_pid
                rm -f $EG_BIN/.trend_pid
        fi
fi

#============================================================================
# make sure that EgMailManager stops
#============================================================================
if [ -r  $EG_BIN/.mail_pid ]
then
 pid=`grep "mail_pid" $EG_BIN/.mail_pid | cut -d '=' -f2`
        if [ "$pid" ]
        then
                kill -9 "$pid" > /dev/null 2>&1

		# after killing the EgMailManager Process remove the file .mail_pid
                rm -f $EG_BIN/.mail_pid
        fi
fi
java com.egurkha.util.EgProcessKill manager
# Stop Apache
#$APACHE_DIR/bin/apachectl stop >/dev/null  2>/dev/null
$EG_HOME/bin/eGmon stop > /dev/null 2>/dev/null
#ps -e -o pid,args | grep cleanup_mgr | sed 's/^ //' | cut -f1 -d ' '| xargs kill -9 > /dev/null 2>/dev/null

# clean up the alarm manager's files
rm -f /tmp/HC*
rm -f /tmp/HN*
#rm -f /tmp/IC*
rm -f /tmp/IN*
rm -f /tmp/LC*
rm -f /tmp/LN*


pid=`ps -e -o pid,args | grep java | grep "dnsjava" | grep -v grep`
if [ "$pid" ]
then
	for p in $pid    # for each pid
	do
		# kill each child
		kill -9 $p > /dev/null 2>&1 
	done
fi


pid=`ps -e -o pid,args | grep java | grep "\-Xrs \-Xmx" | grep eGVCDiscovery | grep -v grep`
if [ "$pid" ]
then
	for p in $pid    # for each pid
	do
		# kill each child
		kill -9 $p > /dev/null 2>&1 
	done
fi


pid=`ps -e -o pid,args | grep java | grep "com.eg.EgStartManager" | grep -v grep`
if [ "$pid" ]
then
	for p in $pid    # for each pid
	do
		# kill each child
		kill -9 $p > /dev/null 2>&1 
	done
fi

if [ $# -eq 0 ]
then
	pid=`ps -e -o pid,args | grep java | grep "com.eg.EgCheckManager" | grep -v grep`
	if [ "$pid" ]
	then
		for p in $pid    # for each pid
		do
			# kill each child
			kill -9 $p > /dev/null 2>&1 
		done
	fi
	pid2=`ps -e -o pid,args | grep "eGmon start" | grep -v grep`
	if [ "$pid2" ]
	then
		for p in $pid2    # for each pid
		do
			# kill each child
			kill -9 $p > /dev/null 2>&1 
		done
	fi
fi

echo "******************************************************************"
echo "The eG Manager has been successfully stopped."
echo "******************************************************************"

