#!/usr/bin/perl

@reports=`ls ../status/publish/*.xml`;
#@testNames=`ls ../svg/ | grep \"svg\$\" | cut -d '.' -f 1`;

@testNames=`cat published_tests.txt | cut -d '.' -f 1`;

$template = "matrixTemplate.html";

%full;
%basic;
%tiny;

$target = "../status/matrix.html";

sub setReportLine
{
	foreach $line ( @report )
	{
		if( $line =~ $_[0] )
		{
			return $line;
		}
	}
	return "";
}

sub getAttribute
{
	my @token=split /\"/,$_[0];
	my $i;
	
	for( $i = 0; $i < $#token; $i++ )
	{
		if( $token[$i] =~ $_[1] )
		{
			return $token[$i+1];
		}
	} 
	return "";
}

%company;

sub setCompanyData
{

	$companyDataLine=setReportLine "svg-status-query";
		print "\nCompany data is $companyDataLine ";

	$company=getAttribute $companyDataLine, " company=";
	$date=getAttribute $companyDataLine, " date=";
	$product=getAttribute $companyDataLine, " product=";
	$version=getAttribute $companyDataLine, " version=";
	$platform=getAttribute $companyDataLine, " platform=";
	$shortName=getAttribute $companyDataLine, " shortName=";
	$profile=getAttribute $companyDataLine, " profile=";
	
}


sub plotComment
{
	my $cellStyle = "";
	my $cellWidth = $colWidth-2;
	my $cellHeight=$rowHeight-2;
	
	if( length( $comment ) > 2 )
	{
		my $top=$y-$rowHeight - 20;
		my $center = $width / 2;
    	print SVG "\n\t\t<g visibility=\"hidden\" $popupGroupStyle transform=\"translate(0,$top)\">";
    	print SVG "\n\t\t\t<set begin=\"$id.mouseover\" attributeName=\"visibility\" to=\"visible\" end=\"$id.mouseout\"/>";
    	print SVG "\n\t\t\t<use xlink:href=\"#cmt-bg\"/>";
		#print SVG "\n\t\t\t<rect $popupRectStyle width=\"$width\" height=\"20\"/>";
    	print SVG "\n\t\t\t<text x=\"$center\" y=\"15\">$comment</text>";
    	print SVG "\n\t\t</g>";
	}

}

#<!--implementationTableHere-->
#<!--fullTableHere-->
#<!--basicTableHere-->
#<!--tinyTableHere-->

sub process
{
	if( $templateLine =~ "-implementationTableHere-" )
	{
		print "\ngenerating Implementor List.";
		print HTML "\n\t\t<table border=\"1\">";
		print HTML "\n\t\t\t<tr>  ";
		#print HTML "\n\t\t\t\t<th>&nbsp;</th>";
		print HTML "\n\t\t\t\t<th>Company</th>";
		print HTML "\n\t\t\t\t<th>Name</th>";
		print HTML "\n\t\t\t\t<th>Product</th>";
		print HTML "\n\t\t\t\t<th>Profile</th>";
		print HTML "\n\t\t\t\t<th>Version</th>";
		print HTML "\n\t\t\t\t<th>Platform</th>";
		print HTML "\n\t\t\t</tr>";

		foreach $report ( @reports )
		{
			print $report;
			chop $report;
			@report = `cat $report`;
			setCompanyData;
	#$company, $date, $product, $version, $platform, $shortName, $profile 
			print HTML "\n\t\t\t<tr>";
			#print HTML "\n\t\t\t\t<th>&nbsp;</th>";
			print HTML "\n\t\t\t\t<td>$company</td>";
			print HTML "\n\t\t\t\t<td>$shortName</td>";
			print HTML "\n\t\t\t\t<td>$product</td>";
			print HTML "\n\t\t\t\t<td>SVG $profile</td>";
			print HTML "\n\t\t\t\t<td>$version</td>";
			print HTML "\n\t\t\t\t<td>$platform</td>";
			print HTML "\n\t\t\t</tr>";
			if( $profile eq 'tiny' )
			{
				$tiny{$shortName} = $report;
				print "\nadding $shortName to tiny.";
			}
			elsif( $profile eq 'basic' )
			{
				$basic{$shortName} = $report;
				print "\nadding $shortName to basic.";
			}
			else
			{
				$full{$shortName} = $report;
				print "\nadding $shortName to full.";
			}

		}		
		
		print HTML "\n\t\t</table>";
	}
	elsif( $templateLine =~ "-tinyTableHere-" )
	{
		print "\ngenerating Tiny Table.";
        print HTML "\n\t\t<h3>SVG Tiny Profile Results</h3>";
        print HTML "\n\t\t<table border=\"1\">\n\t\t\t<tr>";
        print HTML "\n\t\t\t\t<th>Test&nbsp;Name</th>";

        @names = reverse( keys %tiny );
        foreach $name ( @names )
        {
            print HTML "\n\t\t\t\t<th align=\"center\">$name</th>";
        }
        print HTML "\n\t\t\t</tr>";
                #<th align="left">Comments</th>
        foreach $test ( @testNames )
        {
            if( substr( $test, -1 ) eq 't' )
            {
                print HTML "\n\t\t\t<tr>\n\t\t\t\t<td class=\"other\">$test</td>";
                foreach $name ( @names )
                {
                    $file = $tiny{$name};
                    $data = `grep $test $file`;
                    $result = getAttribute $data, "status=";
                    $token = ucfirst( lc $result );
                    if( length $token < 2 )
                    {
                        $token = "Unknown";
                    }
                    print HTML "\n\t\t\t\t<td class=\"$token\" align=\"center\">$token</td>";
                }
            }
            print HTML "\n\t\t\t</tr>";
        }
        print HTML "\n\t\t</table>";


	}
}


sub run
{
	@tplt = `cat $template`;
	open( HTML, ">$target");
	
	print "\ngenerating html matrix.";

    foreach $test ( @testNames )
    {
        chop $test;
    }

	foreach $templateLine ( @tplt )
	{
		if( $templateLine =~ /^<!--/ )
		{
			process;
		}
		else
		{
			print HTML $templateLine;
		}
	}

	close( HTML );
}

run;


