#!/bin/sh
JAVA_HOME=/opt/egvmagent/jre/
JAVA_LIB=/opt/egvmagent/jre//lib
export JAVA_HOME JAVA_LIB


export JAVA_HOME
export JAVA_LIB
JAVA_FONT_HOME=/opt/egvmagent/jre/
export JAVA_FONT_HOME

EG_HOME=/opt/egurkha
export EG_HOME

PATH=$JAVA_HOME/bin:$EG_HOME/bin:/sbin:/usr/bin:$PATH:/bin:/usr/local/bin
export PATH

CLASSPATH=.:$EG_HOME/lib/eg_util.jar:$EG_HOME/lib/eg_plus.jar:$EG_HOME/lib/eg_vmagent.jar:$EG_HOME/lib/jsse.jar:$EG_HOME/lib/jcert.jar:$EG_HOME/lib/jnet.jar:$CLASSPATH
export CLASSPATH

unset JAVA_TOOL_OPTIONS
unset _JAVA_OPTIONS
unset IBM_JAVA_OPTIONS

#============================================================================
# start and stop eG Agent monitor/recover
#=============================================================================
mypid=`ps -o pid= -p $$`
if [ "x$1" = "x" ] 
then
	echo "Wrong input value!"	
elif [ $1 = "stop" ]
then
	pid_list=`ps -e -opid,args | grep "egvmagentmon" | grep -v "grep"| awk '{print $1}'`
	#get the list of pids
	if [ "$pid_list" ]      # if Agent Recovery is running
	then
        	for pid in $pid_list    # for each pid
        	do
                	# kill each child
                    if test  $pid != $mypid ; then
                         kill -9 $pid > /dev/null 2>&1
                    fi

        	done
	fi
        # to kill the sleep command when eGAgentmon is stopped
        userName=`id | awk '{print $1}' | cut -d'(' -f2 | cut -d')' -f1`
	isFree=`uname -a | grep BSD | wc -l`
        if [ $isFree -eq 1 ]
	then
		pid_list=`ps -e -o pid,user,args | grep "java SleepThread 298" | grep "$userName" | grep -v "grep"| awk '{print $1}'`
	else
		pid_list=`ps -e -o pid,user,args | grep "sleep 298" | grep "$userName" | grep -v "grep"| awk '{print $1}'`
	fi
        #get the list of pids
        if [ "$pid_list" ]      # if Recovery is running
        then
                for pid in $pid_list    # for each pid
                do
                        # kill each child
                        kill -9 $pid > /dev/null 2>&1
                done
        fi
        # code for killing sleep ends here

elif [ $1 = "start" ]
then
	isFree=`uname -a | grep BSD | wc -l`
	while :
	do
		if [ $isFree -eq 1 ]
		then 
			java SleepThread 298
		else
			sleep 298
		fi
		#count=`ps -e -o pid,user,args | grep "java" | grep "EgInsideViewServer" | grep -v "grep" | wc -l`
		#if [ $count -eq 0 ]
		#then
		#	nohup /opt/egvmagent/bin/start_vmagent
		#fi
		java EgCheckVmAgent  
	done
else
	echo "Wrong input value!"	
fi

