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

exit(0); # --disabled

use IO::File;
use IO::All;
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 security advisory information
my @sa = ();
foreach my $filename (reverse sort glob("../security/advisories/OpenPKG-SA-*.txt")) {
    my ($basename, $shortname) = ($filename =~ m|^.*?(OpenPKG-SA-(\d+\..+))\.txt$|);
    next if ($shortname =~ m|^0000|);
    push(@sa, { -filename => $filename, -basename => $basename, -shortname => $shortname });
}

#  information parsing and content generation
foreach $sa (@sa) {
    my $txt = io($sa->{-filename})->all or die;

    #   determine date
    my $date = "0000-00-00";
    if ($txt =~ m|\nOpenPKG-SA-[^\n]+[ \t]+([^\n]+)\s*\n|s) {
        $date = $1;
    }
    $date = time2str("%Y-%m-%d", str2time($date));

    #   determine URL
    my $link = "http://www.openpkg.org/security/advisories/".$sa->{-basename}.".html";

    #   determine title and description
    my $title = sprintf("%s: Security Advisory %s", $date, $sa->{-basename});
    my $desc = $title;
    if ($txt =~ m|\nVulnerability:[ \t]+([^\n]+?)\s*\n|s) {
        $desc = $1;
    }

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

    #   generate Atom/1.0 entry
    $uuid->make("v3", $uuid_ns, $sa->{-basename});
    $atom_items .=
        "<entry>\n" .
        "  <id>urn:uuid:".$uuid->export("str")."</id>\n" .
        "  <link rel=\"alternate\" href=\"$link\"/>\n" .
        "  <title>$title</title>\n" .
        sprintf("  <updated>%sT00:00:%02dZ</updated>\n", $date, $atom_dates->{$date}++) .
        "  <content>$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/security.rss.xml\">\n" .
    "    <link>http://www.openpkg.org/security/advisories/</link>\n" .
    "    <title>OpenPKG Security Advisories</title>\n" .
    "    <description>List of all published OpenPKG Security Advisory documents</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/security.atom.xml</id>\n" .
    "  <link rel=\"self\" href=\"http://www.openpkg.org/news/security.atom.xml\"/>\n" .
    "  <link rel=\"alternate\" href=\"http://www.openpkg.org/security/advisories/\"/>\n" .
    "  <title>OpenPKG Security Advisories</title>\n" .
    "  <subtitle>List of all published OpenPKG Security Advisory documents</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 ">security.rss.xml" or die;
$io->print($rdf);
$io->close();

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

