#!/usr/local/bin/perl -s

# Generate the single-file HTML representation of the PNG spec
# Usage: makesingle [-rfc] [-w3c] <master >output

$Chapter = 0;
$Section = 0;
$Paragraph = 0;

$pre_indent = 0;
$if_nest_ok = 0;
$if_nest_fail = 0;

while (<STDIN>) {
	&process_line($_);
}

exit;

sub process_line {
	local($line) = @_;

	# Handle conditional exclusion commands, doing nesting correctly
	local($skip_this_line) = 0;

	if ($line =~ /^<!-- IF RFC -->/i) {
	    if ($rfc && !$if_nest_fail) { $if_nest_ok++; }
	    else { $if_nest_fail++; }
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- IF !RFC -->/i) {
	    if (!$rfc && !$if_nest_fail) { $if_nest_ok++; }
	    else { $if_nest_fail++; }
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- IF W3C -->/i) {
	    if ($w3c && !$if_nest_fail) { $if_nest_ok++; }
	    else { $if_nest_fail++; }
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- IF !W3C -->/i) {
	    if (!$w3c && !$if_nest_fail) { $if_nest_ok++; }
	    else { $if_nest_fail++; }
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- ENDIF -->/i) {
	    if ($if_nest_fail) { $if_nest_fail--; }
	    else { $if_nest_ok--; }
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- NROFF/i) {
	    $if_nest_fail++;
	    $skip_this_line = 1;
	}
	if ($line =~ /^NROFF -->/i) {
	    $if_nest_fail--;
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- TEX/i) {
	    $if_nest_fail++;
	    $skip_this_line = 1;
	}
	if ($line =~ /^TEX -->/i) {
	    $if_nest_fail--;
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- IF !TEX -->/i) {
	    if (!$if_nest_fail) { $if_nest_ok++; }
	    else { $if_nest_fail++; }
	    $skip_this_line = 1;
	}
	if ($line =~ /^<!-- IF HTML -->/i) {
	    if (!$if_nest_fail) { $if_nest_ok++; }
	    else { $if_nest_fail++; }
	    $skip_this_line = 1;
	}

	if ($if_nest_fail || $skip_this_line) {
	    if ($if_nest_fail < 0 || $if_nest_ok < 0) {
		die "Bogus if/endif matching\n";
	    }
	    return;
	}

	# handle inclusion
	if ($line =~ /<!-- INCLUDE (\S+) -->/i) {
	    if (open(INPUT2, $1)) {
		while (<INPUT2>) {
		    &process_line($_);
		}
		close INPUT2;
	    } else {
		warn "Couldn't open $1: $!\n";
	    }
	    return;
	}

	# ignore other special commands
	if ($line =~ /<!-- (.+) -->/) {
	    warn "Ignoring command $line" if $debug;
	    return;
	}

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

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

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

	# handle PRE tag
	if ($line =~ m!</PRE>!) {$pre_indent = 0;}
	if ($pre_indent) {$line =~ s|^|   |;}
	if ($line =~ /<PRE>/) {$pre_indent = 1;}

	# note: ignore HREFs containing / or @

	# fix chapter crossreferences
	$line =~ s|HREF="([^"/@#\.]+)"|HREF="#$1"|gi;
	# fix general section crossreferences
	$line =~ s|HREF="([^"/@#]+)#([^"/@#]+)"|HREF="#$2"|gi;
	# fix abbreviated section crossreferences
	$line =~ s|HREF="([^"/@#\.]+)\.([^"/@#]+)"|HREF="#$1.$2"|gi;

	print STDOUT $line;
}
