#!/usr/bin/perl

use File::Find;

#from Makefile:
#PWD=/usr/local/apache/htdocs/photos/
#URL=http://swordfish.rdfweb.org/photos/

#traverse:
#        ./cal.pl "2004" $(PWD) $(URL)
#        ./cal.pl "2003" $(PWD) $(URL)
#        ./cal.pl "2002" $(PWD) $(URL)


my $dir = $ARGV[0];
my $pwd= $ARGV[1];
my $url = $ARGV[2];

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

my $outfilen=$dir . "ical.xml";
my $prevoutfilen= ($dir - 1) . "ical.xml";
my $nextoutfilen= ($dir + 1) . "ical.xml";

#print "DIR $dir.rss\n";

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

print OUT "<?xml-stylesheet href=\"ical.xsl\" type=\"text/xsl\" media=\"screen\"?>".
"\n<rdf:RDF ".
"\n  xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" ".
"\n  xmlns=\"http://www.w3.org/2002/12/cal/ical#\" ".
"\n  xmlns:dc=\"http://purl.org/dc/elements/1.1/\" ".
"\n  xmlns:rs=\"http://purl.org/net/rdf/papers/sitemap#\" ".
"\n  xmlns:p=\"http://www.usefulinc.com/picdiary/\" ".
"\n  xmlns:x=\"http://www.w3.org/2002/12/cal/prod/kanzaki.co_aecc9ad85690eae3#\" ". 
"\n>".

"\n <Vcalendar> ".
"\n  <prodid>-\/\/kanzaki.com\/\/RDFCal 1.0\/\/EN</prodid>".
"\n  <version>2.0</version>".
"\n  <method>PUBLISH</method>".
"\n  <x:wrCalname>Libby's photos " . $dir ."</x:wrCalname>".
"\n  <dc:source rdf:resource=\"".$url . $dir .".rss\"/>".
"\n  <rs:prev rdf:resource=\"".$prevoutfilen."\"/>".
"\n  <rs:next rdf:resource=\"".$nextoutfilen."\"/>";


print OUT $content;

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

close OUT;

# Seed the random number generator.
srand;

sub wanted{

my $filen=$File::Find::name;
my $fullpath="/Volumes/BigUn/apache/htdocs/photos/";

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


		$filen=~s/\/Volumes\/BigUn\/apache\/htdocs\/photos\///;

		my $bla=date($filen);
		print "[$bla]";
		$content=$content."\n <component>\n  <Vevent>";
		$content=$content."\n <url 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 ""){
			$title=~s/^\s*(.*?)\s*$/$1/s;
			my $yy;
			my $mm;
			my $dd;


				if($bla=~m/(\d\d\d\d)-(\d\d)-(\d\d)/){
				$yy=$1;
				$mm=$2;
				$dd=$3;
				$content=$content."\n   <dtstart rdf:parseType=\"Resource\">".
				"\n    <dateTime>".$bla."</dateTime>".
				"\n	<tzid>Europe/London</tzid>".
				"\n   </dtstart>";
				}
			$content=$content."\n    <summary>".$title."</summary>";

			my $filen2=$filen;
			$filen2=~s/(.*)index.html/$1/;

			if($dir<2003){
				if (($yy==2002 && $mm==8 && $dd<=13) ||($yy==2002 && $mm<8) || ($yy<=2001)){
				$filen2=$filen2;
				}else{
				$filen2=$filen2."Thumbnails/";
				}
			}else{
				if( ($yy==2003 && (($mm==3 && $dd<=4)||($mm==2 && $dd>=8))) || ($yy==2003 && $mm==1 && $dd<=30)){
				$filen2=$filen2."Thumbnails/";
				}else{
				$filen2=$filen2.$bla."-Thumbnails/";
				}
			}



####get highlights
#see http://www.thegodsofcode.com/

# Read entire directory

			my $thumbdir=$fullpath.$filen2;
			opendir(DIRECTORY,$thumbdir);
			@items = readdir(DIRECTORY);
			closedir(DIRECTORY);

print "!!!!! ".$thumbdir."\n";

			my @files;

# Extract just those files with the matching extension.

			foreach $item (@items) {
			   push(@files,$item) if ($item=~m/.JPG/i);
			}

# If there are files found get a random one.
			my $imagename;
			if (@files) {
			   $imagename = $files[rand(scalar(@files))];
			}
print "^^^".$url.$filen2.$imagename."\n";

			$content=$content."\n    <p:highlight rdf:resource=\"".$url.$filen2.$imagename."\"/> ";
			}
		$content=$content."\n  </Vevent>\n </component>\n";

	}


	sub date{

	my $ble=shift;

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

	}


}


