#!/usr/bin/env perl
use warnings;

$caption=0;
$head=1;

while(<STDIN>)
{
    if($head && /^ / && !/\*\*\*/)
    {
        s:(\S+):<a href="#$1">$1</a>:g;
    }
    if(!/^~/)
    {
        print "$_";
        next;
    }
    if(/^~\*end\*/)
    {
        print "</tt></pre>";
        if($ARGV[0])
        {
            my $mtime = (stat($ARGV[0]))[9];
            # GMT is unfriendly, but we don't know the timezone of last change
            # vs the timezone of build machine.
            print "Last modified: ".gmtime($mtime)." GMT" if $mtime;
        }
        print "</body></html>\n";
        exit;
    }
    if(/^~~~/)
    {
        print "<hr>\n";
        $head=$caption=0;
        next;
    }
    if(/^~~H (.*)/)
    {
        print "<a name=\"$1\"></a><h5>$1</h5>\n";
        $caption=1;
        next;
    }
    if(/^~([^~].*)/)
    {
        print "<a name=\"$1\"></a>";
        print "<h5>#$1</h5>" unless $caption;
    }
}
