#!/usr/bin/perl

use File::Find;

my $pwd="/usr/local/projects/ilrt/WWW/discovery/";
my $url="http://www.ilrt.bristol.ac.uk/discovery/";

my $dir = $ARGV[0];

my $content="";
my $seq="";
find (\&wanted, $pwd . $dir);

my $outfilen=$dir . ".rss";

open (OUT,">$outfilen") or die($!);

print OUT "\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>".
"\n <channel rdf:about=\'".$url . $dir .".rss\'>" .
"\n    <title>ILRT Semantic Web Technical papers " . $dir ."</title>".
"\n    <link>$url$dir</link>".
 "\n    <description>Semantic Web Techical papers and software from ILRT " . $dir . "\n".
"\n    </description> ".

"\n   <items>\n      <rdf:Seq>\n " .
"$seq\n      </rdf:Seq>\n    </items>\n  </channel>";


print OUT $content;

print OUT "\n</rdf:RDF>";

close OUT;



sub wanted{

my $filen=$File::Find::name;

	if(($filen =~m/index.html/) || ($filen =~m/readme.html/)){

                if (!($filen =~m/semkos/) && (!($filen =~m/old/))){

		$filen=~s%.*/discovery/%%;

		my $bla=date($filen);
		print "[$bla]";
#		print "[$url$filen]\n";
		$content=$content."\n <item rdf:about=\'".$url.$filen."\'>";

		$seq=$seq . "\n<rdf:li rdf:resource=\'".$url.$filen."\'/>";

		my $title;
		print "* " . $pwd . $filen . "\n";
		
		$_newline = $/;
		$/ = '';

		open (IN,$pwd . $filen) or die($!);

			while(<IN>){
			if($_=~ /<title>\n?(.*?)\n?<\/title>/s){
			print $1;
			$title=$1;
			}


		#	print $_;

			}

		close (IN);
		$/ = $_newline;
			if($title ne ""){
			$content=$content."\n<title>".$title."</title>";
			$content=$content."\n<description>".$bla."</description>";
#			print $title;
			}
			else{
			$content=$content."\n<title>".$url.$filen."</title>";
			$content=$content."\n<description>".$bla."</description>";
			}
		$content=$content."\n <link>".$url.$filen."</link>";
		$content=$content."\n</item>";
		}

	}


	sub date{

	my $ble=shift;

	$ble=~s/(\d\d\d\d\/\d\d)\/.*/$1/;

	$ble=~s/\//-/g;
#	$ble=~s/-index.html//g;
	return $ble;

	}


}

