open INFILE,"download-in.html";
open OUTFILE,">download.html";

while (<INFILE>)
{
	s/[\r\n]*$//;

	if ($flag==1 && m/<\/pre>/)
	{
		print OUTFILE "</table>\n";
		$flag = 2;
	}

	if ($flag==1)
	{
		while (s/\\ *$/ /)
		{
			$in = <INFILE>;
			$in =~ s/[\r\n]*$//;
			$_ = $_.$in;
		}
		s/ +/ /g;
		s/^ +$//;
		if ($_)
		{
			if (m/^(\w+): (.*)/)
			{
				$key = $1;
				$val = $2;
			}else{
				die "failed to parse [key: val] $_\n";
			}
	#		print OUTFILE "[$key][$val]\n";
			if ($key eq "GROUP")
			{
				print OUTFILE "  <tr>\n";
				print OUTFILE "    <td colspan=\"4\">\n";
				print OUTFILE "       <hr size=\"1\">\n";
				print OUTFILE "    </td>\n";
				print OUTFILE "  </tr>\n";

				print OUTFILE "  <tr>\n";
				print OUTFILE "    <td colspan=\"4\">\n";
				print OUTFILE "       <h3 class=\"dlgroup\">$val</h3>\n";
				print OUTFILE "    </td>\n";
				print OUTFILE "  </tr>\n";

				print OUTFILE "  <tr>\n";
				print OUTFILE "    <td colspan=\"4\">\n";
				print OUTFILE "       <hr size=\"1\">\n";
				print OUTFILE "    </td>\n";
				print OUTFILE "  </tr>\n";
			}
			if ($key eq "SUBGROUP")
			{
				print OUTFILE "  <tr>\n";
				print OUTFILE "    <td colspan=\"4\">\n";
				print OUTFILE "       <h4>$val</h4>\n";
				print OUTFILE "    </td>\n";
				print OUTFILE "  </tr>\n";
			}
			if ($key eq "INFO")
			{
				print OUTFILE "  <tr>\n";
				print OUTFILE "    <td colspan=\"4\">\n";
				print OUTFILE "      <p class=\"dlinfo\">\n";
				print OUTFILE "        $val\n";
				print OUTFILE "      </p>\n";
				print OUTFILE "    </td>\n";
				print OUTFILE "  </tr>\n";
			}
			if ($key eq "FILENAME")
			{
				$filename = $val;
			}
			if ($key eq "PLATFORM")
			{
				$platform = $val;
			}
			if ($key eq "LICENSE")
			{
				$license = $val;
			}
			if ($key eq "FILEHREF")
			{
				$filesize = `du -sh $val 2>&1`;
				if ($filesize =~ m/No such/g) { 
					$filesize = "<B>Error: Missing</B>"; 
					print "ERROR: Missing $val\n";
				}
				else {
					($filesize) = split "\t",$filesize;
				}
				print OUTFILE "  <tr>\n";
				print OUTFILE "    <td nowrap>$filename</td>\n";
				print OUTFILE "    <td nowrap>$platform</td>\n";
				print OUTFILE "    <td nowrap>$license</td>\n";
				print OUTFILE "    <td nowrap><a href=\"$val\">Download</A> ($filesize)</td>\n";
				print OUTFILE "  </tr>\n";
				$filename = "-";
				$platform = "-";
				$license = "-";
			}
		}
	}
	if (m/<pre PLACEHOLDER>/)
	{
		$flag = 1;
		print OUTFILE "<table summary=\"downloads\">\n";
	}		

	if ($flag==0)
	{
		print OUTFILE "$_\n";
	}
	if ($flag==2) { $flag = 0; }
}

close INFILE;
close OUTFILE;
