#!/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 = '';

#   read news page
my $html = get("http://meta.openpkg.org/global-news.php");
die "Couldn't get it!" if (not defined($html));

#  information parsing and content generation
my $date = "";
$html =~ s/<div\s+class="active"\s+id="(.+?)">(.+?)<\/div>/&do1($1, $2), ''/sge;
sub do1 {
    my ($id, $html) = @_;

    my $url = "http://www.openpkg.org/news/#$id";
    my $date = "";
    my $title = "";
    my $msg = "";

    $html =~ s/<h2>(.+?)<\/h2>/$title = $1, ''/se;
    $html =~ s/<span\s+class="date">(.+?)<\/span>/$date = $1, ''/se;
    $html =~ s/<span\s+class="more">.+?<\/span>//s;
    $html =~ s/\s+/ /sg;
    $html =~ s/^\s+//sg;
    $html =~ s/\+$//sg;
    $html =~ s/\s+class="[^"]+"//sg;
    $html =~ s/<a\s+.+?>.+?<\/a>//sg;
    $html =~ s/<p\/>/ /sg;
    $msg = $html;

    #   generate RSS/1.0 entry
    $rss_refs .=
        "<rdf:li resource=\"$url\"/>\n"; 
    $rss_items .=
        "<item rdf:about=\"$url\">\n" .
        "  <link>$url</link>\n" .
        "  <title>$title</title>\n" .
        "  <dc:date>$date</dc:date>\n" .
        "  <description>$msg</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\">$title</title>\n" .
        sprintf("  <updated>%sT00:00:%02dZ</updated>\n", $date, $atom_dates->{$date}++) .
        "  <content type=\"html\">$msg</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/news.rss.xml\">\n" .
    "    <link>http://www.openpkg.org/news/</link>\n" .
    "    <title>OpenPKG Latest News</title>\n" .
    "    <description>Latest OpenPKG News Messages</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/news.atom.xml</id>\n" .
    "  <link rel=\"self\" href=\"http://www.openpkg.org/news/news.atom.xml\"/>\n" .
    "  <link rel=\"alternate\" href=\"http://www.openpkg.org/news/\"/>\n" .
    "  <title>OpenPKG Latest News</title>\n" .
    "  <subtitle>Latest OpenPKG News Messages</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 ">news.rss.xml" or die;
$io->print($rdf);
$io->close();

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

