#!/usr/bin/perl

# sitebuild-update-rss-blue
# by Gav Ford
# revford@blueyonder.co.uk
# http://revford.pwp.blueyonder.co.uk
# 2008-01, updated 2009-02-01
# read the update note files and build an RSS file from them.
# updated to be build the feed for the old Blueyonder hosts



use Term::ANSIColor qw(:constants);
use FindBin '$RealBin';


require "$RealBin/lib-rf-date.pl";
require "$RealBin/lib-rf-sitebuild.pl";


$outfile    = "/home/gav/web/blue-site/revford-rss.xml";


@sortedlist = sortpostlist();


# assemble the page
open OUTFILE, "> $outfile" or die RED, "File $outfile could not be opened.\n";

print OUTFILE '<?xml version="1.0" encoding="utf-8"?>'. "\n\n";

print OUTFILE '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'. "\n";
print OUTFILE '<channel>'. "\n\n";

print OUTFILE '<title>revford</title>'. "\n";
print OUTFILE '<link>http://revford.pwp.blueyonder.co.uk</link>'. "\n";
print OUTFILE '<description>Updates to revford and general purpose blog.</description>'. "\n";

# Date
$builddate = rssdate();

print OUTFILE '<lastBuildDate>' . $builddate  . '</lastBuildDate>'. "\n";
print OUTFILE '<language>en-gb</language>'. "\n\n\n";

print OUTFILE '<image> '. "\n";
print OUTFILE '  <url>http://revford.pwp.blueyonder.co.uk/images/logo.png</url>'. "\n";
print OUTFILE '  <width>128</width>'. "\n";
print OUTFILE '  <height>54</height>'. "\n";
print OUTFILE '  <title>revford</title>'. "\n";
print OUTFILE '  <link>http://revford.pwp.blueyonder.co.uk</link>'. "\n";
print OUTFILE '</image>'. "\n\n\n";

#foreach (@sortedlist)
foreach (0..19)
  {
  #my $value = $posttable{$_};
  my $value = $posttable{$sortedlist[$_]};

  open POSTFILE, "$postdir\/$value" or die RED, "File $postdir\/$value could not be opened.\n";
  @post = <POSTFILE>;
  close POSTFILE;

  $title = shift(@post); 
  $date  = shift(@post);
  $cat   = shift(@post); 

  chomp($title); 
  chomp($date);
  chomp($cat); 

  print OUTFILE '<item>' . "\n";

  print OUTFILE '<title>' . $title . '</title>' . "\n";

  ($mylink, $junk) = split(/\./, $value);

  print OUTFILE '<link>http://revford.pwp.blueyonder.co.uk/updates-1.html#post'; 
  print OUTFILE $mylink. '</link>' . "\n";
  print OUTFILE '<guid>http://revford.pwp.blueyonder.co.uk/updates-1.html#post'; 
  print OUTFILE $mylink . '</guid>' . "\n";
  print OUTFILE '<pubDate>' . $date  . '</pubDate>' . "\n";

  @categories = split(/ /, $cat);

  foreach (@categories)
    {
    print OUTFILE '<category>' . $_  . '</category>' . "\n";
    }

  print OUTFILE '<description>' . "\n";

  foreach (@post)
    {
    # replace any href=" that doesn't have an http:// with href="http://revford.pwp.blueyonder.co.uk/

    if (/href="/ and !/href="http/ )
      {
      $_ =~ s/href="/href="http:\/\/revford.pwp.blueyonder.co.uk\//;
      }

   # change the image tags, end with XML style /> and add the full path for local images
    if (/<img/)
      {
      $_ =~ s/>/\/>/;
      }

    if (/src="/ and !/src="http/)
      {
      $_ =~ s/src="/src="http:\/\/revford.pwp.blueyonder.co.uk\//;
      }


    $_ =~ s/<br>/<br\/>/g;
    $_ =~ s/</&lt;/g;
    $_ =~ s/>/&gt;/g;
    }

  print OUTFILE @post;
  print OUTFILE '</description>' . "\n";
  print OUTFILE '</item>' . "\n\n\n";
  };

print OUTFILE "\n\n\n"; 

print OUTFILE '<atom:link href="http://revford.pwp.blueyonder.co.uk/revford-rss.xml" rel="self"' . "\n";
print OUTFILE '  type="application/rss+xml" />' . "\n";
print OUTFILE '</channel>' . "\n";
print OUTFILE '</rss>';

close OUTFILE;

