#!/usr/bin/perl

my $i;
my %info;
my $myName = $0;
my $metafile = undef;
while (<>) {
    $metafile = $ARGV unless defined $metafile;
    if (/{{{/.../}}}/) {
#		print ">>>$_";
		if (/<<</.../>>>/) {
			s/<<<//;
			if (/^Version ([\d\.]+)[\s-]+(.+)$/) {
#               print "The Version: '$1'. The date: '$2'\n";
               $info{Version}=$1;
               $info{Date}=$2;
               $i=undef;
            }
            if (/^File: (.*)$/) {
               $i++ if defined($i);
               $i=0 unless defined($i);
               $info{File}[$i]{Name}=$1;
               $s=undef;
#               print "Got File $i '$info{File}[$i]{Name}'\n";
            }
            if (/^Site: (.*)$/) {
               $s++ if defined($s);
               $s=0 unless defined($s);
               $info{File}[$i]{Site}[$s]=$1;
#               print "Got Site $i $s '$info{File}[$i]{Site}[$s]'\n";
            }
            if (/^Requires:/.../^---$/) {
               s/Requires:|---//;
               $info{File}[$i]{Requires} .= $_;
#               print "Got Requires $i '$info{File}[$i]{Requires}'\n";
		    }
            if (/^Description:/.../^---$/) {
               s/Description:|---//;
               $info{File}[$i]{Description} .= $_;
#               print "Got Description $i '$info{File}[$i]{Description}'\n";
            }
            if (/^Header:/.../^---$/) {
               s/Header:|---//;
               $info{Header} .= $_;
            }
            if (/^Footer:/.../^---$/) {
               s/Footer:|---//;
               $info{Footer} .= $_;
            }
		}
        if (/^>>>$/) {
           my ($anchor,$anchorEnd) = ("","");
           ($anchor,$anchorEnd) = ("<a name=\"latest\">","</a>") if !$first++;
           print "<H2>$anchor";
           print "<img SRC=\"quake.gif\" ALT=\"[little Quake guy]\"";
           print "height=32 width=32> MacGLQuake Version $info{Version}";
           print " - $info{Date}\n";
           print "$anchorEnd</H2>\n";

           print "$info{Header}\n";
           my ($heading,$data,$text) = ("#1188ff","#7777cc","#ffffff\" LINK=\"#000000");
           print "<table BORDER=\"0\" CELLSPACING=\"5\" WIDTH=\"100%\">\n";
           print "<tr bgcolor=\"$heading\">\n";
           foreach my $title ("File","Download<br>Sites",
                              "Requires","Description") {
               print " <th><font size=+1 COLOR=\"$text\">$title</font></th>\n";
           }

           print "</tr>\n";

           my $descriptionIndex = undef;
           for (my $x=0;$x <= $#{@info{File}};$x++) {
               print "<tr bgcolor=\"$data\">\n";
               print " <td>".font("color=\"$text\"",
                                  center($info{File}[$x]{Name}));
               print "</td>\n";
               print " <td> ";
               print font("color=\"$text\"",
                          center(join("\n      <br>or<br>\n      ",
                                      @{$info{File}[$x]{Site}})));
               print "\n </td>\n";
               print " <td>".font("color=\"$text\"",
                                  center($info{File}[$x]{Requires}));
               print "\n </td>\n";
               if (!defined($descriptionIndex) || $descriptionIndex < $x) {
                   for (my $y=$x;$y <= $#{@info{File}};$y++) {
                      if (defined($info{File}[$y]{Description})) {
                          $rowspan = $y-$x+1;
                          $descriptionIndex = $y;
                          last;
                      }
                   }
                   print " <td rowspan=\"$rowspan\">\n";
                   print font("color=\"$text\"",
                              $info{File}[$descriptionIndex]{Description});
                   print "\n </td>\n";
               }
               print "</tr>\n";
           }
           print "</table>\n";
           print "$info{footer}\n\n";

           foreach my $k (keys %info) {
#                printf STDERR "$k\n";
                delete $info{$k};
           }
#           printf STDERR "---\n";
        }
	} else {
		print;
	}
}

print font("size=-1","Converted from <a href=\"$metafile\">$metafile</a> by ".
           "<a href=\"$myName\">$myName</a> on ".
           `date +"%A %B %e, %Y at %r"`.
           ".");

sub center {
  return "<center>$_[0]</center>";
}

sub font {
  return "<font $_[0]>$_[1]</font>";
}

