#!/usr/bin/perl -w

# Blosxom
# Author: Rael Dornfest <rael@oreilly.com>
# Modified by: Zack Weinberg <zackw@panix.com>
# Version: 3+4i
# Home/Docs/Licensing: http://www.oreillynet.com/~rael/lang/perl/blosxom/

# --- Configurable variables -----

# Desired time zone (look in /usr/share/zoneinfo for your choices).
$ENV{TZ} = 'America/New_York';

# What's my blog's title?
my $blog_title = 'Ex Bibliotheca';

# Where are my blog entries kept?
my $datadir = "exbib";

# How many entries should I show on the home page?
my $num_entries = 20;

# --------------------------------

use strict;
use CGI;

undef $/; # slurp mode

# Days of the week
my @dayname = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
my @monname = qw(January February March April May June July
		 August September October November December);

# Take a gander at HTTP's PATH_INFO for optional archive yr/mo/day
my $url = CGI::url();  # may be used by story/head/foot templates
my (undef, $pi_yr, $pi_mo, $pi_da) = split(/\//, CGI::path_info());

$pi_mo = ucfirst(lc($pi_mo)) if defined($pi_mo);

chdir($datadir) or die "entering $datadir: $!\n";

# Templates
open(F, "< head.html") or die "opening $datadir/head.html: $!\n";
my $head = <F>;
close F;
$head =~ s/(\$\w+)/$1/gee;

open(F, "< foot.html") or die "opening $datadir/foot.html: $!\n";
my $foot = <F>;
close F;
$foot =~ s/(\$\w+)/$1/gee;

open(F, "< story.html") or die "opening $datadir/story.html: $!\n";
my $story = <F>;
close F;
# $story doesn't get substituted until later.

# Scan the directory and determine all the files' modtimes.

my %mtimes = ();

opendir(D, ".") or die "opendir($datadir): $!\n";

while (defined($_ = readdir(D))) {
    /\.txt$/ or next;
    $mtimes{$_} = (stat($_))[9];
}
closedir(D);

# Header
print CGI::header('text/html');
print $head;

# Send in the blogs
my $curdate = '';
my $fn;
foreach $fn (sort { $mtimes{$b} <=> $mtimes{$a} } keys %mtimes) {

	last if $num_entries-- <= 0 && !$pi_yr;

	# Date fiddling for by-{year,month,day} archive views
	my @mtime = localtime($mtimes{$fn});
	my($dw,$yr,$mo,$da,$hr,$mn) = (
		$dayname[$mtime[6]],
		$mtime[5] + 1900,
		$monname[$mtime[4]],
		$mtime[3],
		$mtime[2],
		($mtime[1] - $mtime[1] % 5)
	);

	next if $pi_yr && $yr != $pi_yr; last if $pi_yr && $yr < $pi_yr; 
	next if $pi_mo && $mo ne $pi_mo;
	next if $pi_da && $da != $pi_da; last if $pi_da && $da < $pi_da; 

	my($am, $twhr, $abmn);

	$am = ($hr >= 12) ? "PM" : "AM";
	$twhr = $hr;
	$twhr -= 12 if $twhr >= 12;
	$twhr = 12 if $twhr == 0;

	$abmn = ($mn == 0) ? "" : sprintf(":%.2d", $mn);

	$mn = sprintf "%.2d", $mn;
	$hr = sprintf "%.2d", $hr;

	print CGI::h2($curdate = "$dw, $da $mo $yr")
	    if $curdate ne "$dw, $da $mo $yr";

	# Entry
	if (open(F, $fn)) {
		my $body = <F>;
		close F;

		# Timezone marker (optional)
		my $tzne = "";
		if ($body =~ s/^<!--\s*(GMT(?:[+-]\d+)?)\s*-->//) {
		    $tzne = " ($1)";
		}

		my $thisstory = $story;
		$thisstory =~ s/(\$\w+)/$1/gee;
		print $thisstory;
	} else {
	    	print "<p><b>cannot open $fn: $!\n";
	}
}

# Foot
print $foot;