#!/bin/ksh
#**************************************************************
#  
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#  
#**************************************************************

#set -x
umask 0

integer TOTAL_PASS=0
integer TOTAL_FAIL=0
integer TOTAL_RUN=0
integer MAX_RETRIES=5
typeset RUNNAME=`date +%Y%m%d%H%M%S`
typeset PRINTDATE=`date`
typeset PDB_INFILE_DIR
typeset PDB_OUTFILE_DIR
typeset XML_OUTFILE_DIR
typeset REPORT
typeset RESDIR
typeset LOGFILE
typeset COMPLOG
typeset TEST_COMMENTS
typeset BGCOLOR
typeset ODD_BGCOLOR='#BBBBBB'
typeset EVEN_BGCOLOR='#DCDCDC'
typeset PASS_COLOR='#00ff00'
typeset FAIL_COLOR='#ff4040'

typeset ENVFILE=""
# The following variables should be set in the env file
typeset MASTERLIST=""
typeset TESTCASEDIR=""
typeset RESULTSBASE=""
typeset XMERGE_JAR=""
typeset APORTIS_JAR=""
typeset WORDSMITH_JAR=""
typeset MINICALC_JAR=""
typeset PERL5LIB=""
typeset POSE_EXE=""
typeset POSE_PRC=""
typeset TEST_DRIVER_PL=""
typeset COMPARATOR_PL=""
typeset COMPLIST=""
typeset XML_INFILE_DIR=""
typeset PDB_BASELINE_DIR=""
typeset XML_BASELINE_DIR=""
typeset EM_SCRIPT_HOME=""
typeset QAWRAPPER_SCRIPT_HOME=""
typeset EM_ROM_FILE=""
typeset EM_SESSION_FILE=""
typeset QA_LIB_HOME=""
typeset QA_COMPARATOR_HOME=""
typeset CLASSES_DIR=""



################################################################################
Usage() {
	echo "Usage: run-convtest -env <ENVFILE> [-name RUNNAME]"
	exit 1
}


################################################################################
StartReportFile() {
	typeset line=`date`

	ReportLine "<HTML>"
	ReportLine "<HEAD>"
	ReportLine "<TITLE>XMerge Converters Test Results - ${RUNNAME}</TITLE>"
	ReportLine "</HEAD>"
	ReportLine "<BODY BGCOLOR=#ffffff>"
	ReportLine "<H1 align=center>XMerge Converters Test Results - ${RUNNAME}</H1>"
	ReportLine "<P>"
	ReportLine "Test run on: ${PRINTDATE}"
	ReportLine "<P>"
	ReportLine "<CENTER>"
	ReportLine "<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=2>"
	ReportLine "<TR BGCOLOR='#9999CC'>"
	ReportLine "<TH>Test Name</TH>"
	ReportLine "<TH>Test File</TH>"
	ReportLine "<TH>.ext</TH>"
	ReportLine "<TH>Result</TH>"
	ReportLine "<TH>Comments</TH>"
	ReportLine "</TR>"
}


################################################################################
EndReportFile() {
	# remove full path from LOGFILE (link will be to current dir)
	typeset loglink=${LOGFILE##*/}

	ReportLine "<P>"
	ReportLine "<CENTER>"
	ReportLine "<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2>"
	ReportLine "<TR>"
	ReportLine "<TH>Total Tests PASSED</TH>"
	ReportLine "<TH>${TOTAL_PASS}</TH>"
	ReportLine "</TR>"
	ReportLine "<TR>"
	ReportLine "<TH>Total Tests FAILED</TH>"
	ReportLine "<TH>${TOTAL_FAIL}</TH>"
	ReportLine "</TR>"
	ReportLine "<TR>"
	ReportLine "<TH>Total Tests Run</TH>"
	ReportLine "<TH>${TOTAL_RUN}</TH>"
	ReportLine "</TR>"
	ReportLine "</TABLE>"
	ReportLine "</CENTER>"
	ReportLine "<P>"
	ReportLine "<A HREF=${loglink}>Full logfile for test run</A>"
	ReportLine "<P>"
	ReportLine "<CENTER>"
	ReportLine "</BODY>"
	ReportLine "</HTML>"
}

################################################################################
ReportLine() {
	echo $1 >> $REPORT
}

################################################################################
LogLine() {
	echo $1 >> $LOGFILE
}

################################################################################
ReportTestComments() {
	if [[ $TEST_COMMENTS == "" ]] ; then
		TEST_COMMENTS="&nbsp;"
	fi

	ReportLine "<TD>${TEST_COMMENTS}</TD>"
}

################################################################################
GetParams() {
	integer argc=$#
	integer i=0

	if [[ $argc -lt 1 ]] ; then
		Usage
	fi

	while (($i < $argc)) ; do
		arg=$1
		shift
    		i=i+1

		if [[ $arg == '-name' ]] ; then
			if (( $i < $argc )) ; then
				RUNNAME=$1
				echo "RUNNAME=[$RUNNAME]"
				shift
				i=i+1
			else
				Usage
			fi
		elif [[ $arg == '-env' ]] ; then
			if (( $i < $argc )) ; then
				ENVFILE=$1
				shift
				i=i+1
			else
				Usage
			fi
		else
			Usage
		fi
	done

	if [[ $ENVFILE == "" ]] ; then
		Usage
	fi
}


################################################################################
ReadEnvFile() {
	. $ENVFILE

	
	echo ""
	echo "The following values have been set from $ENVFILE:"
	echo "MASTERLIST=$MASTERLIST"
	echo "TESTCASEDIR=$TESTCASEDIR"
	echo "XMERGE_JAR=$XMERGE_JAR"
	echo "APORTIS_JAR=$APORTIS_JAR"
	echo "WORDSMITH_JAR=$WORDSMITH_JAR"
	echo "MINICALC_JAR=$MINICALC_JAR"
	echo "RESULTSBASE=$RESULTSBASE"
	echo "PERL5LIB=$PERL5LIB"
	echo "POSE_EXE=$POSE_EXE"
	echo "POSE_PRC=$POSE_PRC"
	echo "TEST_DRIVER_PL=$TEST_DRIVER_PL"
	echo "COMPARATOR_PL=$COMPARATOR_PL"
	echo "XML_INFILE_DIR=$XML_INFILE_DIR"
	echo "PDB_BASELINE_DIR=$PDB_BASELINE_DIR"
	echo "XML_BASELINE_DIR=$XML_BASELINE_DIR"
	echo "EM_SCRIPT_HOME=$EM_SCRIPT_HOME"
	echo "QAWRAPPER_SCRIPT_HOME=$QAWRAPPER_SCRIPT_HOME"
	echo "EM_ROM_FILE=$EM_ROM_FILE"
	echo "EM_SESSION_FILE=$EM_SESSION_FILE"
	echo "QA_LIB_HOME=$QA_LIB_HOME"
	echo "QA_COMPARATOR_HOME=$QA_COMPARATOR_HOME"
	echo "CLASSES_DIR=$CLASSES_DIR"
	echo "COMPLIST=$COMPLIST"
}

################################################################################
POSESetup() {
	export PERL5LIB
	export EM_SCRIPT_HOME
	export QAWRAPPER_SCRIPT_HOME
	export EM_ROM_FILE
	export EM_SESSION_FILE
	export QA_LIB_HOME
	export QA_COMPARATOR_HOME
   	export CLASSES_DIR
	
}

################################################################################
TestSetup() {
	

	POSESetup

	export ZENDEBUG=1   

	COMPLIST="${COMPLIST}/tempcomp.${RUNNAME}.list"
	# create the directories for the results of this test run
	RESDIR="${RESULTSBASE}/${RUNNAME}"
	\rm -Rf $RESDIR
	mkdir $RESDIR

	# Define the directories for the test input files,
	# test output files, working directories and baseline files
	PDB_INFILE_DIR="${RESDIR}/pdb-orig"
	mkdir "${PDB_INFILE_DIR}"
	PDB_OUTFILE_DIR="${RESDIR}/pdb-new"
	mkdir "${PDB_OUTFILE_DIR}"
	XML_OUTFILE_DIR="${RESDIR}/xml-new"
	mkdir "${XML_OUTFILE_DIR}"

	LOGFILE="${RESDIR}/logfile"
	COMPLOG="${RESDIR}/complog"
	REPORT="${RESDIR}/report.html"
	StartReportFile

	echo "Results in: $RESDIR"
	echo "Report file: $REPORT"
}

################################################################################
TestCleanup() {
	EndReportFile
}

################################################################################
TestCaseSetup() {
	# where to pick up converter classes
	export CLASSPATH=""
	export CLASSPATH=$CLASSPATH:$XMERGE_JAR
	export CLASSPATH=$CLASSPATH:$APORTIS_JAR
	export CLASSPATH=$CLASSPATH:$WORDSMITH_JAR
	export CLASSPATH=$CLASSPATH:$MINICALC_JAR
}

################################################################################
TestCaseCleanup() {
	# empty function
	a=42
}

################################################################################
RunTestCase() {
	testcase=$1

	LogLine ""
	LogLine "test_driver output:"

	# run test_driver in foreground
	$TEST_DRIVER_PL\
		-pose-prc=${POSE_PRC}\
		-pose-exe=${POSE_EXE}\
		-xml-orig=${XML_INFILE_DIR}\
		-pdb-orig=${PDB_INFILE_DIR}\
		-pdb-new=${PDB_OUTFILE_DIR}\
		-xml-new=${XML_OUTFILE_DIR}\
		-test=$testcase -merge >> $LOGFILE 2>&1

	# cleanup in case zombie POSE processes are hanging around
	pkill pose
	pkill -9 pose
}


################################################################################
ComparisonSetup() {
	typeset file=$1

	
	export CLASSPATH="$CLASSES_DIR/xerces.jar"

	# create temporary comparator list file for this test case
	echo $file > $COMPLIST
}

################################################################################
ComparisonCleanup() {
	# remove temporary comparator list file used for this test case
	\rm -f $COMPLIST
}

################################################################################
RunComparison() {
	typeset type=$1

	LogLine ""
	LogLine "Comparator output:"
	$COMPARATOR_PL\
		-xml-orig=${XML_BASELINE_DIR}\
		-pdb-orig=${PDB_BASELINE_DIR}\
		-pdb-new=${PDB_INFILE_DIR}\
		-xml-new=${XML_OUTFILE_DIR}\
		-list=$COMPLIST -log=$COMPLOG -type=$type >> $LOGFILE 2>&1
#		-list=$COMPLIST -log=$COMPLOG -type=$type | tee -a $LOGFILE 2>&1

	pass=`grep TRUE $COMPLOG | wc -l`

	LogLine ""
	LogLine "COMPLIST file:"
	cat $COMPLIST >> $LOGFILE
	LogLine ""
	LogLine "Comparator logfile:"
	cat $COMPLOG >> $LOGFILE

	if [ $pass -eq 0 ]
	then
		TEST_COMMENTS="${TEST_COMMENTS}$type comparison ERROR<BR>"
		echo "$type comparison ERROR"
		return 0
	fi

	echo "$type comparison OK"
	return 1
}

################################################################################
CheckOutput() {
	typeset xmlfile="${XML_OUTFILE_DIR}/$1"
	typeset pdbfile="${PDB_INFILE_DIR}/$2"

	if [ ! -f $pdbfile ] ; then
		TEST_COMMENTS="${TEST_COMMENTS}[$pdbfile] does not exist<BR>"
		LogLine "ERROR: $pdbfile does not exist"
		echo "ERROR: $pdbfile does not exist"
		return 0
	fi

	if [ ! -f $xmlfile ] ; then
		TEST_COMMENTS="${TEST_COMMENTS}[$xmlfile] does not exist<BR>"
		LogLine "ERROR: $xmlfile does not exist"
		echo "ERROR: $xmlfile does not exist"
		return 0
	fi

	return 1
}

################################################################################
RunTest() {
	typeset testcasename
	typeset testcase
	typeset testfile
	typeset pdbfile
	typeset xmlfile
	typeset ext
	integer try
	integer finished_with_test
	integer test_pass

	TestSetup

	BGCOLOR=$ODD_BGCOLOR

	while read line ; do
		# get chars up to 1st space
		testcasename=${line%% *}
		testcase="${TESTCASEDIR}/$testcasename"

		# get 2nd word
		testfile=${line#* }
		testfile=${testfile%% *}

		# get last word
		ext=${line##* }

		LogLine "############################################"
		LogLine "Starting the following testcase"
		LogLine "testcase = $testcase"
		LogLine "testfile = $testfile"
		LogLine "ext = $ext"

		ReportLine "<TR BGCOLOR='${BGCOLOR}'>"
		ReportLine "<TD valign=top>$testcasename</TD>"
		ReportLine "<TD valign=top>$testfile</TD>"
		ReportLine "<TD valign=top>$ext</TD>"

		echo ""
		echo "testcase = $testcase"
		echo "testfile = $testfile"
		echo "ext = $ext"

		try=1
		finished_with_test=0
		TEST_COMMENTS=""

		while (($finished_with_test == 0)) ; do

			TestCaseSetup
			RunTestCase $testcase
			TestCaseCleanup

			xmlfile="${testfile}.${ext}"

			if [[ $ext == "sxc" ]] ; then
				pdbfile="${testfile}-Sheet1.pdb"
			else
				pdbfile="${testfile}.pdb"
			fi

			CheckOutput $xmlfile $pdbfile
			res=$?

			if [[ $res -eq 1 ]] ; then
				ComparisonSetup $pdbfile
				RunComparison pdb
				res=$?
# ignore result until pdb comparator is fixed...
res=1
				ComparisonCleanup
			fi

			if [[ $res -eq 1 ]] ; then
				ComparisonSetup $xmlfile
				RunComparison xml
				res=$?
				ComparisonCleanup
			fi

			if [[ $res -eq 1 ]] ; then
				TOTAL_PASS=TOTAL_PASS+1
				ReportLine "<TD valign=top BGCOLOR='${PASS_COLOR}'>PASS</TD>"
				ReportTestComments
				ReportLine "</TR>"
				LogLine "Test PASSED (on try $try)"
				echo "Test PASSED (on try $try)"
				finished_with_test=1
			else
				TEST_COMMENTS="${TEST_COMMENTS}error on try ${try}<BR>"
				LogLine "TEST FAILED (on try $try)"
				echo "TEST FAILED (on try $try)"

				if [[ $try -eq $MAX_RETRIES ]] ; then
					TOTAL_FAIL=TOTAL_FAIL+1
					ReportLine "<TD valign=top BGCOLOR='${FAIL_COLOR}'>FAIL</TD>"
					ReportTestComments
					ReportLine "</TR>"
					finished_with_test=1
				fi
			fi

			try=try+1
		done

		TOTAL_RUN=TOTAL_RUN+1

		# toggle BGCOLOR for next report line
		if [[ $BGCOLOR == $ODD_BGCOLOR ]] ; then
			BGCOLOR=$EVEN_BGCOLOR
		else
			BGCOLOR=$ODD_BGCOLOR
		fi

	done < $MASTERLIST

	ReportLine "</TABLE>"
	ReportLine "</CENTER>"

	TestCleanup

	echo "Total Tests PASSED: "${TOTAL_PASS}
	echo "Total Tests FAILED: "${TOTAL_FAIL}
	echo "Total Tests RUN: "${TOTAL_RUN}
	echo "Results in: $RESDIR"
	echo "Report file: $REPORT"
}

################################################################################
################################################################################
# main
################################################################################
################################################################################

GetParams $@
ReadEnvFile
RunTest

exit 0