#! /usr/bin/env perl

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

use strict;
use Cwd;
require 5.005;
use vars qw(@tests);

my $test_result = 0;
$|=1;

# Temporary Variables
my @temp;
my $testcount = 0;
my $passcount = 0;

# Tests To Be Run
my @tests = (
    "submit_test simple-script"
    );

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

# Run the tests
foreach(@tests) {
    $testcount++;
    @temp = split;
    print "Running Test Script: ./$temp[0]/$temp[1]\n";
    my $rc = system("./run-test-script.pl $_");
    # Any Failure Is A Global Failure
    if($rc == 0) {
         $passcount++;
         }

    # Capture Log/Stdout/Stderr Output From This Test Script
    if(!open(LOGFILE, "< ./$temp[0]/$temp[1].log")) {
        print DETAIL_LOG "ERROR: Can't Open Scipt Log File ./$temp[0]/$temp[1].log\n";
        }
    else {
        while(<LOGFILE>) {
            print DETAIL_LOG $_;
            }
        close LOGFILE;
        }
    }

# 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;
    }

# Print The Detail Log
print "\n-------------------------------\n";
print "Detailed Logs Follow:\n";
print "-------------------------------\n\n";

seek DETAIL_LOG, 0, 0;
while(<DETAIL_LOG>) {
    print $_;
    }
close DETAIL_LOG;


sub END {
    exit $test_result;
}

