#!/usr/bin/perl
# Kyle Christensen <kyle@junglist.org>
# http://junglist.org/files/scripts/cvs2rss.pl
# 
# This script (when setup properly) will magically create an RSS 2.0 compliant feed
# that logs your cvs commits as well as a diff of the changes made.
#
# Run this from your CVSROOT/loginfo like:
#
#	/path/to/cvs2rss.pl %{sVv}
#
# Also, make sure your apache server has an AddType for rss like:
#
# 	AddType application/rss+xml .rss
#
# P.S. The "other" cvs2rss.pl isn't really cvs2rss, it is more like cvs2xml, boo!
# P.P.S. Parts of this are stolen from commit2rss.rb and the XML::RSS examples.
#
# Version 1.0 - 08.24.04 RSS Compliant, no cvs diff capability yet..
# Version 1.1 - 08.31.04 Adding CVS Diff capability
# Edit by Guy Albertelli, -- corrected the packaging of Entities
#                          - commits now <pre>ed
#                          - removed html rants
#

use strict;
use HTML::Entities();
use XML::RSS;
use POSIX;

# Stuff you need to setup 
my $rssFeed ="/home/loninst/public_html/loncapa.rss";
my $emailDomain = "loncapa.org";
my $channelTitle = "Lon-CAPA RSS Feed";
my $channelLink = "http://install.loncapa.org/loncapa.rss";
my $numEntries = 200;

# Set this to 1 to enable cvs rdiff
my $cvsDiff = 1;

# Leave everything else alone
my $author = getpwuid(getuid()) . "\@" . $emailDomain;
$author = 'guy' . "\@" . 'albertelli.com';
my $pubDate = strftime('%a, %d %b %Y %H:%M:%S %Z',localtime(time));
my $description;
my @title=split(",",$ARGV[0]);

my $rss = new XML::RSS(version => '2.0');

# If rssFeed exists, parse it
if (-r "$rssFeed") {
	$rss->parsefile($rssFeed);
}

$rss->channel(
	title=> $channelTitle,
	link => $channelLink,
	language => 'en',
	description => $channelTitle,
	copyright => '(c) 2005 MSU Board of Trustees.',
	pubDate => $pubDate 
);


# Limit entries in the feed to $numEntries
pop(@{$rss->{'items'}}) while (@{$rss->{'items'}} >= $numEntries);

# Format title of the rss item
# Remove space, append / and set title to /file/that/changed - oldversion/newversion
$title[0] =~s/ /\//;

# Format the cvslog msg itself
while (<STDIN>) {
	chomp($_);
	if ($_=~/^[A-Z].*:\s*$/) {
		$_ = "<br /><b>" . &HTML::Entities::encode($_,'<>&"') . "</b><br />";
	}
	else {
		$_ .= "<br />";
		$_ = &HTML::Entities::encode($_,'<>&"');
	}
	$description .= $_;
}

if ($cvsDiff == 1) {
	# If the old version of the file is not NONE (if it isn't a new file), and if it is a .txt or .java file (that has no html)
	# This will rdiff it against the previous version, and include that diff in the rss feed

	if (($title[1] != "NONE") && ($title[0]=~/(.*).(pm)$/)){
		my $tmpFile = "/tmp/diff.$$";
		my $cmdLine = "cvs -n rdiff -u -kk -r " . $title[1] .  " -r " . $title[2] . " " . $title[0] . ">" . $tmpFile;
		system($cmdLine);

		$description .= "<br /><b>Differences:</b><br /><pre>";
		open CVSDIFF, "<" . $tmpFile;
		foreach my $line (<CVSDIFF>) {
			$description .= &HTML::Entities::encode($line,'<>&"');
		}
		$description .= "</pre>";
		unlink($tmpFile);   
	} 
}

$rss->add_item(
	title => "/" . $title[0] . " - " . $title[1] . "/" . $title[2],
	author => $author,
	description=> $description,
	mode => 'insert',
	pubDate => $pubDate,
	link => 'http://install.loncapa.org/cgi-bin/cvsweb.cgi/'.$title[0].'.diff?r1='.$title[1].';r2='.$title[2].';f=h'
);		

foreach my $element (@{$rss->{'items'}}) {
	$element->{'description'} = &HTML::Entities::encode($element->{'description'},'<>&"'); 
}

$rss->save($rssFeed);

# Rsync this rss feed to another publically accessable machine.
#my $cmdLine="rsync -r -e ssh --delete /export/www/rss/html/ builder\@monkey:/export/www/rss/html/";
#system($cmdLine);
