#!/bin/sh
#  Master validation script

function abort() {
	echo "Validation aborted, error code $1."
	echo "Validation aborted, error code $1." >> $OPD_TESTS
	exit
}

function report_error() {
	# Find all error files in the directory tree starting at $1.
	# Put these in a list file and report how many were found.
	OPD_ERR=$OPDV/list_of_error_files
	find $1 -name 'err_for_*' > $OPD_ERR 
	if [ -s $OPD_ERR ] ;then 
		OPD_ERR_COUNT=`awk 'END{print NR}' < $OPD_ERR`
		echo "Error files are:" >> $OPD_TESTS
		awk '{ print NR ". " $0 }' < $OPD_ERR >> $OPD_TESTS
		echo "Found $OPD_ERR_COUNT error files." >> $OPD_TESTS
		echo "See \`$OPD_TESTS' for the list." >> $OPD_TESTS
	else 
		echo "\`$OPD_TESTS' is the test log." >> $OPD_TESTS
		echo "No errors found." >> $OPD_TESTS
	fi
	rm $OPD_ERR
}

function force_to_null () {
	if [ -e  $1  ] ;then OPD_FORCE_TO_NULL_RET=$*
	else OPD_FORCE_TO_NULL_RET= ;fi
}

function generate_test_code() {
	cd $OPDT
	OPD_AR_TYP=$2
	force_to_null $OPDV/make_*_validate.rec
	OPD_GENERATE=$OPD_FORCE_TO_NULL_RET
	force_to_null $OPDV/make_*_validate_$OPD_AR_TYP.rec 
	OPD_GENERATE="$OPD_GENERATE $OPD_FORCE_TO_NULL_RET"
	for i in $OPD_GENERATE ; do
		echo "Creating tests from \`$i' with flags \`$1'"
		echo "Creating tests from \`$i' with flags \`$1'" >> $OPD_TESTS
		if ! $OPD_ROOT/bin/opd -c -a $i $1 ;then abort 4_$i ;fi
		echo "Created tests from \`$i' with flags \`$1'"
		echo "Created tests from \`$i' with flags \`$1'" >> $OPD_TESTS
		rm *.dat
		rm *.dau
	done
}


OPD_VALIDATE=VALIDATE
OPD_VAL_DATA=VAL_DATA
OPD_MASTER_CREATE=
case $1 in
$OPD_VALIDATE) ;;
$OPD_VAL_DATA) OPD_MASTER_CREATE=yes ;;
*)
	echo "Must specify \`$OPD_VALIDATE' to run validation tests"
	echo "or \`$OPD_VAL_DATA' to create test data and run tests."
	echo "The default is to only run floating point test."
	echo "To run both float and 16 bit integer tests"
	echo "set and export \`INCLUDE_INT16=yes'."
	exit ;;
esac

OPDV=$OPD_ROOT/validate
OPDT=$OPDV/test_nodes
if [ ! -d $OPDT ] ; then if !  mkdir $OPDT ;then abort a1 ;fi ;fi

OPD_TESTS=$OPDV/validation_log
echo "Tests started at `date`." > $OPD_TESTS
echo "Tests started at `date`." 

OPD_FLOAT=$OPD_ROOT/bin/opd_dsp_exe
OPD_HAVE_FLOAT=
if [ -x $OPD_FLOAT ] ;then OPD_HAVE_FLOAT=yes ;fi

OPD_INT16=$OPD_ROOT/bin/opd_dsp16_exe
OPD_HAVE_INT16=
if [ -n "$INCLUDE_INT16" ] ;then
	if [ -x $OPD_INT16 ] ;then OPD_HAVE_INT16=yes ;fi
fi

if [ -z "$OPD_HAVE_FLOAT" ] ;then
	if [ -z "$OPD_HAVE_INT16" ] ;then
		echo "There is no floating point ($OPD_FLOAT) simulator"
		echo "and either no 16 bit integer ($OPD_INT16) simulator"
		echo "or INCLUDE_INT16 was not set."
		echo "Cannot run any validation tests."
		exit
	fi
fi

OPD_BASE_LOG=
OPD_DOING=
if [ -n "$OPD_HAVE_FLOAT" ] ;then 
	OPD_DOING="floating point" 
	if [ -n "$OPD_HAVE_INT16" ] ;then
		OPD_DOING="$OPD_DOING and"
		OPD_BASE_LOG=$OPDV/base_log
	else OPD_BASE_LOG=$OPDV/base_float_log ;fi
else OPD_BASE_LOG=$OPDV/base_int16_log ;fi
if [ ! -z "$OPD_HAVE_INT16" ] ;then OPD_DOING="$OPD_DOING 16 bit integer" ;fi


# Check to see if test data exists. exit
if [ -z "$OPD_MASTER_CREATE" ] ;then
	OPD_BASE_DATA_FILE=$OPDT/val_Basic_float/BasicPlt.dat
	if [ ! -e $OPD_BASE_DATA_FILE ] ;then 
		echo "There is no base line test data:"
		echo "\`$OPD_BASE_DATA_FILE'."
		echo "Do \`make VAL_DATA' to create all base line data for the"
		echo "$OPD_DOING tests"
		echo "and run a tests against this created data. "
		exit
	fi
fi

echo "Performing $OPD_DOING tests." >> $OPD_TESTS

OPD_GEN_CODE=$OPD_MASTER_CREATE
# Check to see if test files exist already, if not create them
OPD_FLOAT_TAR_SRC=$OPDT/val_Basic_float/compare/nodehds.C
if [ -n "$OPD_HAVE_FLOAT" ] ;then 
	if [ ! -e $OPD_FLOAT_TAR_SRC ] ;then OPD_GEN_CODE=yes 
	elif [ $OPD_FLOAT -nt $OPD_FLOAT_TAR_SRC ] ;then OPD_GEN_CODE=yes ;fi
fi
if [ -n "$OPD_GEN_CODE" ] ;then
	echo "Removing old float code and executables."
	echo "Removing old float integer code and executables."  >> $OPD_TESTS
	find $OPDT -name 'val_*_float' -exec \
		$OPD_ROOT/scripts/clear_old_test.sh {} tarflt \;
	eecho "Creating \`.dpp' files and target code for floating point tests." 
	echo "Creating \`.dpp' files and target code for floating point tests." >> \
		$OPD_TESTS
	if ! generate_test_code "" float  ;then abort 2 ;fi
	echo "Created \`.dpp' files and target code for floating point tests." 
	echo "Created \`.dpp' files and target code for floating point tests." >> \
		$OPD_TESTS
fi


OPD_GEN_CODE=$OPD_MASTER_CREATE
OPD_INT16_TAR_SRC=$OPDT/val_Basic_int16/compare/nodehds.C
if [ -n "$OPD_HAVE_INT16" ] ;then
	if [ ! -e $OPD_INT16_TAR_SRC ] ;then OPD_GEN_CODE=yes
	elif [ $OPD_INT16 -nt $OPD_INT16_TAR_SRC ] ;then OPD_GEN_CODE=yes ;fi
fi
if [ -n "$OPD_GEN_CODE" ] ;then
	echo "Removing old 16 bit integer code and executables."
	echo "Removing old 16 bit integer code and executables."  >> $OPD_TESTS
	find $OPDT -name 'val_*_int16' -exec \
		$OPD_ROOT/scripts/clear_old_test.sh {} tarint \;
	echo "Creating \`.dpp' files and target code for 16 bit integer tests." 
	echo "Creating \`.dpp' files and target code for 16 bit integer tests." >> \
		$OPD_TESTS
	if ! generate_test_code "-i" int16 ;then abort 3 ;fi
	echo "Created \`.dpp' files and target code for 16 bit integer tests." 
	echo "Created \`.dpp' files and target code for 16 bit integer tests." >> \
		$OPD_TESTS
fi

cd $OPDT
echo "Building master scripts in \`$OPDT'." >> $OPD_TESTS
echo "Building master scripts in \`$OPDT'."
if ! $OPD_ROOT/scripts/gen_test.sh $OPD_TESTS ;then abort 4 ;fi

if [ ! -z "$OPD_MASTER_CREATE" ] ;then 
	echo "Creating base line test data."
	echo "Creating base line test data." >> $OPD_TESTS
	if ! $OPDT/target_master_create.sh ;then abort 5 ;fi
	echo "Created target base line data for $OPD_DOING tests." >> $OPD_TESTS
	echo "Created target base line data for $OPD_DOING tests." 
	if ! $OPDT/interactive_node_master_ValCreate.sh ;then abort 6 ;fi
	echo "Created interactive node base line data for $OPD_DOING tests." >> $OPD_TESTS
	echo "Created interactive node base line data for $OPD_DOING tests." 
fi

echo "Starting target compare test."
echo "Starting target compare test." >> $OPD_TESTS
OPD_TAR_VAL_PARAM="-l $OPD_TESTS"
export OPD_TAR_VAL_PARAM 
if ! $OPDT/target_master_compare.sh $OPD_TESTS ;then abort 7 ;fi

echo "Starting interactive node compare test."
echo "Starting interactive node compare test." >> $OPD_TESTS
if ! $OPDT/interactive_node_master_ValCompare.sh $OPD_TESTS ;then abort 8 ;fi

echo "Tests completed at `date`." >> $OPD_TESTS
echo "Tests completed at `date`." 
report_error $OPDT
if [ -e $OPD_BASE_LOG ] ;then 
	if ! $OPD_ROOT/bin/cmp_val_log $OPD_TESTS $OPD_BASE_LOG ;then
		echo "WARNING: cannot execute $OPD_ROOT/bin/cmp_val_log"
		echo "to compare test log with base test log."
		echo "Cannot verify that a complete set of tests were run."
	fi
else echo "WARNING: There is no base log file \`$OPD_BASE_LOG'."
	echo "Cannot verify that a complete set of tests were run."
fi
cat $OPD_TESTS

