#!/usr/local/bin/perl
#
# Arnaud Le Hors - lehors@w3.org
# $Id: htmlcat,v 1.4 2006-10-09 18:55:52 ihickson Exp $

use lib 'bin';
use utils;

$PROGNAME = substr($0, rindex($0, "/") + 1);

if (!$ARGV[0]) {
    print "Usage: $PROGNAME output file1 file2 ...\n";
    exit 1;
}

open(OUTPUT, "> $ARGV[0]");
shift;

# copy file in memory
sub readfile {
    $buf = "";
    if (!open(INPUT, $_[0])) {
	print STDERR "$PROGNAME Error: Cannot open file: $_[0]\n";
	return;
    }
    while (<INPUT>) {
	$buf .= $_;
    }
    close(INPUT);
}

# Convert a file name to the first anchor in that file (#q7.0)
sub guess_anchor {
  my ($file, $fragment) = @_;
  my $chapno = $lookup{$file};
  if (!defined $chapno) {return $file . $fragment;} # Not a chapter
  elsif (defined $fragment) {return $fragment;}	    # Fragment of a chapter
  else {return "#q" . $chapno . ".0";} # Guess anchor of first H1 in that file
}

# Read config file
read_config('Project.cfg');

# 1st file:
readfile($ARGV[0]);
$_ = $buf;
# Remove navbar
s/<div\b[^>]*\bclass=["']?navbar\b[^>]*>.*?<\/div>//sigo;
s/<hr\b[^>]*\bclass=["']?navbar\b[^>]*>//sigo;
# Fix local links: "foo.html" -> "#q7.0" and "foo.html#bar" -> "#bar"
s/(href=")([a-z0-9]+\.html)(#[a-z0-9.-]+)?"/$1.guess_anchor($2, $3).'"'/gie;
# take everything up to the body end tag or the end otherwise
if (/<\/body>/sio) {
    print OUTPUT $`;
} else {
    print OUTPUT $buf;
}

shift;

# then for each other given html file only take the body content
foreach $file (@ARGV) {
    readfile($file);
    $_ = $buf;
    # Remove navbar
    s/<div\b[^>]*\bclass=["']?navbar\b[^>]*>.*?<\/div>//sigo;
    s/<hr\b[^>]*\bclass=["']?navbar\b[^>]*>//sigo;
    # Fix local links: "foo.html#bar" -> "#bar"
    s/(href=")([a-z0-9]+\.html)(#[a-z0-9.-]+)?"/$1.guess_anchor($2, $3).'"'/gie;
    # Remove warning
    s/<!-- warning -->.*?<!-- \/warning -->//gs;
    # look for the beginning of the body content
    if (/<body>(.*)/sio) {
	$buf = $1;
    } elsif (/<\/head>/sio) {
	$buf = $';
    }
    # look for the end of the body content
    $_ = $buf;
    if (/<\/body>/sio) {
	print OUTPUT $`;
    } elsif (/<\/html>/sio) {
	print OUTPUT $`;
    } else {
	print OUTPUT $buf;
    }
}
# close body
print OUTPUT "</body>\n";
close(OUTPUT);
