#!/bin/sh
PATH=/usr/bin:$PATH
EG_HOME=/opt/egurkha
export PATH EG_HOME
#============================================================================
# stop_agent: shell script that starts discovery
#=============================================================================

pid_list=`ps -aef -opid,args | grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 
#get the list of pids 
if [ -z "$pid_list" ]
then
	if [ -f "/usr/ucb/ps" ]
	then
	pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep jre | grep -v grep | grep "java " | awk '{print $2}'`
	fi
fi

if [ "$pid_list" ] 	# if EgMainAgent is running
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi

pid_list=`ps -aef -opid,args | grep "EgMain" | grep -v grep | grep jre |  awk '{print $1}'` 
if [ -z "$pid_list" ]
then
	if [ -f "/usr/ucb/ps" ]
	then
	pid_list=`/usr/ucb/ps -gauxwww | grep "EgMain" | grep jre | grep -v grep | grep "java " | awk '{print $2}'`
	fi
fi

#get the list of pids 
if [ "$pid_list" ] 	# if EgMainAgent is running
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi
pid_list=`ps -e -o pid,args | grep "java EgHttp" | grep -v "grep" | awk '{print $1}'`
#get the list of pids
if [ -z "$pid_list" ]
then
	if [ -f "/usr/ucb/ps" ]
	then
	pid_list=`/usr/ucb/ps -gauxwww | grep "java EgHttp" |grep -v grep | awk '{print $2}'`
	fi
fi

#echo $pid_list
if [ "$pid_list" ]      # if EgHttpGet is running
then
        for pid in $pid_list    # for each pid
        do
                # kill each child
                kill -9 $pid > /dev/null 2>&1
        done
fi


pid_list=`ps -aef -opid,args | grep "tprof -P all" | grep -v grep | awk '{print $1}'` 
if [ -z "$pid_list" ]
then
	if [ -f "/usr/ucb/ps" ]
	then
	pid_list=`/usr/ucb/ps -gauxwww | grep "tprof -P all" | grep -v grep | awk '{print $2}'`
	fi
fi

#get the list of pids 
if [ "$pid_list" ] 	# if EgMainAgent is running
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi


pid_list=`ps -aef -opid,args | grep "/usr/bin/trace" | grep -v grep | awk '{print $1}'` 
if [ -z "$pid_list" ]
then
	if [ -f "/usr/ucb/ps" ]
	then
	pid_list=`/usr/ucb/ps -gauxwww | grep "/usr/bin/trace" | grep -v grep | awk '{print $2}'`
	fi
fi

#get the list of pids 
if [ "$pid_list" ] 	# if EgMainAgent is running
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi

pid_list=`ps -aef -opid,args | grep "traceroute" | grep -v grep | awk '{print $1}'` 
if [ -z "$pid_list" ]
then
	if [ -f "/usr/ucb/ps" ]
	then
	pid_list=`/usr/ucb/ps -gauxwww | grep "traceroute" | grep -v grep | awk '{print $2}'`
	fi
fi
#get the list of pids 
if [ "$pid_list" ] 	# Killing traceroute command
then
	for pid in $pid_list    # for each pid
	do
		# kill each child
		kill -9 $pid > /dev/null 2>&1 
	done
fi

#./eGAgentmon stop > /dev/null 2>/dev/null
$EG_HOME/bin/eGAgentmon stop > /dev/null 2>/dev/null

if [ -z "$1" ]
then
	echo "*************************************************"
	echo "The eG agent has been stopped successfully."
	echo "*************************************************"
fi

