#!/usr/bin/perl -wT

use lib ".";
use Kerryandjane;
use strict;

my %sections = ();

print "Content-type: text/xml\n\n";

print <<'EOF';
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns="http://purl.org/rss/1.0/">
    <channel rdf:about="http://www.kerryandjane.com/cgi-bin/news.xml">
	<title>Kerry and Jane's News</title>
	<description>
	    Tedious and rarely-updated news from Kerry and Jane.
	</description>
	<link>http://www.kerryandjane.com</link>
	<items>
	    <rdf:Seq>
EOF

# first get a list of valid sections
open (LIST, "<", "../data/sections.dat") or die ("Failed to open section list: $!");
while (<LIST>) {
    next if /^\s*#/;
    if (/^(.*?),(.*)$/) {
	$sections {$1} = $2;
    }
}
close LIST;

my %files = ();
my $date = "";

foreach my $section (sort keys %sections) {
    while (<../data/$section/news/*xml>) {
	if (-r) {
	    my $filename = $_;
	    $date = get_date ($_);
	    if ($date) {
		if ($files {"$date"}) {
		    my $temp_date = $date;
		    for (my $count = 1; $files {"$temp_date"}; $count++) {
			$temp_date = sprintf ("%s-%02d", $date, $count);
		    }
		    $date = $temp_date;
		}
		$files {"$date"} = $filename;
	    }
	}
    }
}

my $last_file = (keys (%files) < 5 ? keys (%files) - 1 : 4);

foreach ((reverse sort keys %files) [0..$last_file]) {
    parse ($files {$_}, "ref");
}

print << "EOF";
	    </rdf:Seq>
	</items>
    </channel>
    <image rdf:about=\"$Kerryandjane::img_loc/kerryandjane88x31.gif\">
	<title>Kerry and Jane's Page</title>
	<url>$Kerryandjane::img_loc/kerryandjane88x31.gif</url>
	<link>http://www.kerryandjane.com</link>
    </image>
EOF

foreach ((reverse sort keys %files) [0..$last_file]) {
    parse ($files {$_}, "item");
}

print "</rdf:RDF>\n";

sub get_date {
    my ($filename) = @_;
    my $year = 0;
    my $month = 0;
    my $date = 0;
    if (-r $filename) {
	local $/;
	open (ARTICLE, "<", "$filename") or return "00000000";
	my $contents = <ARTICLE>;
	close ARTICLE;
	if ($contents =~ m|<DATE.*YEAR="(\d+)"|is) {
	    $year = sprintf ("%04d", $1);
	}
	if ($contents =~ m|<DATE.*MONTH="(\d+)"|is) {
	    $month = sprintf ("%02d", $1);
	}
	if ($contents =~ m|<DATE.*DATE="(\d+)"|is) {
	    $date = sprintf ("%02d", $1);
	}
    }
    return "$year$month$date";
}

sub parse {
    my ($filename) = $_[0];
    my ($mode) = $_[1];

    $filename =~ m|.*/(.*?)/news/(.*?)$|;
    my $section = $1;
    my $file = $2;
    my $title = "[No Title]";
    my $year = 0;
    my $month = 0;
    my $date = 0;
    my $body = "";
    if (-r $filename) {
	local $/;
	open (ARTICLE, "<", "$filename") or die ("Failed to open $filename: $!");
	my $contents = <ARTICLE>;
	close ARTICLE;
	if ($contents =~ m|<TITLE\b.*?VALUE="(.+?)".*?>|is) {
	    $title = $1;
	}
	if ($contents =~ m|<DATE\b.*YEAR="(\d+)"|is) {
	    $year = $1;
	}
	if ($contents =~ m|<DATE\b.*MONTH="(\d+)"|is) {
	    $month = $1
	}
	if ($contents =~ m|<DATE\b.*DATE="(\d+)"|is) {
	    $date = $1;
	}

	if ($mode eq "ref") {
	    print "            <rdf:li rdf:resource=\"http://www.kerryandjane.com/index.shtml\?section=$section\&amp;filename=$file\"/>\n";
	} else {
	    $contents =~ m|<body>(.*?)</body>|is;
	    $body = $1;
	    # only show the first paragraph
	    $body =~ s/<p>.*//is;
	    # remove cross-reference tags
	    $body =~ s|<xref text="(.?)".?/>|$1|is;
	    print << "EOF";
    <item rdf:about="http://www.kerryandjane.com/index.shtml\?section=$section\&amp;filename=$file\">
	<title>$title ($date/$month/$year)</title>
	<link>http://www.kerryandjane.com/index.shtml\?section=$section\&amp;filename=$file</link>
	<description>$body</description>
    </item>
EOF
	}
    }
}

