#!/bin/perl
# 
# concat.pl $Id: concat.pl,v 1.2 2002/06/19 16:39:34 lmiller Exp $
# Dan Brickley <danbri@w3.org>
# A fearful hack.

# mangles together a heap of original and derrived HTML docs.

use strict;

my $cfg = shift || 'conf/concat.txt';
my $debug=1;
#my $base = '../../summary/';
my $base = '';
my $out = '';

my %toc;

$out .= "<html><head><title>SWAD-Europe: Description of Work</title>\n\n";
$out .= "<link rel=\"StyleSheet\" type=\"text/css\"  href=\"style/swad-europe.css\"  />\n";
$out .= "</head><body bgcolor=\"#ffffff\">\n\n <!-- end boilerpate --> \n\n\n\n\n";

my @parts;

my @sources;
my %bysource;

my $chart;
open (IN, $cfg) || die "no config $cfg";
while(<IN>) { 
  next if (m/^#/);
  next unless (m/\S/);
  chomp;
#  print STDERR "Debug: $_ \n";
  $chart='';
  my $fileinfo=$_; 
  $fileinfo =~ m/^(\S+)\s*(.*)/;
  my $fn=$1; 
  my $meta=$2;
  # print STDERR "CONCAT: Parts: $fn meta: $meta\n";
  $meta =~ m/T=(.+)N=*/i;
  my $title=$1 || 'no_title';

  $meta =~ m/N=(\S+)\s*/i;
  my $n=$1 || '0';

  print STDERR "concat.pl: Parts: $fn meta: $meta Title=$title Num=$n\n";

  push (@sources,$fn);
  my $file = $base . $fn;
 
  $toc{$n} = $fn. ' '. $title; 

  if (-e $file) {
  #  print STDERR "Exists! loading and processing $file\n" if $debug;
  my $wp = $1 if  ($file =~ m#esw-wp-(\d.*)\.html#i);

  my $LINKIMG=0; # don't do this any more (pics too big)
  # special case stuff if we identify a workpackage
  if ($LINKIMG) {
	 print STDERR "File $file is a workpackage: $wp \n";
	#crossref to files such as:views/img/chart_esw-wp-3.svg or .png
	my $imgbase = "views/img/_chart_esw-wp-$wp";
	my $png = $imgbase . '.png';
	my $svg = $imgbase . '.svg';
	print STDERR "Checking $png\n" if $debug;
	$chart .= "<a name=\"desc-esw-wp-$wp\" id=\"desc-esw-wp-$wp\" />\n\n"; # not really a chart
	if (-e $png ) {
	  # *if* we have an image... (single partner WPs don't...), see extract_projdata.pl for details
	  #
  	  $chart = "\n\n<hr />\n\n<img src=\"$imgbase.png\" \
	 	alt=\"pie chart of effort breakdown for esw-wp-$wp \" align=\"right\" />\n\n";
	  $chart .= "\n(<a href=\"$svg\">svg</a>) \n\n\n";
#	  print STDERR "Matched WP $wp to image $imgbase (png/svg) \n" if $debug;
	}

      }

      my $ch;
      open (CHAPTER, $file) || die "Can't open chapter $file";
      while (<CHAPTER>) { 
	  next if (m/xml version=/);
	  next if (m/DOCTYPE/);
	  next if (m/Transitional\/\//);
	  s/<table>/<table summary="overview">/gi;
	  $ch .= $_; 
      }
      close CHAPTER;


      # Some clumsy hacks that try to junk HTML head and other per-doc stuff
      #
      $ch =~ s#<html\s*([^>]+)>(.*)<body\s*([^>]+)># \n\n<!-- begin file: $file -->\n\n#igs;
      $ch =~ s#<\?xml version="1.0"\?>##gis;
      $ch =~ s#<title>([^<]*)</title>##gsi;
      #<meta http-equiv="Content-Type" content="text/html" />
      $ch =~ s#<meta\s*([^>]*)>##gsi;
      #<link rel="StyleSheet" type="text/css" href="style/swad-europe.css" />
      $ch =~ s#<link\s*([^>]*)>##gsi;
      #<html xmlns="http://www.w3.org/1999/xhtml">
      $ch =~ s#<html\s*([^>]*)>##gsi;
      $ch =~ s#"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">##gi;
      $ch =~ s#</?html>##ig;
      $ch =~ s#</?head>##ig;
      $ch =~ s#</?body>##ig;
      $ch =~ s#<\!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"##gs;

	#"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
	$ch =~ s#"http://www\.w3\.org/TR/xhtml1/DTD/xhtml1\-transitional\.dtd"&gt;##gi;

	$ch =~ s#"http([^"]+)"(&gt;)*##gis;
	## Add any extra stuff

	$ch =~ s#^\s*&gt;##sg;
	$ch = $chart . $ch;

      push(@parts, $ch);
#      print STDERR "Storing chunk under $fn\n" if $debug;
      $bysource{$fn}= $ch;
  }
  else {
    die "concat.pl: required file not found: $file -- update config $cfg or create file.\n";
  }
}

print $out;





foreach my $chunk (@sources) {
  print "\n<!-- ...................................................... -->\n\n\n";
  print "<strong style=\"color: red;\">[SOURCE: $chunk] </strong> \n" if $debug; # output 'where it came from' for debugging
  print $bysource{$chunk} ."\n\n\n";
} 


### GENERATE Table Of Contents
open (TOC,">_autotoc.html")|| die "Can't write ToC $!";
print TOC "<html><head><title>SWAD-Europe: Table of Contents</title></head>\n\n";

print TOC "<body><h1>Contents</h1>\n\n<ul>";

my $t;
foreach my $i (sort {$a <=> $b} keys %toc) {
    my $info = $toc{$i};
    $info =~ s/(\S+)\s+//;
    my $file = $1;
    my $label = $info;
#  print STDERR "TocGEN: $i  -> $toc{$i}\n";

 if ($i =~ m/9\.30?(.*)/) { $label .= "(todo: add WP $1 name)\n"; }
  print TOC "<li><a href=\"$base$file\">$i. $label</a></li>\n"; # base rel uri ok?
}
print TOC "\n</ul>\n</body>\n</html>\n\n";
close TOC;


# see also...
#<Philippe> http://www.w3.org/DOM/Group/drafts/scripts/
#<danbri> cool, thanks.
#<Philippe> htmlcat
#<Philippe> htmltrim
#<Philippe>  $(HTMLCAT) - $(FILES) | $(HTMLTRIM) div navbar - $@
