#!/usr/bin/perl

my $status_header = "test_status_header.xml";
my $status_trailer = "test_status_trailer.xml";
my $status_xml_file = "../status/test_status.xml";
my $status_html_file = "../status/test_status12T.html";
my $get_status_xslt = "getTestStatus.xslt";
my $status2html_xslt = "testStatus2html.xslt";
my $test_file_list = "published_tests.txt";
my $feature_name = "ALL";
my $count;


#
#   Function to write out XML configuration file.
#
sub write_status_file
{
    open(STATUS_FH, ">>$status_xml_file") || die "Unable to open config $status_xml_file";
    print STATUS_FH @_;
    close(STATUS_FH);
}

open(HEADER_FH, $status_header) or die "\nERROR: could not open file $status_header\n";
my @header = <HEADER_FH>;
close(HEADER_FH);

write_status_file @header;
write_status_file "  <featureTests featureName=\"$feature_name\">\n";

open(LIST_FH, $test_file_list) or die "\nERROR: could not open file $test_file_list\n";
my @test_list = <LIST_FH>;
close(LIST_FH);

foreach $test_file (@test_list)
{
	my $xsltoutput = "";
	my $system_result = 0;
	
	chomp $test_file;
	print "processing test: $test_file\n";
	open(SYSTEM_COMMAND, "xsltproc --novalid $get_status_xslt ../svg/$test_file 2>&1 |");
    while (<SYSTEM_COMMAND>)
    {
		$xsltoutput = $_;
    }
    close(SYSTEM_COMMAND);
    
    $system_result = ($? >> 8);

    if ($system_result != 0)
    {
		print "ERROR: $test_file did not transform correctly\n";
		next;
	}
    
    write_status_file "    $xsltoutput";
    $count++;
}

open(TRAILER_FH, $status_trailer) or die "\nERROR: could not open file $status_trailer\n";
my @trailer = <TRAILER_FH>;
close(TRAILER_FH);

write_status_file "  </featureTests>\n";
write_status_file @trailer;

print "test_status.xml generated with $count tests\n";

system "xsltproc -o $status_html_file $status2html_xslt $status_xml_file";