#!/usr/local/bin/perl
use warnings;
use strict;
use lib qw( /sw/lib/perl5 /sw/lib/perl5/darwin lib );
use CGI;
use Template;
use Pod::POM 'meta';
use View;

# Just. one. more. tweak.  *crash*

my $cgi = CGI->new;
my ($pod, $head2) = $cgi->path_info =~ m{^/([a-z.-]+)(?:/(.*))?$};
$pod ||= 'os.pod';

my $pom = Pod::POM->new->parse($pod);
$pom->meta( 'consider_old', 10 ) unless $pom->meta( 'consider_old' );
my $view = View->new;

my $makelink = sub {
    my $item = shift;
    my $link = $item->title->present($view);
    $link =~ tr{ ,?'".:}
               {_______};
    return $link;
};

# hacky
$View::cgi = $cgi;
$View::pom = $pom;
$View::pod = $pod;
$View::makelink = $makelink;

my %args = ( pomview  => $view,
             pom      => $pom,
             cgi      => $cgi,
             makelink => $makelink,
             blog     => $pod,
             head2    => $head2,
             warn     => sub { warn @_ },
             self     => $cgi->url . "/$pod",
            );
$| = 1;

if ($cgi->param('rss')) {
    print $cgi->header('text/xml');
    my $tt = Template->new;
    $tt->process('rss.tt2', \%args)
      or die $tt->error;
    exit 0;
}

print $cgi->header;

my $tt = Template->new( PRE_CHOMP => 1, POST_CHOMP => 1 );
$tt->process('main.tt2', \%args)
  or die $tt->error;

