#!/usr/bin/perl -w

#
#  Concatenate Impact Tables
#
#  Willem A. Schreuder
#  February 2, 2004
#

use strict;
use lib '/pm/grflib/perl';
use GetOpt;

#
#  Defaults
#
my %stcol = (
   'Colorado Pumping' => ' bgcolor=cyan',
   'Kansas Pumping'   => ' bgcolor=yellow',
   'Nebraska Pumping' => ' bgcolor=lightgreen',
   'Nebraska Mound'   => ' bgcolor=magenta',
   'CCP Credit'       => ' bgcolor=lightblue',
   );

my %args = GetOpt('t:msd');
my ($header,$leader,@column,@item,%item,%text,%col,@file);
my $mode = '';
$args{m} && ($mode .= 'm');  #  Merge
$args{s} && ($mode .= 's');  #  Sideways
$args{d} && ($mode = 'd');   #  Data file only
my $colfile;

#
#  Snarf the impact tables
#
foreach my $argv (@ARGV)
{
   my ($file,$text,$col) = split(':',$argv);
   $text{$file} = $text ? $text : $file;
   $col{$file} = ($col) ? " bgcolor=$col" : '';
   ($col) && ($colfile++);
   push @file , $file;
   open(HTM , "<$file") || die "Cannot open file $file\n";
   #  Gather stuff on the first pass
   my $init = !$header;
   #  Skip to header
   my $line;
   while ($line = <HTM>)
   {
      ($line =~ /<th/) && last;
   }
   $line || die "Cannot find header line in $file\n";
   chomp $line;
   $line =~ s/<.*?>//g;
   $init && ($header = $line);
   #  Next line is the Column headers
   defined($line = <HTM>) || die "Cannot read column headers\n";
   if ($init)
   {
      chomp $line;
      $line =~ s/<tr .*?>//;
      $line =~ s/<\/tr>//;
      while ($line)
      {
         ($line =~ s/(<th.*?\/th>)//) || die "Missing th: $line\n";
         my $column = $1;
         $column =~ s/<t.*?>//;
         $column =~ s/<\/t.*?>//;
         $mode && ($column =~ s/<br>/ /);
         push @column , $column;
      }
      $leader = shift @column;
   }
   #  Read data
   while (my $item = <HTM>)
   {
      chomp $item;
      ($item =~ /<\/table>/) && last;
      #  Get item
      $item =~ s/<.*?>//;
      if ($init)
      {
         push @item , $item;
      }
      else
      {
         exists($item{$item}) || die "Unknown item $item in $file\n";
      }
      #  Get values
      foreach my $column (@column)
      {
         defined($line = <HTM>) || die "Premature EOF\n";
         chomp $line;
         $line =~ s/ //g;
         my $col = ($mode =~ /m/ && !$col{$file}) ? $stcol{$column} : $col{$file};
         $line =~ s/<td/<td$col/;
         $line =~ s/<th/<th$col/;
         push @{$item{$item}{$file}} , $line;
      }
      #  Read to </tr>
      while (my $line = <HTM>)
      {
         ($line =~ /<\/tr>/) && last;
      }
   }
   close(HTM);
}
#
#
#  Data table
#
if ($mode eq 'd')
{
   for (my $k=0;$k<@column;$k++)
   {
      printf "# $column[$k]\n";
      foreach my $file (@file)
      {
         printf "$text{$file}";
         foreach my $item (@item)
         {
            my $x = $item{$item}{$file}[$k];
            $x =~ s/<.*?>//g;
            printf " %6d" , $x;
         }
         printf "\n";
      }
      print "END\n";
   }
   exit;
}

#
#  Barf the compound table
#
$args{t} && ($header = $args{t});
print  "<html>\n";
print  "<body bgcolor=\"#FFFFFF\">\n";
#  Merged sideways
if ($mode eq 'ms')
{
   for (my $k=0;$k<@column;$k++)
   {
      print "<table border>\n";
      printf "<tr><th align=center colspan=%d><font size=\"+4\">$column[$k]</font></th></tr>\n" , @file+1;
      print  "<tr align=center><th>$leader</th>";
      foreach my $file (@file)
      {
         printf "<th%s>$text{$file}</th>" , (!$col{$file}) ? $stcol{$column[$k]} : $col{$file};
      }
      print "</tr>\n";
      foreach my $item (@item)
      {
         printf "<tr align=right>$item\n";
         foreach my $file (@file)
         {
            print $item{$item}{$file}[$k];
         }
         printf "</tr>\n";
      }
      print "</table><p>\n";
   }
}
#  Merged
elsif ($mode eq 'm')
{
   #  Check year common in headers and in order
   my ($yr0,$yr,%yr) = (0,0);
   foreach my $file (@file)
   {
      ($text{$file} =~ /^(\d\d\d\d)/) || next;
      my $y = $1;
      ($yr0<=$y) || next;
      $yr0 = $y;
      push @{$yr{$y}} , $file;
      $yr++;
   }
   $yr = ($yr==@file) ? 1 : 0;
   #  Remove common years
   my @row;
   if ($yr)
   {
      $row[0] ="<tr align=center>";
      $row[1] ="<tr align=center>";
      foreach my $column (@column)
      {
         foreach my $y (sort keys %yr)
         {
            my @f = @{$yr{$y}};
            my $col = (!$col{$f[0]}) ? $stcol{$column} : $col{$f[0]};
            if (@f==1)
            {
               $row[0] .= "<th rowspan=2$col>$text{$f[0]}</th>";
            }
            else
            {
               $row[0] .= "<th colspan=".scalar(@f)."$col>$y</th>";
               foreach my $file (@f)
               {
                  my $txt = $text{$file};
                  $txt =~ s/^(\d\d\d\d)// || die "WTF $txt\n";
                  $txt =~ s/^<br>// || die "Duplicate year not followed by qualifier $txt\n";
                  $row[1] .= "<th$col>$txt</th>";
               }
            }
         }
      }
      $row[0] .=  "</tr>\n";
      $row[1] .=  "</tr>\n";
   }
   #  Individual columns
   else
   {
      $row[0] ="<tr align=center>";
      foreach my $column (@column)
      {
         foreach my $file (@file)
         {
            $row[0] .= sprintf "<th%s>$text{$file}</th>" , (!$col{$file}) ? $stcol{$column} : $col{$file};
         }
      }
      $row[0] .=  "</tr>\n";
   }
   #  Start table
   print "<table border>\n";
   printf "<tr><th align=center colspan=%d><font size=\"+4\">$header</font></th></tr>\n" , @file*@column+1;
   printf "<tr align=center><th rowspan=%d>$leader</th>" , 2+$yr;
   foreach my $column (@column)
   {
      printf "<th colspan=%d%s>$column</th>" , scalar(@file) , (!$colfile) ? $stcol{$column} : '';
   }
   print "</tr>\n";
   foreach my $row (@row)
   {
      print $row;
   }
   foreach my $item (@item)
   {
      printf "<tr align=right>$item\n";
      for (my $k=0;$k<@column;$k++)
      {
         foreach my $file (@file)
         {
            print $item{$item}{$file}[$k];
         }
      }
      printf "</tr>\n";
   }
   printf "</tr>\n";
   print "</table>\n";
}
elsif ($mode eq 's')
{
   for (my $k=0;$k<@column;$k++)
   {
      print "<table border>\n";
      printf "<tr><th align=center colspan=%d><font size=\"+4\">$column[$k]</font></th></tr>\n" , @item+1;
      print  "<tr align=center><td>Year</td>";
      foreach my $item (@item)
      {
         printf "$item";
      }
      print "</tr>\n";
      foreach my $file (@file)
      {
         printf "<tr align=right><td>$text{$file}</td>\n";
         foreach my $item (@item)
         {
            print $item{$item}{$file}[$k];
         }
         printf "</tr>\n";
      }
      print "</table><p>\n";
   }
}
else
{
   print "<table border>\n";
   printf "<tr><th align=center colspan=%d><font size=\"+4\">$header</font></th></tr>\n" , @file*@column+1;
   print  "<tr align=center><th rowspan=2>$leader</th>";
   foreach my $file (@file)
   {
      printf "<th colspan=%d$col{$file}>$text{$file}</th>" , scalar(@column);
   }
   print "</tr>\n";
   print "<tr align=center>";
   foreach my $file (@file)
   {
      foreach my $column (@column)
      {
         printf "<th$col{$file}>$column</th>";
      }
   }
   print "</tr>\n";
   foreach my $item (@item)
   {
      printf "<tr align=right>$item\n";
      foreach my $file (@file)
      {
         for (my $k=0;$k<@column;$k++)
         {
            print $item{$item}{$file}[$k];
         }
      }
      printf "</tr>\n";
   }
   printf "</tr>\n";
   print "</table>\n";
}
print "</body\n";
print "</html>\n";
