#! /usr/bin/env perl

BEGIN { push(@INC, $ENV{GLOBUS_LOCATION} . "/lib/perl"); }

use strict;
use Cwd;
require 5.005;
use vars qw(@tests);
use IO;
 require "xml-tags.pl";

my $test_result = 0;
$|=1;

# Temporary Variables
my @temp;
my $testcount = 0;
my $passcount = 0;
my $indent    = 0;
my $TempDirectory = "";
# Tests To Be Run
my @tests = (
#   "submit_test lhs",
#   "submit_test lcs",
    "submit_test local-host-script",
    "submit_test lucky-pbs-script",
    "submit_test lucky-condor-script",
#   "submit_test local-proxy-script",
#   "submit_test lucky-pbs-script",
#   "submit_test ned-fork-script",
#   "submit_test ned-pbs-script",
#   "failure_test test-script"
    );

# Create and Open index.html for Appending
my $xmlfile = "$ENV{GLOBUS_LOCATION}/tmp/index.xml";
my $xfh = new IO::File;
if(!$xfh->open(">> $xmlfile")) {
    print "Unable To Open $xmlfile\n";
    exit(1);
    }

# Write Opening Tag and Date/Time Record
$indent = xml_write_open_tag($xfh,$indent,"TestRun");
my $datetime = localtime();
$indent = xml_write_field($xfh,$indent,"DateTime",$datetime);


# Open A Detail Log File
if(!open(DETAIL_LOG, "+> ./detail_log")) {
    print "Unable To Open Detail Log File\n";
    exit(1);
    }

# Open a Post Processing Script File
if(!open(MAKEHTML, "> $ENV{GLOBUS_LOCATION}/tmp/make-html")) {
    print "Unable To Open make-html File\n";
    exit(1);
    }

# Wrap index.xml File In Top Level Tags
# Note: Done this way to allow concatenation of index.xml with each test run
print MAKEHTML "echo \"\<File\>\" \> $ENV{GLOBUS_LOCATION}/tmp/_index.xml\n";
print MAKEHTML "cat $ENV{GLOBUS_LOCATION}/tmp/index.xml \>\> $ENV{GLOBUS_LOCATION}/tmp/_index.xml\n";
print MAKEHTML "echo \"\</File\>\" \>\> $ENV{GLOBUS_LOCATION}/tmp/_index.xml\n";

# Call Xalan to create top level html file from xml file
print MAKEHTML "java -classpath $ENV{GLOBUS_LOCATION}/lib/xercesImpl.jar:$ENV{GLOBUS_LOCATION}/lib/xalan.jar org.apache.xalan.xslt.Process -in $ENV{GLOBUS_LOCATION}/tmp/_index.xml -xsl index.xsl -out $ENV{GLOBUS_LOCATION}/tmp/index.html\n";

# Move the Stylesheets to the tmp directory
# system("cp index.xsl $ENV{GLOBUS_LOCATION}/tmp");
# system("cp detail-index.xsl $ENV{GLOBUS_LOCATION}/tmp");

# Run the tests
foreach(@tests) {
    # Write The Opening ScriptExecution Tag
    $indent = xml_write_open_tag($xfh,$indent,"ScriptExecution");

    # Bump Test Count, Grab Directory and File Name
    $testcount++;
    @temp = split;

    # Write FileName and Directory
    $indent = xml_write_field($xfh,$indent,"TestDirectory","$temp[0]");
    $indent = xml_write_field($xfh,$indent,"ExecutionScript","$temp[1]");

    # Report, Run the Test
    print "Running Test Script: ./$temp[0]/$temp[1]\n";
    my $rc = system("./run-test-script.pl $_");
    $rc = $rc >> 8;

    # Capture Return Code To XML File
    $indent = xml_write_field($xfh,$indent,"Result",$rc);

    # Bump Count
    if($rc == 0) {
         $passcount++;
         }

    # Capture The Output File From This Test Script
    if(!open(OUTPUTFILE, "< ./$temp[0]/$temp[1].out")) {
        print DETAIL_LOG "ERROR: Can't Open Scipt Output File ./$temp[0]/$temp[1].log\n";
        }
    else {
        my $line = <OUTPUTFILE>;
        chomp($line);
        $indent = xml_write_field($xfh,$indent,"TestName",$line);
        $line = <OUTPUTFILE>;
        chomp($line);
        $TempDirectory = $line;
        $indent = xml_write_field($xfh,$indent,"TempDirectory",$line);
        $line = <OUTPUTFILE>;
        chomp($line);
        $indent = xml_write_field($xfh,$indent,"ResultString",$line);
        close OUTPUTFILE;

        # Write the xml to html coversion command for this test group
        print MAKEHTML "java -classpath $ENV{GLOBUS_LOCATION}/lib/xercesImpl.jar:$ENV{GLOBUS_LOCATION}/lib/xalan.jar org.apache.xalan.xslt.Process -in $ENV{GLOBUS_LOCATION}/tmp/$TempDirectory/index.xml -xsl detail-index.xsl -out $ENV{GLOBUS_LOCATION}/tmp/$TempDirectory/index.html\n";

        # Write the chmod command to make temp directory acessbile on web
        print MAKEHTML "chmod a+x $ENV{GLOBUS_LOCATION}/tmp/$TempDirectory\n";
        }

    # Write The Closing ScriptExecution Tag
    $indent = xml_write_close_tag($xfh,$indent,"ScriptExecution");
    }

# Write Closing Tag
$indent = xml_write_close_tag($xfh,$indent,"TestRun");

# Check Results
print "-------------------------------\n";
print "$passcount of $testcount test scripts passed\n";
print "-------------------------------\n";
if($testcount == $passcount) {
    # Success
    $test_result = 0;
    }
else {
    # Failure
    $test_result = 1;
    }

# Close Open Files
close DETAIL_LOG;
close MAKEHTML;

# Source The Make HTML Script
system(". $ENV{GLOBUS_LOCATION}/tmp/make-html");

sub END {
    exit $test_result;
}
