#!/usr/bin/perl

$Match=$ARGV[0].".html";
$Archive="archives";
$MsgBoard="wwwboard.html";
$TmpBoard="tempboard.html";
$ArcBoard="Arcboard.html";
$Mesg="messages";
$ext="html";


$msg1= "<center>[ <a href=\"#followups\">Follow Ups</a> ] ";
$msg2= "[ <a href=\"http://hbd.org/brewery/wwwboard/wwwboard.html\">";
$msg3= "Brews And Views: The Brewery's Discussion Board</a> ] ";
$msg4= "[ <a href=\"/index.html\">The Brewery</a> ] ";
$msg5= "[ <a href=\"http://hbd.org/brewery/wwwboard/faq.html\">FAQ</a> ]";
$msg6= "</center>";
$ArcMsg=$msg1.$msg2.$msg3.$msg4.$msg5.$msg6;


$Arc1= "<html><head><title>Brews and Views: The Brewery's Discussion Board Archives</title>";
$Arc2= "</head><BODY Bgcolor=\"#FFFFFF\"><IMG SRC=\"/brewery/img/bn_bull.gif\" ALT=\" \">";
$Arc3= "<center><h1>Brews and Views</h1><h2>The Brewery's Discussion Board</h2> </center>";
$Arc4= "<p><hr size=7 width=75%> <center>";
$Arc5= " [ <a href=\"http://brewery.org/brewery/\">The Brewery</a> ]";
$Arc6= " [ <a href=\"faq.html\">FAQ</a> ] ";
$Arc7= " [ <a href=\"brewery/wwwboard/archives/index.html\">Archive Index</a> ]";
$Arc8= " </center> <hr size=7 width=75%> <ul> <!--begin-->";
$ArcMsg=$Arc1.$Arc2.$Arc3.$Arc4.$Arc5.$Arc6.$Arc7.$Arc8;


$tail= "<!--#include virtual=\"/brewery/wwwboard/Footer.html\"--></body></html>";

# process out the messages into the archive directory

$ArchiveDir=$Archive."/".$Mesg;
&ReadNum;

foreach (@Data) {
  $File=$_.".".$ext;
  open(IF,$Mesg."/".$File) || die;
  flock(IF,2);
  $OutFile=$Archive."/".$Mesg."/".$File;
  open(OF,">".$Archive."/".$Mesg."/".$File) || die;
  while (<IF>) {
   s/href="/href="\/brewery\/wwwboard\/archives\/messages\//;
    if (/#followups/) {
      $_=<IF>;
      print OF "$ArcMesg\n";
      next;
    }
    if (/<a name="postfp">Post a Followup<\/a><p>/) {
      while (! /<p><hr size=7 width=75%>/) {
        $_=<IF>;
      }
      print OF "$tail\n";
      close(IF);
      $_='';
    }
    print OF "$_";

  }
  flock(IF,8);
  close(IF);
  close(OF);
  $ByeBye=$Mesg."/".$File;
  print "$ByeBye\n";
  $stat=`rm  $ByeBye`;
}

exit(0);

#Next, build the "index" html file for this group of messages
# Build a name based off of the date in the first record of $ArcBoard
open(IF,$ArcBoard) || die;
$_=<IF>;
close(IF);
($left,$right)=split(/<i>/,$_,2);
($left,$date)=split(/ /,$right,2);
$date=~s/<\/i>//g;
$date=~s/\//-/g;
chop($date);
$NewArch=$Archive."/".$date.".".$ext;
open(IF,$ArcBoard);
open(OF2,">".$NewArch);
print OF2 "$ArcMsg\n";
while (<IF>) {
  print OF2 "$_";
}
print OF2 "$tail\n";
close(IF);
close(OF2);

open(OF,">>".$Archive."/list.html");
print OF "<li><a href=\"/brewery/wwwboard/$NewArch\">$date</a><br>\n";
close(OF);



chdir($Archives);
$stat=`mkindx`;



sub ReadNum {

  open(IF,$ArcBoard) || die;
  $idx=0;
  while (<IF>){
    if (/--top: /) {
        ($a,$b)=split(/top: /,$_,2);
        ($num,$jnk)=split(/--/,$b,2);
        $Data[$idx++]=$num;
    } 
  }
  close(IF);
}
