#!/usr/bin/perl 

use LWP::UserAgent ;
use XML::Parser ;
use XML::RSS ;

# this code from
# http://csociety.ecn.purdue.edu/~jacoby/Code/usemodwiki.txt
# where it was released as example code
#
# Modified by Chris Dent <cdent@blueoxen.org> June 2003
# for use with PurpleWiki
#
# To use adjust the values of $scriptname, $url and $wiki to reflect the
# site for which you wish to generate RSS 1.0 output. Also adjust
# the settings in the rss channel.

my $outputfile = $ARGV[0] || die "please provide an output file";
my $scriptname = 'wiki.pl';
my $url = qq(http://purplewiki.blueoxen.net/cgi-bin/$scriptname?RecentChanges) ;
my $wiki = q(http://purplewiki.blueoxen.net/cgi-bin) ;
my $rss = new XML::RSS ;
   $rss->channel (
   'title'       => qq(PurpleWiki) ,
   'link'        => qq($wiki/wiki.pl?FrontPage) ,
   'description' => qq(PurpleWiki Wiki) 
   ) ;

my $spy    = new LWP::UserAgent ; # Pull Agentmaking to main
   $spy->agent(qq(UseModWiki Spy)) ;

my $request = new HTTP::Request('GET',$url) ;
my $response = $spy->request($request);
my $output = $response->content ;
my @output = split /\n/ , $output ;

@output = grep /^<li>/ , @output ;

for my $line ( @output[0..7] ) {

  next if $line !~ /^<li>/ ;

  $line    =~ /(<a href="($scriptname\?([A-Za-z0-9]+))">)/ ; 
  my $anchor  = $1 ;
  my $link    = $2 ; 
  my $word    = $3 ;

  $line       =~ /strong>([^<]+)<\/strong/ ;
  my $comment = $1 ;
  $comment    = undef if $comment eq $anchor ;

  $word =~ s/([A-Z])/ $1/g ;

  $rss->add_item(
    'title'       => qq($word) ,
    'link'        => qq($wiki/$link) , 
    'description' => qq($comment) 
    ) ;

}

open  RSS , ">$outputfile" || die "unable to open $outputfile: $!";
print RSS $rss->as_string ;
close RSS ;

