#!/usr/local/bin/perl

# Generate the a perls script to show cross refernces in the PNG spec
# Usage: makeshowxrefs <master >showxrefs; showxrefs <master >output

$inchapter = 0;
$inlist = 0;

$Chapter = -1;
$Section = 0;
$Paragraph = 0;

print STDOUT "\#!/usr/local/bin/perl\n";
print STDOUT "while (<STDIN>) {\n";
while (<STDIN>) {

    # handle H2 tag
    if (/<H2>.+NAME.+>/i) {
         $Chapter++;
         $Section = '0';
         $Paragraph = '0';
	 s|(<H2>[^>]+>)|$1$Chapter. |i;
    }

    # handle H3 tag
    if (/<H3>.+NAME.+>/i) {
         $Section++;
         $Paragraph = '0';
	 s|(<H3>[^>]+>)|$1$Chapter.$Section. |i;
    }

    # handle H4 tag
    if (/<H4>.+NAME.+>/i) {
         $Paragraph++;
	 s|<H4>|<H4> $Chapter.$Section.$Paragraph. |;
    }

    # handle DT tag
    if (/<DT>.+NAME.+>/i) {
         $Paragraph++;
	 s|(<DT>[^>]+>)|$1$Chapter.$Section.$Paragraph. |i;
     }

    if (m|<H2><A NAME="([^"]+)">([^ ]+)\. (.+)</A></H2>|i) {
        print STDOUT "s|<A HREF=\"$1\">|<A HREF=\"$1\">Chapter $2, |i;\n";
    }
    if (m|<H3><A NAME="([^"]+)">([^ ]+)\. (.+)</A></H3>|i) {
        print STDOUT "s|<A HREF=\"$1\">|<A HREF=\"$1\">Section $2, |i;\n";
    }
    if (m|<H4><A NAME="([^"]+)">([^ ]+)\. (.+)</A></H3>|i) {
        print STDOUT "s|<A HREF=\"$1\">|<A HREF=\"$1\">Paragraph $2, |i;\n";
    }

    if (m|<DT><A NAME="([^"]+)">([^ ]+)\. (.+)</A>|i) {
        print STDOUT "s|<A HREF=\"$1\">|<A HREF=\"$1\">Paragraph $2, |i;\n";
    }

}

print STDOUT "print STDOUT\n";
print STDOUT "}\n";
