#! /usr/bin/env perl
BEGIN{$^W=1}  use strict;

use Data::Dumper;
use LWP::Simple;
use HTML::Entities;
use HTTP::Date qw(time2str str2time);

# Fichier temporaire à utiliser comme cache
my $cache_file="/tmp/geeksworld.xml";
# Delay de rafraichissement.
my $refresh_delay = 7200;
# nombre de strip à montrer
my $show_strip=8;

###########################################
# CGI : Try to use already processed file
###########################################

if ( -e $cache_file )   {
        # Récupération timestamp
        my @etat=stat($cache_file);
        my $fn_time = $etat[9];

        # Si fichier là depuis moins de 2 heures, réutiliser
        if(time() - $fn_time < 7200)       {
			# Vérifier que c'est bien nécessaire
			if( ($ENV{'HTTP_IF_MODIFIED_SINCE'}) && (str2time($ENV{'HTTP_IF_MODIFIED_SINCE'}) >= $fn_time) ) {
				print "Status: 304\n";
			}
			else	{
				print "Last-Modified: " . time2str($fn_time) ."\n"; 
#				print "Expires: " . time2str($fn_time+$refresh_delay) . "\n";
				print "Content-type: text/xml\n\n";
			
				open CACHE, "<$cache_file";
               	while(<CACHE>)  {
               		print $_;
               	}
			}
            exit;
        }
}

# Ecriture headers CGI
print "Last-Modified: " . time2str() ."\n"; 
#print "Expires: " . time2str(time()+$refresh_delay) . "\n";
print "Content-type: text/xml\n\n";

#############################################
# Load the main page, parse posts
#############################################
	# Idiots don't implement etag or last-modified, so we have to eat up 70k every time
my $main_page = get('http://www.geeksworld.org')
	or die "Unable to fetch page.\n\t";

#print "Fetched bytes ", length($main_page), " from Geeksworld\n";
decode_entities($main_page);

# Chope les titres
my @stories;
#while ($main_page =~ m#<option value='(\d+)'>$1 - (.+?) \(\d+-\d+-\d+\)</option>#gi )	{
while ($main_page =~ m#<option value='(\d+)'(?: selected="selected")?>\1 \- (.*?) \(\d+-\d+-\d+\)</option>#gi )	{
#	print "Found $1 - $2\n";
	$stories[$1]=$2;
}

# Chope le nombre de pages.
$main_page =~ m#<span class="page">Page \d+/(\d+)</span>#gi;
my $total_strip=$1;


#############################################
# Output RSS file
#############################################
my $i;
open RSS, ">$cache_file"	or die "Unable to write file $cache_file : $!";
print RSS <<"EOF";
<?xml version="1.0" encoding="iso-8859-1"?>
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://purl.org/rss/1.0/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
>
    <channel rdf:about="http://trolleur.net/cgi-bin/geeksworld.pl">
           <title>Geeksworld</title>
           <link>http://www.geeksworld.org</link>
           <description>A new comic every Monday, Wednesday and Friday</description>
           <language>en-us</language>
		   
		   <image rdf:about="http://www.geeksworld.org/imgs/logal.png">
		   	<title>Geeksworld</title>
			<link>http://www.geeksworld.org/</link>
			<url>http://www.geeksworld.org/imgs/logal.png</url>
		   </image>
		   
		   <items>
		   	<rdf:Seq>
EOF

for($i=0; $i<$show_strip; $i++)	{
	print RSS	'<rdf:li rdf.resource="http://www.geeksworld.org/strip_' . ($total_strip - $i). '.html"/>';
}

print RSS <<"EOF";
			 </rdf:Seq>
			</items>
		</channel>
EOF

for($i=0; $i<$show_strip; $i++)	
{
    print RSS '<item rdf:about="http://www.geeksworld.org/strip_' . ($total_strip - $i) . '.html">';
    print RSS '<title>' . $stories[($total_strip - $i)] . '</title>';
    print RSS '<link>http://www.geeksworld.org/strip_' . ($total_strip - $i)  . '.html</link>';
#	print RSS '<description>$story->{summary}</description>;

	print RSS '<content:encoded>&lt;img src=&quot;';

	# Trouver le bon format de fichier : GIF, PNG ou JPG
	my $begin_fn = sprintf "http://geeksworld.org/pages/%03d", ($total_strip - $i);
	if(head($begin_fn.".png"))	{
		print RSS $begin_fn . ".png";
	}
	elsif(head($begin_fn.".gif"))	{
		print RSS $begin_fn . ".gif";
	}
	else	{
		print RSS $begin_fn . ".jpg";
	}
	print RSS "&quot;&gt;</content:encoded></item>\n";
}

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


close RSS;

# finalement, ressort le cache
open CACHE, "<$cache_file";
        while(<CACHE>)  {
                print $_;
        }


