#!/usr/bin/ksh
################################################################################
#Copyright 2009,2010 Oracle and/or its affiliates.  All rights reserved.
#script: stopsunvts
#path: /usr/sunvts/bin/stopsunvts
#The purpose of 'stopsunvts' script is to stop and quit SunVTS application.
# Usage options:
# -a, --agent 		(for stopping and quitting sunvts agent only) 
# -t, --tty 		(for stopping/quitting tty interface only) 
# -h, --help		(help)
# no option		By default, stopsunvts will stop and quit
#		 	the sunvts application 
################################################################################
#check if user is a valid user for accessing SunVTS
userAuth()
{
userinfo=`id | awk '{print $1}'`
userinfo=`echo $userinfo | awk '{i=index($1,"(");j=index($1,")");print substr($1,i+1,j-i-1)}'`

if [ "$userinfo" != "root" ]; then
echo "Access Denied. You need root or superuser privileges."
exit -4
fi
}

userAuth;

YES=0 #TRUE
NO=1  #FALSE
#get the current directory name and parse the root installtion dir for SunVTS 7.0
path=`pwd | awk '{ print substr( $0, 0,length($0) - length("/usr/sunvts/bin") )
}'`
#check if user running the script from  installtion dir (/$root_dir/usr/sunvts/bin). If not exit.
base_dir=`pwd | awk '{ print substr( $0,length($0) - (length("/usr/sunvts/bin")-1),length("/usr/sunvts/bin") ) }'`
if [ $base_dir != "/usr/sunvts/bin" ]
then
	echo "Please execute 'stopsunvts' script from  installation directory ({root_dir}/usr/sunvts/bin)."
	exit 1
fi

#initialize global variable to store vtsk pid
#checkvtsk_ps="";
#checkvtsk_vtspid="";
#check if vts.pid file exits.
#If yes, then match the pid in file to vtsk process id
getvtsk_pid()
{
checkvtsk_ps="";
checkvtsk_vtspid="";
#check for vts.pid file. If it exists, get the vtsk process id.
if [ -r /var/sunvts/vts.pid ] ; then
            checkvtsk_vtspid=`cat /var/sunvts/vts.pid | egrep -v VTS_PID`
checkvtsk_ps=`ps -ef | grep sunvts/bin | grep vtsk | egrep -v grep | awk '{print $2}'`
if [ -z "$checkvtsk_ps" ]; then
checkvtsk_ps=`ps -ef | grep ./vtsk | egrep -v grep | awk '{print $2}'`

fi

#compare the two vtsk pids
# return YES if pids match else return NO
if  [ "$checkvtsk_vtspid" -eq "$checkvtsk_ps" ]; then
	return $YES; 
fi
else
	return $NO;
fi
}

#check which version of vtsk is running, stopsunvts script will work with
#version 7.0 onwards.  
#--------------------------------
checkvtsk_version()
{
getvtsk_pid;
#if [ "$?" = "$YES" ]; then
	checkVtsk="$checkvtsk_ps"; 
#fi

if [ -n "$checkVtsk" ]
then
	agent_version=`$path/usr/sunvts/bin/vts_cmd get_version`
	which_version=`echo $agent_version | awk '{print substr($0, 0, 1)}'`
	if [ $which_version != "7" ]
	then
		echo "Previous version of SunVTS is running.\n stopsunvts can be executed with SunVTS 7.0 or higher version.\n stopsunvts will now exit."
		exit 1
	fi
fi
}

stopVTSK()
{
	echo "All currently executing tests will be stopped. Stopping the SunVTS Agent..."
	$path/usr/sunvts/bin/vts_cmd stop
}

quitVTSK()
{
	echo "Quitting SunVTS Agent..."
	$path/usr/sunvts/bin/vts_cmd quit
	sleep 5 
	getvtsk_pid	
	#if [ "$?" = "$YES" ]; then
       		 checkVtsk="$checkvtsk_ps";
	#fi

	if [ -z "$checkVtsk" ]
	then
		echo "SunVTS Agent quitted successfully"
	else
		echo "Quitting SunVTS Agent was unsuccessful"
	fi
}

quitTTY()
{
	checkTTY=`ps -ef | grep vtstty | egrep -v grep | awk '{print $2}'`
	if [ -n "$checkTTY" ]
	then
		echo "Quitting vtstty..."
		kill -2  `ps -ef | grep vtstty | egrep -v grep | awk '{print $2}'`
		sleep 2 
		checkTTY=`ps -ef | grep vtstty | egrep -v grep | awk '{print $2}'`
		if [ -z "$checkTTY" ]
		then
			echo "vtstty quitted successfully"
		else
			#echo "Quitting vtstty was unsuccessful"
			kill -9 `pgrep vtstty`
		fi
	fi
}


usage()
{
	echo "Usage: /usr/sunvts/bin/stopsunvts [-a] [-t] [-h]"
	echo "The following options are supported:"
	echo "-a	Stops and quits the SunVTS agent and other dependent processes and TTY UI"
	echo "-t	Quits vtstty only"
	echo "-h	Help with usage of the command"
	echo "default	Quits SunVTS application"
}

stop_n_quit_SunVTS()
{
	 getvtsk_pid
        #if [ "$?" = "$YES" ]; then
                 checkVtsk="$checkvtsk_ps";
        #fi
		
	if [ -n "$checkVtsk" ]
	then
		status=`$path/usr/sunvts/bin/vts_cmd get_status`
		status=`echo $status | awk '{print substr($0, 0, 4)}'`
		if [ $status == "idle" ]
		then
			echo "SunVTS Agent status is idle, will quit the agent and all dependent processes"
			quitTTY
			quitVTSK
		else
			stopVTSK
			status=`$path/usr/sunvts/bin/vts_cmd get_status`
			status=`echo $status | awk '{print substr($0, 0, 4)}'`
			while [ $status != "idle" ]
			do
				#echo "waiting on the status to get idle.."
				sleep 1
				status=`$path/usr/sunvts/bin/vts_cmd get_status`
				status=`echo $status | awk '{print substr($0, 0, 4)}'`
			done
		#e	evaluate_agent_flag
			quitVTSK
		fi
	#else
		#evaluate_agent_flag
	fi
}

while getopts :ath opt
do
	case "$opt" in
	    a)
		# a - agent only
		quit_only_agent=$YES
		stop_n_quit_SunVTS
		exit 1;;
	    t)
		# t - quit vtstty only
		quitTTY
		exit 1;;
	    h)
		# h - for usage help
		usage
		exit 1;;
	    \?)
		echo "Incorrect option !"
		usage
		exit 1;;
	esac
done
# in case no option is specified, then quit all components of 
# the SunVTS application.
echo "Stopping SunVTS application..."
quit_only_agent=$NO
checkvtsk_version;
stop_n_quit_SunVTS
exit 1
