#!/v/openpkg/sw/bin/perl

use IO::File;
use IO::All;
use LWP::Simple;
use OSSP::uuid;
use Date::Format;
use Date::Parse;

#   prepare UUID generation
my $uuid_ns_url = new OSSP::uuid;
my $uuid_ns     = new OSSP::uuid;
my $uuid        = new OSSP::uuid;
$uuid_ns_url->load("ns:URL");
$uuid_ns->make("v3", $uuid_ns_url, "http://www.ossp.org/");

#   prepare content generation
my $rss_refs  = '';
my $rss_items = '';
my $atom_items = '';
my $atom_dates = {};

#   read CVS repository timeline
my $html = get("http://cvs.openpkg.org/timeline?d=14");
die "Couldn't get it!" if (not defined($html));

#  information parsing and content generation
my $date = "";
#my $n = 0;
$html =~ s/^.*?Search<\/a>//;
$html =~ s/(<td bgcolor="#d0d9f4" class="bkgnd1">.*?<\/td>|<td valign="top">\s*Check-in\s+.*?<\/td>)/&do1($1), ''/sge;
sub do1 {
    my ($html) = @_;
    #return if ($n++ > 100);

    if ($html =~ m|^<td bgcolor="#d0d9f4" class="bkgnd1">(.*?)<\/td>|s) {
        $date = $1;
        $date =~ s/^.+?,\s*//s;
        my $mon = {
            "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6,
            "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12
        };
        $date =~ s/^(\d+)-(.+)-(\d+)/sprintf("%04d-%02d-%02d", $1, $mon->{$2}, $3)/se;
    }
    elsif ($html =~ m/Check-in\s+<a href="([^""]+)">\[(\d+)\]<\/a>:\s*(.+?)\(By\s+(\S+)\)<\/td>/s) {
        my ($url, $id, $desc, $user) = ($1, $2, $3, $4);
        $url = "http://cvs.openpkg.org/$url";
        $desc =~ s|^\s+||s;
        $desc =~ s|\s+$||s;
        $desc =~ s|</?[ib]>||sg;
        my $title = $desc;
        $title =~ s/^((?:\&(?:lt|gt|quot);|.){60}).*$/$1.../s;
        $desc =~ s|\&|&amp;|sg;

        #   generate RSS/1.0 entry
        $rss_refs .=
            "<rdf:li resource=\"$url\"/>\n"; 
        $rss_items .=
            "<item rdf:about=\"$url\">\n" .
            "  <link>$url</link>\n" .
            "  <title>$date: $user: $title</title>\n" .
            "  <dc:date>$date</dc:date>\n" .
            "  <description>$user: $desc</description>\n" .
            "</item>\n";

        $title =~ s|\&|&amp;|sg;

        #   generate Atom/1.0 entry
        $uuid->make("v3", $uuid_ns, $url);
        $atom_items .=
            "<entry>\n" .
            "  <id>urn:uuid:".$uuid->export("str")."</id>\n" .
            "  <link rel=\"alternate\" href=\"$url\"/>\n" .
            "  <title type=\"html\">$date: $user: $title</title>\n" .
            sprintf("  <updated>%sT00:00:%02dZ</updated>\n", $date, $atom_dates->{$date}++) .
            "  <content type=\"html\">$user: $desc</content>\n" .
            "</entry>\n";
    }
}

#   generate RSS/1.0 output
$rss_refs =~ s/^/      /mg;
$rss_items =~ s/^/  /mg;
my $rdf =
    "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" .
    "<!-- OpenPKG Content Syndication News Feed -->\n" .
    "<!-- Syntax: RSS/1.0 RDF/1999-02-22 XML/1.0 -->\n" .
    "<rdf:RDF\n" .
    "  xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" .
    "  xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" .
    "  xmlns=\"http://purl.org/rss/1.0/\">\n" .
    "  <channel rdf:about=\"http://www.openpkg.org/news/cvs.rss.xml\">\n" .
    "    <link>http://cvs.openpkg.org/timeline/</link>\n" .
    "    <title>OpenPKG CVS Repository</title>\n" .
    "    <description>List of all OpenPKG CVS Repository transactions from the last 14 days</description>\n" .
    "    <image rdf:resource=\"http://www.openpkg.org/favicon.ico\"/>\n" .
    "    <items>\n" .
    "      <rdf:Seq>\n" .
    $rss_refs .
    "      </rdf:Seq>\n" .
    "    </items>\n" .
    "  </channel>\n" .
    $rss_items .
    "</rdf:RDF>\n";

#   generate Atom/1.0 output
$atom_items =~ s/^/  /mg;
$date = time2str("%Y-%m-%dT%H:%M:%SZ", time());
my $atom =
    "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" .
    "<!-- OpenPKG Content Syndication News Feed -->\n" .
    "<!-- Syntax: Atom/1.0 XML/1.0 -->\n" .
    "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n" .
    "  <id>http://www.openpkg.org/news/cvs.atom.xml</id>\n" .
    "  <link rel=\"self\" href=\"http://www.openpkg.org/news/cvs.atom.xml\"/>\n" .
    "  <link rel=\"alternate\" href=\"http://cvs.openpkg.org/timeline/\"/>\n" .
    "  <title>OpenPKG CVS Repository</title>\n" .
    "  <subtitle>List of all OpenPKG CVS Repository transactions from the last 14 days</subtitle>\n" .
    "  <icon>http://www.openpkg.org/favicon.ico</icon>\n" .
    "  <updated>$date</updated>\n" .
    "  <author>\n" .
    "    <name>OpenPKG Project</name>\n" .
    "    <email>openpkg\@openpkg.org</email>\n" .
    "    <uri>http://www.openpkg.org</uri>\n" .
    "  </author>\n" .
    $atom_items .
    "</feed>\n";

#   store RSS/1.0 output
my $io = new IO::File ">cvs.rss.xml" or die;
$io->print($rdf);
$io->close();

#   store Atom/1.0 output
my $io = new IO::File ">cvs.atom.xml" or die;
$io->print($atom);
$io->close();

