#!/usr/bin/perl -w

use strict;

my $archive = "/home/www/html/ccp/archive.txt";

print "Content-type: text/html\n\n";

print "<title>The CVS Continuity Project Mail Archive</title>\n\n";
print "<html>\n";
print "<body bgcolor=ffffff fgcolor=000000>\n";
print "\n";
print "<p>\n";
print "\n";

print "<center>\n";
print "<h1><a href=\"index.html\">CCP</a> Mailing List Archive </h1>\n";
print "</center>\n";

print "\n<p>\n<hr>\n<p>\n";

print "<ol>\n";

open (ARCHIVE, "<$archive") or die ("unable to open $archive ($!)\n");

my $date = "";
my $sender = "";
my $subject = "";
my $count = 0;
my $lines = -1;
my $bytes = -1;
while (<ARCHIVE>)
{
  chop;

  if (/^From [^\s]+ (.*)/) {
    # Reset everything
    $date = "";
    $sender = "";
    $subject = "";
    $lines = -1;
    $bytes = -1;

    $date = $1;
  }

  if (/^From: (.*)/) {
    $sender = $1;
  }

  if (/^Subject: (.*)/) {
    $subject = $1;
  }
      
  if ($date and ($lines < 0) and ($bytes < 0) and ($_ == ""))
  {
    # End of headers, so start counting size of body.
    print "<br><font color=red>here</font>\n";
    $lines = 0;
    $bytes = 0;
  }
  elsif (($lines >= 0) and ($bytes >= 0))
  {
    $lines += 1;
    $bytes += (1 + length);   # compensate for the chopped newline
  }

  if ($date and $sender and $subject)
  {
    $count++;
    print "<li>Subject: <a href=\"find-msg.cgi?num-to-find=${count}\">"
        . "${subject}</a>"
        . "\n<br><tt>${lines} lines, ${bytes} bytes</tt>"
        . "\n<br>\n($sender, $date)</li>\n<p>\n";
  }
}

close (ARCHIVE);

print "\n";
print "</ol>\n";
print "\n";
print "</body>\n";
print "</html>\n";
