#!/usr/bin/perl

use strict;
use warnings;
use Time::Local;

# convert to rss (rfc822) dateformat
sub rssdate {
  my @d=split '-',shift;
  my $z;
  my @ww=("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
  my @mth=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct",
           "Nov","Dec");
  my ($mday,$mon,$year,$wday);

  ($z,$z,$z,$mday,$mon,$year,$wday,$z,$z)=localtime(
      timelocal(0,0,0,$d[2],$d[1]-1,$d[0]));
  return sprintf "%s, %02d %s %d 00:00:00 GMT",$ww[$wday],$mday,
      $mth[$mon],$year+1900;
}

# output rss
sub outrss {
  my $fname=shift;
  my $data=shift;
  my $sec=shift;
  my $id;

  open OUT,">$fname";
  print OUT "<rss version=\"2.0\">\n".
            "<channel>\n".
            "  <title></title>\n".
            "  <link>link</link>\n".
            "  <description></description>\n".
            "  <item>\n";
  foreach $id (sort {$$data{$b}{date} cmp $$data{$a}{date}} keys %$data) {
    if (($$data{$id}{sec}|$sec)==1) {
      print OUT "    <guid>$id</guid>\n".
                "    <pubDate>".(rssdate($$data{$id}{date}))."</pubDate>\n".
                "    <description>$$data{$id}{desc}</description>\n";
    }
  }
  print OUT "  </item>\n".
            "</channel>\n".
            "</rss>\n";
  close OUT;
}

# output html
sub outhtml {
  my $data=shift;
  my $id;

  foreach $id (keys %$data) {
    open OUT,">$id.html";
    print OUT "<html>\n".
              "<head>\n".
              "<title></title>\n".
              "</head>\n".
              "<body>\n";
    print OUT "<table>\n".
              "<tr><td>Name</td><td>$$data{$id}{name}</td></tr>\n".
              "<tr><td>Version</td><td>$$data{$id}{ver}</td></tr>\n".
              "<tr><td>Release</td><td>$$data{$id}{rel}</td></tr>\n".
              "<tr><td>Summary</td><td>$$data{$id}{sum}</td></tr>\n".
              "<tr><td>Description</td><td>$$data{$id}{desc}</td></tr>\n".
              "</table><hr>\n";
    print OUT "<h4>Update Information</h4>\n<p>$$data{$id}{upd}</p><hr>\n"
        if (defined($$data{$id}{upd}));
    print OUT "<h4>Changelog</h4>\n$$data{$id}{chg}<hr>\n"
        if (defined($$data{$id}{chg}));
    print OUT "</body>\n".
              "</html>\n";
    close OUT;
  }
}

# process a html page for fedora update annoucement
# return 0 for no error, 1 not-an-annoucement, 2 error
my @errstr=("OK","Not an annoucement","Error!");
sub process {
  my $url=shift;
  my $upd=shift;
  my ($html,%upd,$id,$sec);

  $html="";
  open IN,"curl -s \"$url\"|";
  while (<IN>) {
    $html.=$_;
  }
  close IN;

  # remove DOS-styled CRLF
  $html=~s/\cm+//g;

  #
  # do processing
  ####
  my $de="-"x69;
  return 1 if ($html!~/Fedora\s(?:Security\s)?Update\sNotification/);

  if ($html=~/<em>subject<\/em>: (.+?)<\/li>/is) {
    $sec=($1=~/\[SECURITY\]/)?1:0;
  } else {
    print "ERR: parsing HTML: cannot find subject\n";
    return 2;
  }

  $html=$1 if ($html=~/<pre>(.+)<\/pre>/is);

  if ($html!~/----\n
              Fedora\s(?:Security\s)?Update\sNotification\n
             (FEDORA-\d+-\d+)\n
             (\d{4}-\d{2}-\d{2})\n
              ----/x
  ) {
    print "ERR: parsing HTML: cannot find id\n";
    return 2;
  }
  $html=$'; $id=$1; $$upd{$id}{date}=$2; $$upd{$id}{sec}=$sec;

  if ($html!~/\nName\s+:\s+(.+?)\n
                Version\s+:\s+(.+?)\n
                Release\s+:\s+(.+?)\n
                Summary\s+:\s+(.+?)\n
                Description\s:\n/x) {
    print "ERR: parsing HTML: cannot find name\n";
    return 2;
  }
  $html=$';
  $$upd{$id}{name}=$1; $$upd{$id}{ver}=$2; $$upd{$id}{rel}=$3; $$upd{$id}{sum}=$4;
  $$upd{$id}{name}=~s/[ \t]*$//mg;
  $$upd{$id}{ver}=~s/[ \t]*$//mg;
  $$upd{$id}{rel}=~s/[ \t]*$//mg;
  $$upd{$id}{sum}=~s/[ \t]*$//mg;

  if ($html!~/(.+?)\n\n$de/s) {
    print "ERR: parsing HTML: cannot find description\n";
    return 2;
  }
  $html=$'; $$upd{$id}{desc}=$1;
  $$upd{$id}{desc}=~s/([^-@])\n/$1 /g;
  $$upd{$id}{desc}=~s/^[ \t\n]*//g;
  $$upd{$id}{desc}=~s/[ \t\n]*$//g;


  if ($html!~/(?:Update Information:)?
              (?:\s+(.+?)$de)?
              (?:\s+(\*\s(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat).+?)$de)?
                 \s+This\supdate\scan\sbe\sdownloaded\sfrom/sx) {
    print "ERR: parsing HTML: cannot find update\n";
    return 2;
  }
  $html=$';
  my $tmp1=$1; my $tmp2=$2;
  if (defined($tmp1)) {
    if ($tmp1=~/^\*\s(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)/) {
      $$upd{$id}{chg}=$tmp1;
    } else {
      $$upd{$id}{upd}=$tmp1;
      $$upd{$id}{chg}=$tmp2 if (defined($tmp2));
    }
  }
  if (defined($$upd{$id}{chg})) {
    $$upd{$id}{chg}=~s/^[ \t\n]*//g;
    $$upd{$id}{chg}=~s/[ \t\n]*$//g;
  }
  if (defined($$upd{$id}{upd})) {
    $$upd{$id}{upd}=~s/([^-@])\n/$1 /g;
    $$upd{$id}{upd}=~s/^[ \t\n]*//g;
    $$upd{$id}{upd}=~s/[ \t\n]*$//g;
  }

  return 0;
}

my ($rc,%upd);
my $html="";
my $urlbase="http://www.redhat.com/archives/fedora-announce-list/2004-January";
open IN,"curl -s $urlbase/thread.html|";
while (<IN>) {
  chomp;
  $html.=$_;
}
close IN;
 
while ($html=~/<a[^>]+?href=(['"]?)(msg\d+\.html)\1[^>]*>/) {
  $html=$';
  $rc=process("$urlbase/$2",\%upd);
  print "$2 ProcessStatus $errstr[$rc]\n";
}

#
# do output
####
outrss("rss.xml",\%upd,1);
outrss("rss_sec.xml",\%upd,0);
outhtml(\%upd);

