#!/bin/sh
PATH=/usr/bin:$PATH
EG_HOME=/opt/egurkha
EG_BIN=/opt/egurkha/bin
export PATH EG_HOME 
export EG_BIN
#============================================================================
# stop_agent: shell script that starts discovery
#=============================================================================
UNIX95=ps
export UNIX95
pid_list=`ps -aef -o pid,args | grep "traceroute" | grep -v grep | awk '{ print $1}'`
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

pid_list=`ps -aef -o pid,args | grep "glance" | grep -v grep | awk '{ print $1}'`
if [ "$pid_list" ] 	# if glance 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 [ -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 fi
		#le .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

