#!/bin/sh
# Copyright 1999-2002 Carnegie Mellon University.  
# Portions Copyright 2002 Sun Microsystems, Inc.  
# Portions Copyright 2002 Mitsubishi ElectricResearch Laboratories.
# All Rights Reserved.  Use is subject to license terms.
# 
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL 
# WARRANTIES.
#
# This script performs the highlevel coordination for the regression
# tests which includes building S4, runnng the tests, logging the
# results, putting the results out to CVS and performing email
# notification
#
# This script should be run from the regression test directory
#
# usage: executeRegressionTest [testType [qsub]]
#
# where testType is either:
#
# 	standard
#	quick
#  	an4
#       an4_full
#       an4_plus
#       an4_spelling
#       an4_words
#       rm1
#       ti46
#       tidigits
#
# more tests can be added by adding a script called runTests_XXX to
# the regression test directory, where XXX is the name of the new test.
#


regressionDir=`pwd`

testType=${1:-standard}
batchMode=${2:-normal}

echo executeRegressionTest $testType $batchMode

# We can run full tests or quick tests. Quick tests
# are used for testing the regression tests
#

runTests="runTests_"$testType

###################
# Since the scripts just came out of CVS, they may not
# be executable yet.  So make them executable
#
scripts="getTime getHostname mailReport makeReport \
    parseS4Output parseS4LiveOutput regressionTest\
    runTest runLiveTest s4_align s4_align_sum collectMetrics \
    getTime makeWebpage pushChanges pushReport whatsNew \
    cvs_history updateLog finalizeLog \
    s4_align s4_align_sum archiveTestResults \
    runTests_friday runTests_monday runTests_nightly \
    runTests_quick runTests_saturday runTests_sunday \
    runTests_thursday runTests_tuesday runTests_wednesday \
    runTests_live makeTestDetailReport pushAllTestReports getJavaVersion \
    makeTestSummaryReport makeTestReports waitForFile pushDocCheck"

chmod +x $scripts

rootDir=../../
#rootDir=/home/plamere/s4test/
date=`date +%Y-%m-%d`
time=`date +%T`
uptime=`uptime`
javaVersion=`./getJavaVersion`


#############################
# Log the metrics
#############################
# Collect the metrics first, before we muddy up the work space
#
cd $regressionDir
machine=`./getHostname`
echo $date > .startDate
echo $time > .startTimeOfDay
./collectMetrics | $regressionDir/updateLog

##########################
# then we build the world
##########################
cd $rootDir
who=`whoami`

startBuildTime=`$regressionDir/getTime`
if ant all
then
  status="OK";
else
  status="FAILED";
fi
endBuildTime=`$regressionDir/getTime`

#totalBuildTime=$((endBuildTime - startBuildTime))
totalBuildTime=`expr $endBuildTime - $startBuildTime`

############################################
# If the build succeeded, run the doc check
###########################################
$regressionDir/pushDocCheck

#########################################
# If the build succeeded, run the tests
########################################
cd $regressionDir

if [ $status = "OK" ]; then
   echo running tests from $regressionDir/$runTests

   case $batchMode in

       normal) 
		echo Normal mode;
       		echo $regressionDir/$runTests;
       		$regressionDir/$runTests;;

       batch) 
	      echo batch mode;
	      rm -f $regressionDir/.testDone;
	      echo qsub -q s4 $regressionDir/$runTests; 
	      qsub -q s4 $regressionDir/$runTests; 
	      echo waitForFile $regressionDir/.testDone;
	      $regressionDir/waitForFile $regressionDir/.testDone;;
   esac

   startRunTime=`cat $regressionDir/.startTime`
   endRunTime=`$regressionDir/getTime`
   totalRunTime=`expr $endRunTime - $startRunTime`
else
   echo build failure - tests not run
   totalRunTime="N/A"
fi

#############################
# Log the environment  information
# (This should be done before the build info)
#############################
cd $regressionDir
echo "environment|$date|$machine|$JAVA_HOME|$javaVersion|$uptime|" | $regressionDir/updateLog


#############################
# Log the build information
#############################
cd $regressionDir
echo "build|$date|$time|$machine|$who|make|$status|$totalBuildTime|$totalRunTime|$testType|" | $regressionDir/updateLog

#############################
# All log updates are done so finalize the log
#############################
cd $regressionDir
./finalizeLog

#################################
# archive the interesting files (.out, .log etc)
#################################
errorLog=$regressionDir/error.log
$regressionDir/archiveTestResults $errorLog $testType

#################################
# Generate the email notification
#################################
$regressionDir/mailReport $errorLog


###############################
# Generate the regression test web page
#################################A
$regressionDir/pushReport
# Changes now generated from a perl script
# $regressionDir/pushChanges

###############################
# Generate the system description web page
#################################A
$regressionDir/pushSystemDescription

###############################
# Generate the test results
#################################
cd $regressionDir
$regressionDir/pushAllTestReports
