#!/bin/bash
PATH=/usr/bin:/bin:$PATH
EG_BIN=/opt/egurkha/bin
export EG_BIN PATH 
#============================================================================
# stop_agent: shell script that stops egurkha agent
#============================================================================
ppid=
amicontainer=`ps -e -o pid,args | grep "eGAgent_container"`

## Getting container agent entry point pid 
if [ -r  $EG_BIN/.container_entrypoint_pid ]
then
	entry_point_pid=`grep "container_agent_entrypoint_pid" $EG_BIN/.container_entrypoint_pid | cut -d '=' -f2`
	entry_point_parent_pid=`ps -o ppid= $entry_point_pid | sed 's/ //g'`
	if [ "$entry_point_parent_pid" ]
	then
		ppid=$entry_point_parent_pid
	fi
	## echo ppid=$ppid;echo entry_point_pid=$entry_point_pid;echo entry_point_parent_pid=$entry_point_parent_pid
fi


if [ -r  $EG_BIN/.eGurkha_pid ]
then
	pid=`grep "agent_pid" $EG_BIN/.eGurkha_pid | cut -d '=' -f2`
	if [ "$pid" ]
	then
		temp=
		if [ "$amicontainer" ]
		then
			temp=`ps -o ppid= $pid | sed 's/ //g'`
		fi
		if [ "$temp" ]
		then
			ppid=$temp
		fi
		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 $ppid > /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
#echo "PPID=$ppid"
if [ "$ppid" ]
then
	pid_list=`ps -e -o pid,ppid,args | grep $ppid | grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 
else
	pid_list=`ps -e -o pid,args |  grep "EgMain" | grep -v grep | grep "java " | awk '{print $1}'` 
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

if [ "$ppid" ]
then
	pid_list=`ps -aef -opid,ppid,args 2>/dev/null | grep $ppid | grep "EgMain" | grep -v grep | grep jre |  awk '{print $1}'` 2>/dev/null  
else
	pid_list=`ps -aef -opid,args 2>/dev/null | grep "EgMain" | grep -v grep | grep jre |  awk '{print $1}'` 2>/dev/null  
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

if [ "$ppid" ]
then
	pid_list=`ps -e -o pid,ppid,args | grep $ppid  | grep "java EgHttp" | grep -v "grep" | awk '{print $1}'`
else
	pid_list=`ps -e -o pid,args | grep "java EgHttp" | grep -v "grep" | awk '{print $1}'`
fi

#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

if [ "$ppid" ]
then
	pid_list=`ps -e -o pid,ppid,args | grep $ppid | grep "winexe" | grep "vmgfiles" | grep -v "grep" | awk '{print $1}'`
else
	pid_list=`ps -e -o pid,args | grep "winexe" | grep "vmgfiles" | grep -v "grep" | awk '{print $1}'`
fi

#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

if [ "$ppid" ]
then
	pid_list=`ps -e -o pid,ppid,args | grep $ppid | grep "traceroute" | grep -v "grep" | awk '{print $1}'`
else
	pid_list=`ps -e -o pid,args | grep "traceroute" | grep -v "grep" | awk '{print $1}'`
fi

#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 $ppid > /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

