#!/usr/bin/perl

require 'strings.inc';


$name='xxx';
$number='0';
$test_file_list = "published_tests.txt";

# reads in the directory.dat file.
sub readDirectoryInformation
{
# read in the manifest
    my $manifest='directory.dat';

    open( DIR, $manifest )
        or die "\nERROR: could not open file $manifest\n";

    my @dir = <DIR>;
    close( DIR );
    my $chapter = '0';
    my $section = '0.0';
    my $specname="";

    foreach $line ( @dir )
    {
# each valid line in the manifest will have three tab delimited fields
        my @token = split /\t/, $line;
        if( $#token >= 2 )
        {
# the name of this chapter or sector is first
            my $shortname = $token[0];
            #print "---start---\n";
            #print "$shortname\n";
# the sector code is second, if it starts with a period it is a subsection
            my $sector = $token[1];
            #print "$sector\n";
# the label is third
            my $lable = $token[2];
# create the section number, chapter . sector of just sector
            if( $sector =~ /^\./ )
            {
                $section = $chapter . $sector;
                chomp $lable;
                #print "lable: $lable\n";
            }
            else
            {
                $chapter = $sector;
                $section = $chapter;
                $specname =$token[3];
                chomp $specname;
                #print "spec: $specname\n";
            }
            #print "$lable, $section, $shortname, $sector, $specname\n";

            $titles{$shortname}=$lable;
            $sections{$shortname}=$section;
            $names{$section}=$shortname;
            $linkLookup{$shortname}=$sector;
            $htmlFile{$shortname}=$specname;
            #print "$titles{$shortname}, $sections{$shortname}, $names{$section}, $linkLookup{$shortname}, $htmlFile{$shortname}\n";
            #$_ = <STDIN>;
            #print "$htmlFile{$shortname}\n";
        }
    }
}



sub splitfile
{   
    #@tok = split /\//, $file;
    #@tok = split /\./, $tok[2];
    #@tok = split /\-/, $tok[0];
    
    @name_tok = split /\-/, $file;
    @prof_tok = split /\./, $name_tok[3];

    $chapter = $name_tok[0];
    $section = $name_tok[1];
    $index = $name_tok[2];
    $profile = $prof_tok[0];
    #print "chapter: $chapter, section: $section, index: $index, profile: $profile\n";
    #$_ = <STDIN>;
}

sub getSpecData
{
    $f="../../publish/$htmlfilename.html";
    my @links=`cat $f`;

    $linenumber= substr( $linkLookup{$hashvalue}, 1 ) - 1;

    my @ltok=split /\t/, $links[$linenumber];

    $specURL="$pathToSpec$htmlfilename.html$ltok[1]";
    #chop( $specURL );
    #chop( $specURL );
    $specTitle=$titles{$hashvalue};
    $specSection=$sections{$hashvalue};
}


sub manifesto
{
    $hashvalue="$chapter-$section";
    $htmlfilename=$htmlFile{$hashvalue};
    
    #print "$hashvalue\n";
    #print "$htmlFile{$hashvalue}\n";
    #$_ = <STDIN>;

    if ($htmlfilename eq "")
    {
        #print "$hashvalue not found ";
        $hashvalue = $chapter;
        $htmlfilename=$htmlFile{$hashvalue};
    }

    #print "$hashvalue, $htmlfilename\n";

    #@tok = split /\//, $file;
    #@tok = split /\./, $tok[2];
    @test_tok = split /\./, $file;
    $test_name = $test_tok[0];
    
    getSpecData();
    print DATA "$test_name\t$profile\t$test_name.svg\t$specSection\t$specURL\t$specTitle\n";
    print HTML "\t<br>$test_name generated from $test_name.svg, Spec url = <a href=\"$specURL\" target=\"spec\">$specURL</a>\n";    
    #print DATA "$filename\t$profile\t$file\t$specSection\t$specURL\t$specTitle\n";
    #print HTML "\t<br>$filename generated from $file, Spec url = <a href=\"$specURL\" target=\"spec\">$specURL</a>\n";
}


sub buildLinks
{
    open(LIST_FH, $test_file_list) or die "\nERROR: could not open file $test_file_list\n";
    @files = <LIST_FH>;
    close(LIST_FH);
    #@files=`ls ../svg/*.svg`;
    #@files=`cat published_tests.txt | sed 's%^%../svg/%'`;
    open( DATA, '>manifest.dat' );
    open( HTML, '>manifest.html' );

    print HTML $htmlToTitle;
    print HTML '1.2 Tiny Test Suite Manifest';
    print HTML $htmlToBody;

    foreach $file ( @files )
    {
        chop( $file );
        print "Processing: $file\n";
        splitfile();
        manifesto();
    }
    print HTML "<\html>\n";
    close DATA;
    close HTML;
}

print "=== Generating manifest.dat and manifest.html ===\n";
readDirectoryInformation();
buildLinks();
