#!/usr/bin/perl
#
# $Revision: 1.4 $
#

$SVGToFilename="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--======================================================================-->\n<!--=  Copyright 2000 World Wide Web Consortium, (Massachusetts          =-->\n<!--=  Institute of Technology, Institut National de Recherche en        =-->\n<!--=  Informatique et en Automatique, Keio University). All Rights      =-->\n<!--=  Reserved. See http://www.w3.org/Consortium/Legal/.                =-->\n<!--======================================================================-->\n<!-- =====================================================================-->\n<!--                                                                      -->\n<!-- This page generated from script                                      -->\n<!--                                                                      -->\n<!-- Author : script: Rick Graham                                         -->\n<!--          layout: Vincent Hardy                                       -->        \n<!--                                                                      -->\n<!-- File: ";

require 'strings.inc';

@manifest=`cat manifest.dat`;

$mainIndex="../index";

$svgDir='svg';


sub splitManifestLine
{
	my @token = split /\t/, $line;
	$thisTest 		= $token[0];
	$thisProfile 	= $token[1];
	$thisOriginal 	= $token[2];
	$thisSection 	= $token[3];
	$thisLink 		= $token[4];
	$thisComment 	= $token[5];
	chop $thisComment;
}

sub makeIndex
{
	print HTML $htmlToTitle;
	print HTML $title;
	print HTML $htmlToBody;
	print HTML "<h1 class=\"pageTitle\">$title</h1>\n";
	print HTML "<p>Below are the list of tests in the $title. Each test \n";
	print HTML "has a link to different versions as well as navigation.</p>\n";
	$index = '0';

	print HTML "<ol>\n";
	while( $index <= $#data )
	{
		$line = $data[$index];
		splitManifestLine;
		print HTML "<li><a href=\"$thisTest.html\">$thisTest</a></li>\n";
		++$index;
	}
	print HTML "</ol>\n";

	print HTML "	</body>\n</html>\n";
}

sub makeTopLevel
{
	print HTML $htmlToTitle;
	print HTML $title;
	print HTML $htmlToBody;
	print HTML "<h1 class=\"pageTitle\">$title</h1>\n";
	print HTML "<h2>";
	print HTML `date "+%d %b %Y"`;
	print HTML "</h2>\n";

	print HTML "<p>Welcome to the SVG 1.2 Tiny Test Suite.</p>\n";

	print HTML "<p>See the matrix of test results in either\n";
	print HTML "<a href=\"status/matrix.html\">HTML</a>\n";
	print HTML "or <a href=\"status/matrix.svg\">SVG</a>.<p>\n";

	print HTML "<p>Tests in HTML using &lt;object&gt;:</p>\n";
	print HTML "<ul>\n";
	print HTML "<li><a href=\"htmlObjectHarness/index.html\">Tiny</a></li>\n";
	print HTML "</ul>\n";

	print HTML "<p>Tests in HTML using &lt;embed&gt;:</p>\n";
	print HTML "<ul>\n";
	print HTML "<li><a href=\"htmlEmbedHarness/index.html\">Tiny</a></li>\n";
	print HTML "</ul>\n";
	
	print HTML "<p>Download archives of the Testsuite</p>\n";
	print HTML "<ul>\n";
	print HTML "<li><a href=\"archives/W3C_SVG_11_TinyTestSuite.tar.gz\">Tiny</a></li>\n";
	print HTML "</ul>\n";
	

	print HTML "	</body>\n</html>\n";
}



sub makeIndexes
{
	print "generating html indexes.\n";

	open HTML, ">../htmlEmbedHarness/$tinyPFX" . "index.html";
	$title="SVG 1.2 Tiny test suite";
	$pfx = $tinyPFX;
	@data = @tinyData;
	makeIndex();
	close HTML;

	open HTML, ">../index.html";
	$title="SVG 1.2 Tiny Test Suite";
	makeTopLevel();
	close HTML;

    `cp ../htmlEmbedHarness/*index.html ../htmlObjectHarness`;

}


sub createListings
{
	foreach $line ( @manifest )
	{
		push( @tinyData, $line );
		#push( @basicData, $line );
		#push( @fullData, $line );
	}
}

sub setLast
{
	$line = $_[0];
	splitManifestLine;
	$lastTest = $thisTest;
}

sub setNext
{
	$line = $_[0];
	splitManifestLine;
	$nextTest = $thisTest;
}

sub generateSVGTest
{
	$filename="../svgHarness/$thisTest.svg";
	open TEST,  ">$filename";

	my $frameWidth = 10 + 2 * $width;
	my $svgWidth = $frameWidth + 20;
	my $svgHeight = $height + 103;
	my $center = $svgWidth / 2;
	my $rightColumn = $center + 5;
	my $rightCenter = $rightColumn + $width / 2;
	my $leftCenter = 10 + $width / 2;
	my $bottomNav = $svgHeight - 9;

	print TEST $SVGToFilename . $thisTest . "					-->";
	print TEST "\n<!-- =====================================================================-->";
	print TEST "\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">";
	print TEST "\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" height=\"$svgHeight\" width=\"$svgWidth\">"; 
	print TEST "\n	<image id=\"svgImage\" xlink:href=\"../$svgDir/$thisTest.svg\" height=\"$height\" width=\"$width\" y=\"77\" x=\"10\" />";
	print TEST "\n	<image id=\"refImage\" xlink:href=\"../png/$thisTest.png\" height=\"$height\" width=\"$width\" y=\"77\" x=\"$rightColumn\" />";
	print TEST "\n	<g font-weight=\"bold\" font-size=\"12\" text-anchor=\"middle\" fill=\"white\">";
	print TEST "\n		<rect x=\"10\" y=\"25\" width=\"$frameWidth\" height=\"25\" fill=\"black\" />";
	print TEST "\n		<text id=\"testName\" y=\"43\" x=\"$center\" text-anchor=\"middle\" font-size=\"14\" >$thisTest</text>";
	print TEST "\n		<rect x=\"10\" y=\"51\" width=\"$width\" height=\"25\" fill=\"#aaaaaa\" />";
	print TEST "\n		<text x=\"$leftCenter\" y=\"68\" text-anchor=\"middle\">SVG Image</text>";
	print TEST "\n		<rect x=\"$rightColumn\" y=\"51\" width=\"$width\" height=\"25\" fill=\"#aaaaaa\" />";
	print TEST "\n		<text x=\"$rightCenter\" y=\"68\" text-anchor=\"middle\">PNG Image</text>";
	print TEST "\n		<use xlink:href=\"#navigationGroup\" />";
	print TEST "\n	</g>";
	print TEST "\n	<defs>";
	print TEST "\n		<g id=\"navigationGroup\" font-weight=\"bold\" fill=\"#aaaaaa\" >";
	print TEST "\n			<a xlink:href=\"$pfx$lastTest.svg\">";
	print TEST "\n				<text y=\"16\" x=\"10\" text-anchor=\"start\" >Previous</text>";
	print TEST "\n			</a>";
	print TEST "\n			<a xlink:href=\"../$svgDir/$thisTest.svg\">";
	print TEST "\n				<text y=\"16\" x=\"85\" text-anchor=\"start\" >No Nav</text>";
	print TEST "\n			</a>";
	print TEST "\n			<a xlink:href=\"$pfx$nextTest.svg\">";
	print TEST "\n				<text y=\"16\" x=\"150\" text-anchor=\"start\" >Next</text>";
	print TEST "\n			</a>";
	print TEST "\n      </g>";
	print TEST "\n    </defs>";
	print TEST "\n</svg>";
}

sub printOperatorScript
{

 	my @lines = `cat ../svg/$thisTest.svg`;

    my $printing = 'false';
    
    foreach $line ( @lines )
    {
        if( $printing eq 'notyet')
        {
            $line =~ s/[^<>]*>//;
            $printing = 'true';
        }

        if( $printing eq 'false' )
        {
            if( $line =~ /OperatorScript/ )
            {
                #if($line =~ />$/)
                if($line =~ />/)
                {
                    $printing = 'true';
                }
                else
                {
                    $printing = 'notyet';
                }
            }
        }
        elsif( $printing eq 'true' )
        {
            if( $line =~ /<\/[^>]*OperatorScript>/ )
            {
                #print "Printing Status: $printing, Line: $line\n";
                return;
            }
            $line =~ s/<Paragraph>/<p>/;
            $line =~ s/<\/Paragraph>/<\/p>/;
            print TEST $line;
            #print "Printing Status: $printing, Line: $line\n";
            #$_ = <STDIN>;
        }
    }
    
    print "\nERROR transcribing operator script in file $thisTest.svg\n";

}

sub printHtml
{
	open TEST,  ">$filename";
	print TEST $header;
	print TEST "SVG 1.2 Tiny test:$thisTest";
	print TEST $htmlToBody;
	print TEST "\n	<div class=\"linkbar\"> \n";

	if( $thisProfile eq 't' )
	{
	    print TEST "<p>\n";
		print TEST "<a href=\"$thisTest.html\">Tiny version</a>";
	    print TEST "</p>\n";
	}

	print TEST "<p>Specification link: <a target=\"spec\" href=\"$thisLink\">$thisSection $thisComment</a></p>\n";

	print TEST " <p>\n";
	print TEST "    <a href=\"$pfx$lastTest.html\">$lastTest ←</a> \n";
	print TEST "    <a href=\"$indexName.html\">index</a>\n";
	print TEST "    <a href=\"$pfx$nextTest.html\">→ $nextTest</a>\n";
	print TEST "</p></div>\n";

	print TEST "\n	<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">\n";
	print TEST "		<tr>\n";
	print TEST "			<td align=\"center\" colspan=\"3\">\n";
	print TEST "				<table border=\"0\" cellpadding=\"8\">\n";
	print TEST "					<tr>\n";
	print TEST "						<td align=\"center\" colspan=\"2\" class=\"pageTitle\">\n";
	print TEST "							<h1>$thisTest</h1>\n";
	print TEST "						</td>\n";
	print TEST "					</tr>\n";
	print TEST "					<tr  class=\"navbar\">\n";
	print TEST "						<td align=\"center\">\n";
	print TEST "							SVG Image\n";
	print TEST "						</td>\n";
	print TEST "						<td align=\"center\">\n";
	print TEST "							PNG Image\n";
	print TEST "						</td>\n";
	print TEST "					</tr>\n";
	print TEST "					<tr>\n";
	print TEST "						<td align=\"right\">\n";
    print TEST $svggenRef;
	print TEST "						</td>\n";
	print TEST "						<td align=\"left\">\n";
	print TEST "							<img alt=\"raster image of $thisTest\" src=\"../png/$thisTest.png\" width=\"$width\" height=\"$height\"/>\n";
	print TEST "						</td>\n";
	print TEST "					</tr>\n";
	print TEST "				</table>\n"; 
	print TEST "			</td>\n"; 
	print TEST "		</tr>\n"; 
	print TEST "	</table>\n"; 


	print TEST "\n               \n<div class=\"opscript\">\n";
	printOperatorScript;
	
	print TEST $content;

	print TEST "\n		</div>\n";
	print TEST "<div class=\"linkbar\"> <p>\n";
	print TEST "				<a href=\"$pfx$lastTest.html\">$lastTest ←</a>\n";
	print TEST "				<a href=\"$indexName.html\">index</a>\n";
	print TEST "				<a href=\"$pfx$nextTest.html\">→ $nextTest</a>\n";
	print TEST "						</p></div>\n";
	print TEST "</body>\n</html>\n";

	close TEST;

}

sub generateHtmlEmbedTest
{
	$filename="../htmlEmbedHarness/$thisTest.html";
    $header=$htmlToTitle;
    $svggenRef="   	    		  			<embed src=\"../$svgDir/$thisTest.svg\" width=\"$width\" height=\"$height\"/>\n";

    printHtml();

}

sub generateHtmlObjectTest
{
    print "Processing: $thisTest\n";
	$filename="../htmlObjectHarness/$thisTest.html";
	$header=$htmlObjectToTitle;
	$svggenRef="   	    		  			<object data=\"../$svgDir/$thisTest.svg\" width=\"$width\" height=\"$height\" type=\"image/svg+xml\"><p style=\"font-size:300%;color:red\">FAIL</p></object>\n";

    printHtml();

}

sub generateHarness
{
	$index = '0';

	while( $index <= $#data )
	{
		if( $index == 0 )
		{
			setLast $data[$#data];
			setNext $data[$index+1];
		}
		elsif( $index == $#data )
		{
			setLast $data[$index-1];
			setNext $data[0];
		}
		else
		{
			setLast $data[$index-1];
			setNext $data[$index+1];
		}
		$line = $data[$index];
		splitManifestLine;
		generateHtmlEmbedTest; 
        generateHtmlObjectTest; 
		generateSVGTest; 
		++$index;
	}
}

	
sub generate
{
	print "generating tiny harness\n";
	$height="360";
	$width="480";
#	$indexName="tiny-index";
    $indexName="index";
	$pfx = $tinyPFX;
	@data = @tinyData;
	generateHarness();
}

mkdir '../htmlEmbedHarness', 0777;
mkdir '../htmlObjectHarness', 0777;
mkdir '../svgHarness', 0777;
createListings();
generate();
makeIndexes();

