#!/bin/ksh
#set -x
# Copyright 2009,2012 Oracle and/or its affiliates.All rights reserved.
#===============================================================

#check if user is a valid user for accessing VTS
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;

SUCCESS=0
FAIL=1

##--- Default options ---#
vtsdir="/usr/sunvts"
vtslogdir="/var/sunvts/logs"
outdir="/var/tmp"
verbose="Disable"
start_sl=1
end_sl=6
ltargs="all"
sched_policy="Test Passes"
test_passes=5
specific_pass_level=0
test_time=0
err_limit=5
Verbose=0
debug=0
## -- donot edit anything beyond this point --##

USAGE="\n\
  $0 [-v] [-V] \n\
	\t [-l start_stress_level] [-L end_stress_level] \n\
	\t [-t test_time] [-p test_passes] [-e error_limit] \n\
	\t [-P specific_pass_level] \n\
	\t ['all'  _or_  list of Test seperated by a space] \n\
  examples:\n\t $0 -l3 all \n\t $0 -vt 60 -l 5 MLT ILT \n\t $0 -l3 -L6 PLT NLT DLT \n\
  where,	\n\
  \t v = VTS test verbose mode ON (default: OFF)	\n\
  \t V = to make the script to dump verbose informations & faults (default: OFF) \n\
  \t stress_level = any stress level between and including 1 and 6 \n\
  	\t\t (default: start_stress_level=1, end_stress_level=6)	\n\
  \t test_time = max test time (global) in mts (default: infinity)	\n\
  \t test_passes = max test passes (default: 5) \n\
	\t\t (effective only when time option is not given) 	\n\
  \t error_limt = max no. of errors a test can tolerate (default: 5) \n\
  \t specific_pass_level = number of the pass level, when only one specific pass level to be run \n
  \n\
  \t all - run all Tests (default) \n\
  \t PLT - run Processor Test	\n\
  \t MLT - run Memory Test		\n\
  \t ILT - run Interconnect Test	\n\
  \t NLT - run Network Test 	\n\
  \t DLT - run Disk Test 	\n\
  \t ELT - run Environment Test	\n\
  \t MELT - run Media Test	\n\
  \t IOLT - run Ioports Test \n"

##parse options
while getopts l:L:t:p:P:a:e:vVdh o
do case "$o" in
	l) start_sl="$OPTARG";; 
	L) end_sl="$OPTARG";;
	t) test_time="$OPTARG" && sched_policy="Test Time";;
	p) test_passes="$OPTARG";;
	P) specific_pass_level="$OPTARG";;
	e) err_limit="$OPTARG";;
	v) verbose="Enable";;
	V) Verbose=1;;
	d) debug=1;;
	h) echo "$USAGE" && exit 0;;
	\?) echo "$USAGE" && exit 0;;
esac
done
shift $(($OPTIND -1))
if [[ ! -z $* ]]; then
	ltargs=$*
fi
# To control number of test passes and time in particular mode.
sv_test_passes=$test_passes
sv_test_time=$test_time

function Print_Verbose {
	if [[ $Verbose -eq 1 ]]; then
		echo $1
	fi
}

function ifset_debug {
	if [[ $debug -eq 1 ]]; then
		set -x
	fi
}

sl=$start_sl


#check if vts.pid file exits.
#If yes, then match the pid in file to vtsk process id
getvtsk_pid()
{
checkvtsk_ps="";
checkvtsk_vtspid="";

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

#check for vts.pid file. If it exists, get the vtsk process id.
if [ -r /var/sunvts/vts.pid ] ; then
	for vtspid in `cat /var/sunvts/vts.pid | egrep -v VTS_PID `; do
		if  [ "$vtspid" = "$checkvtsk_ps" ]; then
		checkvtsk_vtspid="$vtspid";
		fi
	done

#compare the two vtsk pids
if  [ "$checkvtsk_vtspid" = "$checkvtsk_ps" ]; then
        return $SUCCESS;
fi
else
        return $FAIL;
fi
}


function is_vtsk_idle {
sleep 5;
getvtsk_pid

#if [ "$?" = "$SUCCESS" ]; then
        checkvtsk="$checkvtsk_ps";
#fi

if [ -z "$checkvtsk" ]; then
#echo "checkvtsk value: " $checkvtsk
exit 2;
fi

vtskstatus=`$vtsdir/bin/vts_cmd get_status`
#echo $vtskstatus
vtskstatus=`echo $vtskstatus | cut -d "/" -f1`

if [ "$vtskstatus" != "idle" ]; then
exit 1;
fi

}

#check if there are any registered tests.
#if not, then exit script with appropriate message.
function is_test_present
{
testlist=`$vtsdir/bin/vts_cmd list_tests`

if [ -z "$testlist" ]; then
        exit 3;
fi
	
}

# Function checks for the test status
#
function report_test_status
{
        j=0
#for i in `$vtsdir/bin/vts_cmd list_tests | tr -d ' '`
        for i in `echo $list_tests_output | tr -d ' '`
        do
                Test[j]=`echo $i | cut -d";" -f1 | cut -d":" -f2`
                if [[ $1 = ${Test[j]} ]]
                then
                Status[j]=`echo $i | cut -d";" -f2 | cut -d":" -f2`
                if [[ ${Status[j]} = "True" ]]
                then
                       return 0
                else
                       return 1
                fi
                fi
#                echo ${Test[j]} ${Status[j]}
                let j=$j+1
        done

}

function stop_vtsk {


ifset_debug

	if [[ $Verbose -eq 1 ]]; then
		echo "\nStopping VTS Tests. Please wait."
	fi
sleep 5

getvtsk_pid

#if [ "$?" = "$SUCCESS" ]; then
        isvtsk="$checkvtsk_ps";
#fi

#echo "runvts_value of isvtsk" $isvtsk;

if [ -z "$isvtsk" ]; then
#echo "VTS agent has quit. Exiting now."
exit 2;
fi

	if [[ $Verbose -eq 1 ]]; then
		i=1
		while [[ $i -le $tests ]]; do
			echo "get_status ${selected_lts[$i]} : "
			$vtsdir/bin/vts_cmd get_status ${selected_lts[$i]}
			let i=$i+1
		done
	fi

	ret=`$vtsdir/bin/vts_cmd stop`
	sleep 30
if [ -z "$ret" ]; then
exit 2;

fi 
if [ "$ret" == "DONE" ]; then
	status=`$vtsdir/bin/vts_cmd get_status`
	if [ -n "$status" ]; then
	status=`echo $status | awk '{print substr($0, 0, 4)}'`
	while [ "$status" != "idle" ]
	do
		sleep 10
		status=`$vtsdir/bin/vts_cmd get_status`
		status=`echo $status | awk '{print substr($0, 0, 4)}'`
	done
	fi
fi
	if [[ $Verbose -eq 1 ]]; then
		echo "All Tests have stopped."
	fi
	cstatus=`$vtsdir/bin/vts_cmd get_status`
	if [[ $Verbose -eq 1 ]]; then
		echo "\nCurrent status: $cstatus"
	fi
}

function cleanup {
	$vtsdir/bin/vts_cmd stop 
	sleep 60
	$vtsdir/bin/vts_cmd quit
	sleep 10

	exit 1
}

#Additions for text output
function init_vtslogs {
	        mkdir $rpath > /dev/null 2>&1
	        chmod go+rw $rpath
}

function get_hr_min_sec {
	sec1=`echo $1 | cut -d ":" -f3`
        sec1=`echo $sec1|sed 's/^0*//'`
        sec1=$(($sec1+0))
        min1=`echo $1 | cut -d ":" -f2`
        min1=`echo $min1|sed 's/^0*//'`
        min1=$(($min1+0))
        hr1=`echo $1 | cut -d ":" -f1`
        hr1=`echo $hr1|sed 's/^0*//'`
        hr1=$(($hr1+0))
        sec2=`echo $2 | cut -d ":" -f3`
        sec2=`echo $secr2|sed 's/^0*//'`
        sec2=$(($sec2+0))
        min2=`echo $2 | cut -d ":" -f2`
        min2=`echo $min2|sed 's/^0*//'`
        min2=$(($min2+0))
        hr2=`echo $2 | cut -d ":" -f1`
        hr2=`echo $hr2|sed 's/^0*//'`
        hr2=$(($hr2+0))
}

function sub_time {
	ifset_debug

	get_hr_min_sec $1 $2

	if [[ $sec1 -lt $sec2 ]]; then
		let sec=$sec1+60
		let sec=$sec-$sec2
		let min2=$min2+1
	else
		let sec=$sec1-$sec2
	fi
	if [[ $min1 -lt $min2 ]]; then
		let min=$min1+60
		let min=$min-$min2
		let hr2=$hr2+1
	else
		let min=$min1-$min2
	fi

	let hr=$hr1-$hr2
}

function sum_time {
	ifset_debug

	get_hr_min_sec $1 $2

	cmin=0; chr=0
	let sec=$sec1+$sec2
	if [[ $sec -ge 60 ]]; then
		cmin=1
		let sec=$sec-60
	fi
	let min=$min1+$min2+$cmin
	if [[ $min -ge 60 ]]; then
		chr=1
		let min=$min-60
	fi
	let hr=$hr1+$hr2+$chr
}

function get_max {
	ifset_debug

	get_hr_min_sec $1 $2
	max_time=$1

	if [[ $hr2 -eq $hr1 ]]; then
		if [[ $min2 -eq $min1 ]]; then
			if [[ $sec2 -gt $sec1 ]]; then
				max_time=$2
			fi
		else
			if [[ $min2 -gt $min1 ]]; then
				max_time=$2
			fi
		fi
	else
		if [[ $hr2 -gt $hr1 ]]; then
			max_time=$2
		fi
	fi
}

## display helpers
function dis_result_txt {
	echo $start_date >> $ofile_txt

	i=1
	while [[ $i -le $tests ]]; do
		echo "${table_txt[$i]}\n" >> $ofile_txt

		k=1
		for vtime in `echo ${table_td[$i]} | tr -d "[:alpha:]</>"` ; do
			sum_time $VTtotal $vtime
			VTtotal="$hr:$min:$sec"
			if [[ $sl -eq 5 || $sl -eq 6 ]]; then
				sum_time ${HTtotal[$k]} $vtime
				HTtotal[$k]="$hr:$min:$sec"
			else
				get_max ${HTtotal[$k]} $vtime
				HTtotal[$k]=$max_time
			fi
			let k=$k+1
		done

		test_status=`$vtsdir/bin/vts_cmd get_status ${selected_lts[$i]}`
		if [[ ${specific_pl_lt[$i]} -gt 0 ]]; then
			PL_status="tested only SPECIFIC PASS LEVEL=${specific_pl_lt[$i]}"
		else
			PL_status="tested pass levels upto ${passcnt_lt[$i]}"
		fi
		echo "\tAll passes : $VTtotal \n\t$test_status ;; $PL_status\n" >> $ofile_txt
		VTtotal="0:0:0"

		let i=$i+1
	done

	echo "** SL-$sl: All LT total time ** \n" >> $ofile_txt
	echo "\tPass #1 : ${HTtotal[1]}\n" >> $ofile_txt
	echo "\tPass #2 : ${HTtotal[2]}\n" >> $ofile_txt
	echo "\tPass #3 : ${HTtotal[3]}\n" >> $ofile_txt
	echo "\tPass #4 : ${HTtotal[4]}\n" >> $ofile_txt
	echo "\tPass #5 : ${HTtotal[5]}\n" >> $ofile_txt
	echo "`date`\n" >> $ofile_txt
	echo "\nStatus summary: $vtsk_status \n\n" >> $ofile_txt

#	if [[ -d $rpath ]]; then
#		echo "Check here ../$rfile for VTS log files (with test status snapshot)" >> $ofile_txt
#	fi


}
function enable_test {

	ifset_debug

	Print_Verbose "\tenabling $1 tests...."	
is_vtsk_idle;
	e_msg=`$vtsdir/bin/vts_cmd enable_test $1`
	if [[ $e_msg == "DONE" ]]; then
		let tst=$tests+1
	specific_pl_lt[$tst]=0

		if [[ $specific_pass_level -gt 0 ]]; then
			is_vtsk_idle;
			e_msg=`$vtsdir/bin/vts_cmd set_test_options $1[Stress:$stress_level,Scheduling Policy:Test Passes,Test Passes:$test_passes,Specific Pass Level:$specific_pass_level,Error Limit:$err_limit]`

			if [[ $e_msg != "DONE" ]]; then
				echo "NOTE: failed to set_test_options for $1 test ($e_msg); \n\t... trying with default options"
	is_vtsk_idle;			
	e_msg=`$vtsdir/bin/vts_cmd set_test_options $1[Stress:$stress_level,Scheduling Policy:Test Passes,Test Passes:$test_passes,Error Limit:$err_limit]`
			else
				specific_pl_lt[$tst]=$specific_pass_level
			fi

		else
is_vtsk_idle;
		e_msg=`$vtsdir/bin/vts_cmd set_test_options $1[Stress:$stress_level,Scheduling Policy:Test Passes,Test Passes:$test_passes,Error Limit:$err_limit]`
		fi

		if [[ $e_msg == "DONE" ]]; then
			let tests=$tests+1
			selected_lts[$tests]=$1
			Print_Verbose "\tdone."
		else
			echo "Error: failed to set_test_options for $1 test ($e_msg); \n\t... skipping $1 test"
		fi

	#else
		#echo " \n\t "
	fi
}


SIGINT=2
SIGKILL=9
SIGTERM=15
SIGTSTP=24
trap trhdlr $SIGINT $SIGTERM $SIGTSTP $SIGKILL

#setup signal handlers
function trhdlr {
	ifset_debug

	## reset trap handlers
	trap - $SIGINT $SIGTERM $SIGTSTP $SIGKILL

	vtsk_status=`$vtsdir/bin/vts_cmd get_status`


		if [[ $testing -eq 1 ]]; then
			stop_vtsk
			dis_result_txt
		fi
		sleep 10
# Quit the vtsk as we got a signal.
		echo "\nQuitting VTS."
		$vtsdir/bin/vts_cmd quit 

	exit 100
}


ifset_debug

if [[ ! -d $vtsdir ]]; then
        print "ERROR: $vtsdir :: not found\n"
         exit 1
fi
#directory/file set up
outdirpath=$outdir
sdir=`echo $outdir | cut -d "/" -f1`
if [[ ! -z $sdir ]]; then
        outdir=`pwd`
	outdir=$outdir/$outdirpath
fi
rfile=`uname -i`-`uname -n`-LT-timings-`date '+%m-%d-%y-%H:%M:%S'`
rpath="$outdir/$rfile"
#mfile="$rpath$ofile_ext"
#ofile="$rpath/lttime.html"
ofile_txt="$rpath/lttime.txt"

until [[ $sl -gt $end_sl ]]; do

	if [[ $sl -eq 1 ]]; then
		mode="Online Stress"
		stress_level="low"
		test_passes=$sv_test_passes
		test_time=0
	elif [[ $sl -eq 2 ]]; then
		mode="Online Stress"
		stress_level="high"
		test_passes=$sv_test_passes
		test_time=0
	elif [[ $sl -eq 3 ]]; then
		mode="System Exerciser"
		stress_level="low"
		test_passes=0
		test_time=$sv_test_time
	elif [[ $sl -eq 4 ]]; then
		mode="System Exerciser"
		stress_level="high"
		test_passes=0
		test_time=$sv_test_time
	elif [[ $sl -eq 5 ]]; then
		mode="Component Stress"
		stress_level="low"
		test_passes=$sv_test_passes
		test_time=0
	elif [[ $sl -eq 6 ]]; then
		mode="Component Stress"
		stress_level="high"
		test_passes=$sv_test_passes
		test_time=0
	else
		echo "Unrecognized Mode Specified.Please check the usage. \n $USAGE"
		cleanup 2
	fi


	##debug inputs
	Print_Verbose "Starting with your following inputs : \n SL=$stress_level;  mode=$mode; \n tests = $ltargs \n SunVTS_test_verbose=$verbose; \n sched_policy=$sched_policy;  test_time=$test_time;  test_passes=$test_passes; err_limit=$err_limit; specific_pass_level=$specific_pass_level\n Start Test: $start_sl ; End Test: $end_sl \n "

	## do the action
	echo "Testing in $mode Mode -$stress_level"
	$vtsdir/bin/startsunvts -c > /dev/null 2>&1

	is_vtsk_idle
	is_test_present
	
	Print_Verbose "\t Setting the mode (set_mode) $mode ..."
	e_msg=`$vtsdir/bin/vts_cmd set_mode [$mode]`
	if [[ $e_msg != "DONE" ]]; then
		echo "ERROR (vts_cmd set_mode $mode): failed to set mode (vtsk running?)\n $e_msg "
		cleanup 3
	fi
	Print_Verbose "\tdone.\n"
is_vtsk_idle

# Get the vts_cmd list_tests output and store it in a global
#
	Print_Verbose "\tGetting the tests status\n"
	list_tests_output=`$vtsdir/bin/vts_cmd list_tests`
	Print_Verbose "\tdone.\n"
is_vtsk_idle

	Print_Verbose "\tset_global_options : Duration: $test_time, Verbose:$verbose ..."
	e_msg=`$vtsdir/bin/vts_cmd set_global_options [Duration of Testing:$test_time,Verbose:$verbose]` 
	if [[ $e_msg != "DONE" ]]; then
		echo "ERROR (vts_cmd set_global_options): failed to set verbose mode\n $e_msg"
		cleanup 5
	fi
	Print_Verbose "\tdone.\n"
#Disable tests only if targ has a test name
# To add the rest of the tests
			targ1=`echo $ltargs | cut -d " " -f1` 
			typeset -u upper1
			upper1=$targ1	
			targ1=$upper1

			if [[ $targ1 == "PLT"  ||  $targ1 == "MLT" || $targ1 == "ILT"  || $targ1 == "NLT"  || $targ1 == "DLT"  || $targ1 == "ELT"  || $targ1 == "MELT"  || $targ1 == "IOLT" ]]; then
			Print_Verbose "\tdisabling all test ..."
				e_msg=`$vtsdir/bin/vts_cmd disable_test all`
				if [[ $e_msg != "DONE" ]]; then
					echo "ERROR (vts_cmd disable_test all): failed to disable all tests to start with\n $e_msg "
					cleanup 4
				fi
				Print_Verbose "\tdone.\n"

			fi

	tests=0
	for targ in `echo $ltargs` ; do
			typeset -u upper
			upper=$targ	
			targ=$upper


			if [[ $targ == "ALL"  ||  $targ == "PLT" ]]; then
				report_test_status "Processor"
				if [[ $? -eq 0 ]]; then
				enable_test "Processor"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "MLT" ]]; then
				report_test_status "Memory"
				if [[ $? -eq 0 ]]; then
				enable_test "Memory"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "ILT" ]]; then
				report_test_status "Interconnect"
				if [[ $? -eq 0 ]]; then
				enable_test "Interconnect"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "NLT" ]]; then
				report_test_status "Network"
				if [[ $? -eq 0 ]]; then
				enable_test "Network"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "DLT" ]]; then
				report_test_status "Disk"
				if [[ $? -eq 0 ]]; then
				enable_test "Disk"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "ELT" ]]; then
				report_test_status "Environment"
				if [[ $? -eq 0 ]]; then
				enable_test "Environment"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "MELT" ]]; then
				report_test_status "Media"
				if [[ $? -eq 0 ]]; then
				enable_test "Media"
				fi
			fi
			if [[ $targ == "ALL"  ||  $targ == "IOLT" ]]; then
				report_test_status "Ioports"
				if [[ $? -eq 0 ]]; then
				enable_test "Ioports"
				fi
			fi
	done

## enable any other (custom) Test
#	if [[ $tests -eq 0 ]]; then
#for targ in `echo $ltargs` ; do
#			enable_test $targ
#		done
##	fi
	if [[ $tests -eq 0 ]]; then
		echo "Unrecognized / NIL  test Test option...pls see the usage below\n $USAGE"
		cleanup 6
	fi

	if [[ $Verbose -eq 1 ]]; then
		$vtsdir/bin/vts_cmd get_mode
		$vtsdir/bin/vts_cmd list_tests
	fi
# init message log dir
        init_vtslogs

## print System configurations and VTS version/profile information
	if [[ $sl -eq $start_sl ]]; then

		if [[ ! -d $vtslogdir ]]; then
			echo "ERROR: $vtslogdir :: not found\n"
			exit 1
		fi

echo "\nTiming/characterization results in text format would be logged in $rpath \n"
#		echo "<pre>" > $ofile_txt
		echo "\n*** System configuration summary ***" >> $ofile_txt
		/usr/sbin/prtdiag | head -4 >> $ofile_txt
		/usr/sbin/psrinfo -vp >> $ofile_txt
		n=`/usr/sbin/psrinfo | grep  -v "on-line" | wc -l`
		if [[ $n -gt 0 ]]; then
			echo "There are $n no. of cpus un-available for testing\n"
			/usr/sbin/psrinfo | grep -v "on-line" 
		fi
		/usr/sbin/prtconf | head -4 >> $ofile_txt
		if [[ $? -ne 0 ]]; then
			/etc/prtconf | head -4 >> $ofile_txt
		fi
		echo "*** VTS Version: " >> $ofile_txt
		cat $vtsdir/bin/.version >> $ofile_txt
		echo "*** VTS Profile : " >> $ofile_txt
		ls -l $vtsdir/lib/conf/profiles >> $ofile_txt
		echo " *** *** *** ***\n" >> $ofile_txt
#		echo "</pre>" >> $ofile_txt
#cp $ofile $ofile_txt
		chmod go+rw $ofile_txt
	fi


	## Start the tests
#echo ">>> Starting  Stress Level $sl tests ..."
is_vtsk_idle

	e_msg=`$vtsdir/bin/vts_cmd start`
	if [[ $e_msg != "DONE" ]]; then
		echo "ERROR (vts_cmd start): failed to start the tests"
		cleanup 7
	fi

	testing=1
	#date
	echo "\n****** Stress Level $sl : $mode - $stress_level ******\n" >> $ofile_txt
	start_date=`date`

	if [[ $Verbose -eq 1 ]]; then
#echo "\nVTS testing started."
		echo "\nA Test Report will be generated and displayed \nafter the completion of the tests.Please wait till then."
	fi
        VTtotal="0:0:0"
	stserrcnt=0
	log_interval=0
	i=1
        while [[ $i -le $tests ]]; do
                passcnt_lt[$i]=0
                prev_test_time[$i]=000:00:00
                table_td[$i]="<td> ${selected_lts[$i]} </td>"
                table_txt[$i]="*${selected_lts[$i]}_Test-SL$sl *"
                HTtotal[$i]=0:0:0
                let i=$i+1
        done 
	# main timing loop ....#
	break_flag=0

	while [ 1 ]; do
	t_status=`$vtsdir/bin/vts_cmd get_status ` 
		if [[ $? -ne 0 ]]; then
			echo "VTS kernel might have exited...Terminating the testing"
			break
		fi
	global_test_status=`$vtsdir/bin/vts_cmd get_status | cut -d "/" -f1` 
		if [[ $global_test_status == "idle" ]]; then
			break_flag=1
		fi
#Addition for text logs
                i=1
                while [[ $i -le $tests ]]; do
                        statusinfo=`$vtsdir/bin/vts_cmd get_status ${selected_lts[$i]}`
                        if [[ $? -ne 0 ]]; then
                                let i=$i+1
                                let stserrcnt=$stserrcnt+1
                                continue
                        fi

                        passcnt=`echo $statusinfo | cut -d "/" -f2 | cut -d "=" -f2`
                        if [[ -z `echo $passcnt | tr -d '[0-9]'` ]]; then
                          if [[ $passcnt -gt ${passcnt_lt[$i]} ]]; then
                                Print_Verbose "\t${selected_lts[$i]} :: $statusinfo \n"
                                test_time[$i]=`echo $statusinfo | cut -d "/" -f4 | cut -d "=" -f2`

                                sub_time ${test_time[$i]} ${prev_test_time[$i]}
# Enable for debug if only required.
#echo "${selected_lts[$i]} LT pass #$passcnt completed in (hhh:mm:ss) : $hr:$min:$sec\n"
                                table_td[$i]=`echo "${table_td[$i]} <td> $hr:$min:$sec </td>"`
                                table_txt[$i]=`echo "${table_txt[$i]} \n\tPass #$passcnt time : $hr:$min:$sec"`

                                passcnt_lt[$i]=$passcnt
                                prev_test_time[$i]=${test_time[$i]}
                          fi
                        fi

                        let i=$i+1
                done
		
		if [[ $break_flag -eq 1 ]]; then
			break
		fi

		sleep 10
	done

	# save the vtsk status
	vtsk_status=`$vtsdir/bin/vts_cmd get_status`

	# Stop the kernel 
	stop_vtsk
	dis_result_txt
	reset_status=`$vtsdir/bin/vts_cmd reset`
	#date

	testing=0

	if [[ $Verbose -eq 1 ]]; then
		echo "<<<< Completed testing in mode $mode\n"
	fi

	let sl=$sl+1
	sleep 10

done
