#!/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";

#$fullPFX="full-";
#$basicPFX="basic-";
$tinyPFX="tiny-";

$svgDir='svg';


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

sub copyFiles
{

	foreach $line ( @manifest )
	{
		 splitManifestLine;
    	`cp $thisOriginal ../svg/$thisTest.svg`;
	}
}

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 "\t<br />\n\t<a href=\"$pfx$thisTest.html\">$thisTest</a>\n";
		print HTML "<li><a href=\"$pfx$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/full-index.html\">Full</a></li>\n";
#	print HTML "<li><a href=\"htmlObjectHarness/basic-index.html\">Basic</a></li>\n";
	print HTML "<li><a href=\"htmlObjectHarness/tiny-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/full-index.html\">Full</a></li>\n";
#	print HTML "<li><a href=\"htmlEmbedHarness/basic-index.html\">Basic</a></li>\n";
	print HTML "<li><a href=\"htmlEmbedHarness/tiny-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_FullTestSuite.tar.gz\">Full</a></li>\n";
	#print HTML "<li><a href=\"archives/W3C_SVG_11_BasicTestSuite.tar.gz\">Basic</a></li>\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/$fullPFX" . "index.html";
	#$title="SVG 1.1 Full test suite";
	#$pfx = $fullPFX;
	#@data = @fullData;
	#makeIndex();
	#close HTML;

	#open HTML, ">../htmlEmbedHarness/$basicPFX" . "index.html";
	#$title="SVG 1.1 Basic test suite";
	#$pfx = $basicPFX;
	#@data = @basicData;
	#makeIndex();
	#close HTML;

	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 )
	{
		splitManifestLine();
		#if( $thisProfile eq 'f' )
		#{
		#	push( @fullData, $line );
		#}
		#elsif( $thisProfile eq 'b' )
		#{
		#	push( @basicData, $line );
		#	push( @fullData, $line );
		#}
		#elsif( $thisProfile eq 't' )
		{
			push( @tinyData, $line );
			push( @basicData, $line );
			push( @fullData, $line );
		}
		#else
		#{
		#	print "\n\nDanger Will Robinson!!!, bad profile on manifest line {{{$line}}}\n\n";
		#}
	}
}

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

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

sub generateSVGTest
{
	$filename="../svgHarness/$pfx$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>";

	if( $thisProfile eq 't' )
	{
		if( $pfx eq "full-" )
		{
			print TEST "\n			<text y=\"$bottomNav\" x=\"10\" text-decoration=\"underline\" text-anchor=\"start\" >Full</text>";
			print TEST "\n			<a xlink:href=\"basic-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"75\" text-anchor=\"start\" >Basic</text>";
			print TEST "\n			</a>";
			print TEST "\n			<a xlink:href=\"tiny-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"150\" text-anchor=\"start\" >Tiny</text>";
			print TEST "\n			</a>        ";
		}
		if( $pfx eq "basic-" )
		{
			print TEST "\n			<a xlink:href=\"full-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"10\" text-anchor=\"start\" >Full</text>";
			print TEST "\n			</a>";
			print TEST "\n			<text y=\"$bottomNav\" x=\"75\" text-decoration=\"underline\" text-anchor=\"start\" >Basic</text>";
			print TEST "\n			<a xlink:href=\"tiny-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"150\" text-anchor=\"start\" >Tiny</text>";
			print TEST "\n			</a>        ";
		}
		if( $pfx eq "tiny-" )
		{
			print TEST "\n			<a xlink:href=\"full-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"10\" text-anchor=\"start\" >Full</text>";
			print TEST "\n			</a>";
			print TEST "\n			<a xlink:href=\"basic-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"75\" text-anchor=\"start\" >Basic</text>";
			print TEST "\n			</a>";
		}
	}
	if( $thisProfile eq 'b' )
	{
		if( $pfx eq "full-" )
		{
			print TEST "\n			<text y=\"$bottomNav\" x=\"10\" text-decoration=\"underline\" text-anchor=\"start\" >Full</text>";
			print TEST "\n			<a xlink:href=\"basic-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"75\" text-anchor=\"start\" >Basic</text>";
			print TEST "\n			</a>";
			print TEST "\n			<text y=\"$bottomNav\" x=\"150\" text-decoration=\"line-through\" text-anchor=\"start\" >Tiny</text>";
		}
		if( $pfx eq "basic-" )
		{
			print TEST "\n			<a xlink:href=\"full-$thisTest.svg\">";
			print TEST "\n				<text y=\"$bottomNav\" x=\"10\" text-anchor=\"start\" >Full</text>";
			print TEST "\n			</a>";
			print TEST "\n			<text y=\"$bottomNav\" x=\"75\" text-decoration=\"underline\" text-anchor=\"start\" >Basic</text>";
			print TEST "\n			<text y=\"$bottomNav\" x=\"150\" text-decoration=\"line-through\" text-anchor=\"start\" >Tiny</text>";
		}
	}
	if( $thisProfile eq 'f' )
	{
		print TEST "\n			<text y=\"$bottomNav\" x=\"10\" text-decoration=\"underline\" text-anchor=\"start\" >Full</text>";
		print TEST "\n			<text y=\"$bottomNav\" x=\"75\" text-decoration=\"line-through\" text-anchor=\"start\" >Basic</text>";
		print TEST "\n			<text y=\"$bottomNav\" x=\"150\" text-decoration=\"line-through\" text-anchor=\"start\" >Tiny</text>";
	}
	print TEST "\n		</g>";
	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')
	  {
	    #print "\nBefore trim: $line";
	    $line =~ s/[^<>]*>//;
	    #print "\nTrimmed line: $line";
	    $printing = 'true';
	  }

        if( $printing eq 'false' )
        {
            if( $line =~ /OperatorScript/ )
            {
	      #print "\nFound: $line";
	      if($line =~ />$/)
	      {
		$printing = 'true';
	      }	      
	      else
		{
		  $printing = 'notyet';
		}
	      }
	  }
        elsif( $printing eq 'true' )
        {
	    if( $line =~ /<\/[^>]*OperatorScript>/ )
            {
                return;
            }
            $line =~ s/<Paragraph>/<p>/;
            $line =~ s/<\/Paragraph>/<\/p>/;
            print TEST $line;
        }

    }
    
    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=\"$fullPFX$thisTest.html\">Full version</a>, ";
		#print TEST "<a href=\"$basicPFX$thisTest.html\">Basic version</a>, ";
		print TEST "<a href=\"$tinyPFX$thisTest.html\">Tiny version</a>";
	    print TEST "</p>\n";
	}

	#if( $thisProfile eq 'b' )
	#{
        #print TEST "<p>\n";
		#print TEST "<a href=\"$fullPFX$thisTest.html\">Full version</a>, ";
		#print TEST "<a href=\"$basicPFX$thisTest.html\">Basic 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/$pfx$thisTest.html";
    $header=$htmlToTitle;
    $svggenRef="   	    		  			<embed src=\"../$svgDir/$thisTest.svg\" width=\"$width\" height=\"$height\"/>\n";

    printHtml();

}

sub generateHtmlObjectTest
{
	$filename="../htmlObjectHarness/$pfx$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 full harness\n";
	#$height="360";
	#$width="480";
	#$indexName="full-index";
	#$pfx = $fullPFX;
	#@data = @fullData;
	#generateHarness();
	#print "generating basic harness\n";
	#$height="120";
	#$width="160";
	#$indexName="basic-index";
	#$pfx = $basicPFX;
	#@data = @basicData;
	#generateHarness();
	print "generating tiny harness\n";
	$height="360";
	$width="480";
	$indexName="tiny-index";
	$pfx = $tinyPFX;
	@data = @tinyData;
	generateHarness();
}

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