#!/bin/bash
PATH=/usr/bin:/bin:$PATH
EG_BIN=/opt/egurkha/bin
export EG_BIN PATH 
#============================================================================
# stop_agent: shell script that stops egurkha agent
#============================================================================
if [ -r  $EG_BIN/.eGurkha_pid ]
then
	pid=`grep "agent_pid" $EG_BIN/.eGurkha_pid | cut -d '=' -f2`
	if [ "$pid" ]
	then
		kill -9 "$pid" > /dev/null 2>&1

		# after killing the agent remove the agent_pid entry from the file .eGurkha_pid
		grep -v "agent_pid" $EG_BIN/.eGurkha_pid >  $EG_BIN/temp
		cp $EG_BIN/temp $EG_BIN/.eGurkha_pid
		rm -f $EG_BIN/temp
		$EG_BIN/eGAgentmon stop > /dev/null 2>/dev/null
		#if [ "x$1" = "x" ]
		#then
        	#echo "*************************************************"
        	#echo "The eG agent has been stopped successfully."
        	#echo "*************************************************"
		#fi
	#else
		#if [ "x$1" = "x" ]
		#then
		#echo "*************************************************************"
		#echo "Failed to stop the agent. The agent is not running."
		#echo "*************************************************************"
		#fi
	fi 
#else
	#if [ "x$1" = "x" ]
	#then
	#echo "*****************************************************************"
	#echo "Failed to stop the agent. The agent is not running." 
	#echo "*****************************************************************"
	#fi
fi

pid_list=`ps -e -o pid,args | grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 
#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 2>/dev/null | grep "EgMain" | grep -v grep | grep jre |  awk '{print $1}'` 2>/dev/null 
#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
#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 -e -o pid,args | grep "winexe" | grep "vmgfiles" | grep -v "grep" | awk '{print $1}'`
#get the list of pids
#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 -e -o pid,args | grep "traceroute" | grep -v "grep" | awk '{print $1}'`
#get the list of pids
if [ "$pid_list" ]      # if traceroute is running
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
$EG_HOME/bin/kill_ds > /dev/null 2>/dev/null



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

