#!/s/std/bin/perl

#-------------------------------------------------------------------------------
# USAGE: comics.pl [YYYYMMDD]
#-------------------------------------------------------------------------------

BEGIN {push @INC, '/u/e/n/englet/public/html/comics/';}

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Request::Common;
use Time::Local;

use comics;

sub download_comic($);

my $DEBUG = 0;
my $VERSION = v1.0.1;

my $item;
my $prefix = '/u/e/n/englet/public/html/comics/';

# If this script is called with a YYYYMMDD timestamp, then use that date, otherwise use the current date.
my $now = undef;
if ((defined $ARGV[0]) && ($ARGV[0] =~ m/(\d{4})(\d\d)(\d\d)/)) 
{
    $now = timelocal(0,0,0, $3, $2-1, $1-1900) 
} else {
    $now = time();
}
print(localtime($now)."\n");

my @time   = localtime($now);
my $obj    = new comics::(\@time);
my %list   = $obj->list();

my $agent = LWP::UserAgent->new;
#my $hdrs = new HTTP::Headers('User-Agent' => "Ima JRATT/$VERSION");
# 20080605 - UComics.com seems to check the user agent string now.  Use the same as wget.
my $hdrs = new HTTP::Headers('User-Agent' => "Wget/1.10.2");

#-------------------------------------------------------------------------------
# Go through the lits of comics and download each one.
#-------------------------------------------------------------------------------
foreach $item (keys %list) 
{
	$list{$item}{name} = $item;
	download_comic($list{$item});
};

#-------------------------------------------------------------------------------
# Sub procedures for downloading the comics...
#-------------------------------------------------------------------------------

# Download comic image
sub download_comic($) 
{
	my %item  = %{$_[0]};
	#my $file = sprintf("$item{fill}-%04d%02d%02d.gif", $time[5]+1900,$time[4]+1,$time[3]);
	my $file = $item{file};
	my $folder = $item{folder};
	unless ( ( -e $prefix.$folder) and ( -d $prefix.$folder) ) {mkdir "$prefix$folder"};
	my $page_url;
	my $image_url;
	my $req; 
	my $response;

	print "\$file = $file\n" if ($DEBUG);

	unless ( -e $prefix.$folder."/".$file) 
	{
		if ( $item{search} )
		{
			# For comics.com we have to first download the page and search for the name of the image file.
			$page_url = "http://www.comics.com/$item{fill}/"
			$image_url = undef;

			# B.C. and a few others are different because they're from Creators Syndicate Inc.
			#if( $item{creators} )
			#{
			#	$page_url = sprintf("http://www.comics.com/creators/$item{fill}/archive/$item{fill}-%04d%02d%02d.html", $time[5]+1900,$time[4]+1,$time[3]);
			#}

			$req = HTTP::Request->new("GET" => $page_url);
			$response = $agent->request($req);
			if ($response->is_success)
			{
				if (($response->content) =~ m@$item{search}@) 
				{
					$image_url = $1;
				}
				else
				{
					print STDERR "Failed to parse $item{name} page\n";
					return;
				}
			}
			else
			{
				print STDERR "Failed to get $item{name} page   ($page_url)\n";
				return;
			}
		}
		else
		{
			$image_url = $item{url};
		}

		# Download the image file.
		$hdrs->referrer($item{referer}) if defined $item{referer};
		$req = new HTTP::Request("GET", $image_url, $hdrs);
		$response = $agent->request($req, $prefix.$folder."/".$file);
		if ($response->is_success)
		{
			print "Got $item{name}\n";

			# Copy the latest image to a static location for comics_static.html
			`cp $prefix$folder/$file $prefix$folder/$folder.gif`;
		}
		else
		{
			print STDERR "Failed to get $item{name} image    ($image_url)\n";
			print STDERR "Status: " . $response->status_line . "\n";
		}
	}
}
