#!/usr/bin/perl

@reports=`ls ../status/publish/*.xml`;
#@tests=`ls ../svg/ | grep \"svg\$\"`;
@tests=`cat published_tests.txt`;

$target="../status/matrix.svg";
$rows=$#tests + 1;
$cols=$#reports + 1;
$colWidth=30;
$rowHeight=15;
$leftMargin=200;
$topMargin=100;
$width=$leftMargin+($colWidth*($cols+1));

if( $width < 400 )
{
    $width=400;
}

$height=$topMargin+($rowHeight*$rows);
$popupGroupStyle=" opacity=\".85\" stroke=\"none\" fill=\"olive\" ";
$popupRectStyle=" stroke=\"olive\" fill=\"white\" ";


sub plotVerticalGridLine
{
    $xOffset=$x+$colWidth;
    print SVG "M$xOffset,0 $x,$topMargin v$height";
}


sub plotHorizontalGridLine
{
    print SVG "M0,$y h$right"; 
}


sub doWrap
{
    $wrap++;
    if( $wrap%10==0 )
    {
        print SVG "\n\t\t";
    }
}


sub plotGrid
{
    print SVG "\n\t<path id=\"grid\" stroke=\"olive\" fill=\"none\"\n\t\td=\"";
    my $i=0;
    my $wrap=1;
    my $iter=$cols;
    for( $i = 0; $i <= $iter; $i++ )
    {
        $x = $i * $colWidth + $leftMargin;
        plotVerticalGridLine;
        doWrap;
    }

    $iter=$rows;
    $right=$width-$colWidth;
    $style="";

    for( $i = 0; $i <= $iter; $i++ )
    {
        $y = $i * $rowHeight + $topMargin;
        plotHorizontalGridLine;
        doWrap;
    }

    print SVG "\"/>";

}


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";
    $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 plotCompanyData
{
    print SVG "\n\t\t<text id=\"$shortName\" x=\"$x\" y=\"$y\" transform=\"rotate(-74,$x,$y)\">$shortName</text>";
    my $left=$x-$leftMargin + 5;
    my $top=$topMargin+5;
    my $popupWidth=$leftMargin+$colWidth;

    print SVG "\n\t\t<g visibility=\"hidden\" $popupGroupStyle transform=\"translate($left,$top)\">";
    print SVG "\n\t\t\t<set begin=\"$shortName.mouseover\" attributeName=\"visibility\" to=\"visible\" end=\"$shortName.mouseout\"/>";
    print SVG "\n\t\t\t<rect $popupRectStyle width=\"$popupWidth\" height=\"95\"/>";
    print SVG "\n\t\t\t<g text-anchor=\"end\">";
    print SVG "\n\t\t\t\t<text x=\"60\" y=\"15\">company:</text>";
    print SVG "\n\t\t\t\t<text x=\"60\" y=\"30\">date:</text>";
    print SVG "\n\t\t\t\t<text x=\"60\" y=\"45\">product:</text>";
    print SVG "\n\t\t\t\t<text x=\"60\" y=\"60\">version:</text>";
    print SVG "\n\t\t\t\t<text x=\"60\" y=\"75\">platform:</text>";
    print SVG "\n\t\t\t\t<text x=\"60\" y=\"90\">profile:</text>";
    print SVG "\n\t\t\t</g>";
    print SVG "\n\t\t\t<g text-anchor=\"start\" fill=\"black\">";
    print SVG "\n\t\t\t\t<text x=\"65\" y=\"15\">$company</text>";
    print SVG "\n\t\t\t\t<text x=\"65\" y=\"30\">$date</text>";
    print SVG "\n\t\t\t\t<text x=\"65\" y=\"45\">$product</text>";
    print SVG "\n\t\t\t\t<text x=\"65\" y=\"60\">$version</text>";
    print SVG "\n\t\t\t\t<text x=\"65\" y=\"75\">$platform</text>";
    print SVG "\n\t\t\t\t<text x=\"65\" y=\"90\">$profile</text>";
    print SVG "\n\t\t\t</g>";
    print SVG "\n\t\t</g>";
}


sub plotCompanies
{
    my $iter=$cols-1;
    my $i;
    $x=$leftMargin + 23;
    $y=$topMargin - 10;
    print SVG "\n\t<g id=\"implementor-data\">";

    for( $i = 0; $i <= $iter; $i++ )
    {
        @report=`cat $reports[$i]`;
        setCompanyData;
        plotCompanyData;
        $x += $colWidth;
    }
    print SVG "\n\t</g> <!-- id=\"implementor-data\" -->";

}


sub plotTestNames
{
    $y=$topMargin+12;
    $x=$leftMargin + 23;
    print SVG "\n\t<g id=\"test-names\" stroke=\"none\" fill=\"black\">";
    foreach $test ( @tests )
    {
        my @tok=split /\./,$test;
        print SVG "\n\t\t<text x=\"50\" y=\"$y\">$tok[0]</text>";
        $y+=$rowHeight;
    }

    print SVG "\n\t</g> <!-- id=\"test-names\" -->";
}



sub plotCell
{
    my $cellStyle = "";
    my $cellWidth = $colWidth-2;
    my $cellHeight=$rowHeight-2;

    $setId = " id=\"$id\" ";

    if( length( $comment ) < 3 )
    {
        $setId="";
    }

    if( $status eq 'OK' )
    {
        $cellStyle=" fill=\"#0A0\" ";
    }
    elsif( $status eq 'PARTIAL' )
    {
        $cellStyle=" fill=\"#FA0\" ";
    }
    elsif( $status eq 'FAIL' )
    {
        $cellStyle=" fill=\"#F00\" ";
    }

    elsif( $status eq 'BUG' )
    {
        $cellStyle=" fill=\"#00F\" ";
    }
    else
    {
        $cellStyle=" fill=\"#000\" ";
    }

    print SVG "\n\t\t<rect $setId $cellStyle x=\"$x\" y=\"$y\" width=\"$cellWidth\" height=\"$cellHeight\"/>";
}


sub plotData
{
    my $iter=$cols-1;
    my $i;
    
    $x=$leftMargin + 1;
    
    for( $i = 0; $i <= $iter; $i++ )
    {
        @report=`cat $reports[$i]`;
        setCompanyData;
        print SVG "\n\t<g id=\"$shortName-data\" text-anchor=\"middle\">";

        $y=$topMargin + 1;
        $index=0;

        foreach $test ( @tests )
        {
            $id="$shortName-$index";
            
            my @tok=split /\./,$test;
            
            $testName = $tok[0];
            $testData=setReportLine $testName;

            if( length( $testData ) > 0 )
            {
                $status = getAttribute $testData, " status=";
                $comment = getAttribute $testData, " comment=";
                plotCell;
            }

            $index++;
            $y+=$rowHeight;

        }

        print SVG "\n\t</g> <!--id=\"$shortName-data\" -->";
        $x += $colWidth;
    }
}


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


sub plotComments
{
    my $iter=$cols-1;
    my $i;

    $x=$leftMargin + 1;

    for( $i = 0; $i <= $iter; $i++ )
    {
        @report=`cat $reports[$i]`;
        setCompanyData;
        print SVG "\n\t<g id=\"$shortName-comments\" text-anchor=\"middle\">";

        $y=$topMargin + 1;
        $index=0;

        foreach $test ( @tests )
        {
            $id="$shortName-$index";

            my @tok=split /\./,$test;

            $testName = $tok[0];
            $testData=setReportLine $testName;

            if( length( $testData ) > 0 )
            {
                $status = getAttribute $testData, " status=";
                $comment = getAttribute $testData, " comment=";

                plotComment;
            }

            $index++;
            $y+=$rowHeight;
        }

        print SVG "\n\t</g> <!--id=\"$shortName-comments\" -->";

        $x += $colWidth;
    }
}


sub openFile
{
    open( SVG, ">$target" );

    print SVG "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    print SVG "\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">";
    print SVG "\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
    print SVG " id=\"svg-1.2-status-matrix\" width=\"$width\" height=\"$height\">";
    
    plotGrid;
    plotTestNames;
    plotData;
    plotCompanies;
    plotComments;

}


sub closeFile
{
    print SVG "\n</svg>\n";
    close( SVG );
}


sub run
{
    openFile;

    $col = 0;
    
    foreach $report ( @reports )
    {
        chomp $report;

        @rep=`cat $report`;
        $row = 0;

        foreach $test ( @tests )
        {
            $row++;
        }

        $col++;
    }
    
    closeFile;
}

run;