#!/bin/ksh
#
#ident	"@(#)CON5_str.stf	1.15	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This is the CON5 test module.
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################


# variables
prgdir=../../prgs	# program directory
maxwarn=50		# max number of nonfatal warnings to tolerate.
srcdir=$1		# Source directory
targdir=$2		# Target Directory
inst=1			# number of instances


# functions

clean_up()
{
	echo "CON5 Ending!"
	if [ -d $1/kernel2 ]
	then
		rm -rf $1/kernel2
	fi
	
	rm -rf $2/dcout*
	rm -rf $2/tbase*
	
	if [ -f /tmp/drives ]
	then
		rm -f /tmp/drives
	fi
}

### MAIN ###

# set trap
trap 'clean_up $sdir $targdir; exit' 17

# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
# ( This only makes sense if this script echos warning messages, but
#   inclusion of this variable in the log file is *MANDATORY* in all cases. )
#echo "maxwarnings=${maxwarn}"

((hours=$RUNTIME/3600))
((mins=$RUNTIME%3600))
((mins=$mins/60))
((secs=$RUNTIME%60))
typeset -Z2 mins secs
duration=$hours:$mins:$secs

echo "Starting CON5 test for $duration..."

if [ -z "$srcdir" ]
then	
	SDIR=`$prgdir/sliceinfo | awk '{print $2}' | sed -n '3p'`
	SDIR=$SDIR/kernel2
else
	SDIR=$srcdir
fi

if [ -z "$targdir" ]
then	
	TDIR=`$prgdir/sliceinfo | awk '{print $2}' | sed -n '7p'`
	targdir=$TDIR/tmp
fi
	
if [ ! -d $SDIR ]
then 
	mkdir -p $SDIR
fi

#cp -rf /kernel/* $SDIR
command="time -p cp -rf /kernel/* $SDIR "
lapse=`( $command ) 2>&1 | egrep 'real *' | awk '{print $2}' | sed ' {
s/\..*$//g
}' `
((RUNTIME=$RUNTIME-$lapse))
((hours=$RUNTIME/3600))
((mins=$RUNTIME%3600/60))
((secs=$RUNTIME%60))
duration=$hours:$mins:$secs

srcdir=$SDIR

if [ ! -d $targdir ]
then
	mkdir -p $targdir
fi

# targdir may already exist - ensure there is no 'log' directory in there
rm -rf $targdir/log
rm -rf $targdir/dcout*
rm -rf $targdir/tbase*

cd CON5/bin
./CON5repeat -nofsck $srcdir $targdir $duration $inst

clean_up $srcdir $targdir
