#!/usr/bin/perl -W
# which strips?  add these to the following variable to add support for them.
# foxtrot dildbert calvin rwwr pennyarcade ctrlaltdel dieselsweeties
# modestdestiny userfriendly redmeat
my $favourites = "foxtrot dilbert calvin rwwr pennyarcade ctrlaltdel dieselsweeties modestdestiny userfriendly redmeat 8bittheatre dinosaur";

# set verbosity level.  0 is silent, 1 shows output progress and status.
my $verbose=1;

# flags to pass to wget.  don't know why you'd need 'em, but you can set them.
# by default, comics.pl will use lwp. if you prefer wget, uncomment this.
my $wget_flags = "";

# be sure to set this to an appropriate value.  i keep my comics in 
# ~/Documents/comics, so if you keep them elsewhere, adjust as necessary.
my $comicsdir = "$ENV{HOME}/Documents/comics"; 

# default browser
# comment this out if you do not wish to view your latest comics immediately
my $browser = "/usr/bin/firefox -remote \"openURL(file://$comicsdir/comics.html,new-tab)\"";

# setting this to zero will cause every execution to generate a special page of
# thumbnails for that strip.  this job could take a *very* long time to finish,
# depending on the size of your comics archive.
my $thumbnail_index=1;

# when using the thumbnail index, this specifies the size of the thumbnails to
# use.  percentages only, no % sign please.  20 = 20%, 40=40% (etc)
my $thumbnail_scale=25;






















use LWP::Simple;
use File::Copy;
use Digest::MD5;
use URI::Split qw(uri_split uri_join);
# DO NOT TOUCH ME
chdir ($comicsdir);

# day/month/year
my $lastrunday = (localtime)[3] . "/" . ((localtime)[4] + 1) . "/" . 
    ((localtime)[5] + 1900);

sub cmd5 {
    open (FH, "$_[0]");
    binmode (FH);
    my $hash = Digest::MD5->new->addfile (*FH)->hexdigest;
    close (FH);
    return $hash;
}

sub display {
    if ($verbose == 1)
    {
	print "@_" . "\n";
    }
}

my %config;
open (CONFIG, ".config");
while (<CONFIG>) {
    $_ =~ /^(.+?) (.+?)$/;
    $config{$1} = $2;
}
close (CONFIG);

if (defined $ARGV[0]) {
    if ($ARGV[0] == "-q")
    {
	$verbose = 0;
    }
}

if ($verbose == 0) {
    $wget_flags = "-q";
} 

sub listed {
    if ($favourites =~ $_[0])
    {
	return 1;
    } else {
	return 0;
    }
}

sub download {
    qx {wget $wget_flags $_[0]};
}

sub redmeat {
    chomp ($config{redmeat_lastday});
    chomp ($lastrunday);
    if ($config{redmeat_lastday} eq $lastrunday) {
	display "skipping redmeat (will not scan more than once per day)";
	return "redmeat/$config{redmeat_count}.gif";
    }

    mkdir "redmeat/";
    display "scanning redmeat...";
    download "http://www.redmeat.com/redmeat/current/index-1.gif";
    my $hash = cmd5 "index-1.gif";
    if ($hash eq $config{redmeat_hash})
    {
	display "strip already fetched.  deleting...";
	unlink ("index-1.gif");
    } else {
	$config{redmeat_count}++;
	$config{redmeat_hash} = $hash;
	$config{redmeat_lastday} = $lastrunday;
	move ("index-1.gif", "redmeat/" . $config{redmeat_count} . ".gif");
    }
    return "redmeat/$config{redmeat_count}.gif";
}

sub dilbert {
    display "scanning dilbert...";
    my $content = LWP::Simple::get "http://www.dilbert.com";
    if (defined $content)
    {
	if ($content =~ m@(/comics/dilbert/archive/images/dilbert.*?\....)@i)
	{
	    my $image = "http://www.dilbert.com$1";
	    my $fname = $image;
	    $fname =~ s/http:\/\/www.dilbert.com\/comics\/dilbert\/archive\/images\///;
	    if ( -e "dilbert/$fname" )
	    {
		display "latest dilbert exists...";
		return "dilbert/$fname";
	    }
	    display "getting dilbert...";
	    mkdir "dilbert/";
	    download $image;
	    move ($fname, "dilbert/");
	    return "dilbert/" . $fname;
	}
	else
	{
	    return "";
	}
    } else {
	return "";
    }
}

sub foxtrot {
    display "scanning foxtrot...";
    my $content =
	LWP::Simple::get 'http://www.ucomics.com/foxtrot/index.phtml';
    if (defined $content) 
    {
	if ($content =~ m@(http://images.ucomics.com/comics/ft/.*?gif)@i) 
	{
	    my $image = $1;
	    my $fname = $image;
	    $fname =~ s/http:\/\/images.ucomics.com\/comics\/ft\/2005\///;
	    if ( -e "foxtrot/$fname" )
	    {
		display "latest foxtrot exists...";
		return "foxtrot/" . $fname;
	    }
	    display "getting foxtrot...";
	    mkdir "foxtrot/";
	    download $image;
	    move ($fname, "foxtrot/$fname");
	    return "foxtrot/" . $fname;
	} 
	else 
	{
	    return "";
	}
    }
    else
    {
	return "";
    }
}

sub calvin {
    display "scanning calvin...";
    my $content =
	LWP::Simple::get 'http://www.ucomics.com/calvinandhobbes/index.phtml';
    if (defined $content) 
    {
	if ($content =~ m@(http://images.ucomics.com/comics/ch/.*?gif)@i) 
	{
	    my $image = $1;
	    my $fname = $image;
	    $fname =~ s/.*\///;
	    if ( -e "calvin/$fname" )
	    {
		display "latest calvin exists...";
		return "calvin/" . $fname;
	    }
	    display "getting calvin...";
	    mkdir "calvin/";
	    download $image;
	    move ($fname, "calvin/$fname");
	    return "calvin/" . $fname;
	} else {
	    return "";
	}
    }
    else
    {
	return "";
    }
}

sub rwwr{
    display "scanning rwwr...";
    my $content = LWP::Simple::get "http://www.rwwr.com";
    if (defined $content)
    {
	if ($content =~ m@(http://www.rwwr.com/images/strips/200.*.jpg)@i)
	{
	    my $image = $1;
	    my $fname = $image;
	    $fname =~ s/http:\/\/www.rwwr.com\/images\/strips\///;
	    if ( -e "rwwr/$fname")
	    {
		display "latest rwwr exists...";
		return "rwwr/" . $fname;
	    }
	    display "getting rwwr...";
	    mkdir "rwwr/";
	    download $image;
	    move ($fname, "rwwr");
	    return "rwwr/" . $fname;
	} else {
	    return "";
	}
    } else { return ""; }
}

sub pennyarcade {
    display "scanning pennyarcade...";
    my $content = LWP::Simple::get "http://www.penny-arcade.com/view.php3";
    if (defined $content)
    {
	if ($content =~ m@(images/2005/2005.*.jpg)@i)
	{
	    my $image = "http://www.penny-arcade.com/$1";
	    my $fname = $image;
	    $fname =~ s/.*\///;
	    if ( -e "pennyarcade/$fname")
	    {
		display "latest pennyarcade exists...";
		return "pennyarcade/" . $fname;
	    }
	    display "getting pennyarcade...";
	    mkdir "pennyarcade/";
	    download $image;
	    move ($fname, "pennyarcade");
	    return "pennyarcade/" . $fname;
	} else {
	    return "";
	}
    } else { return ""; }
}

sub ctrlaltdel {
    display "scanning ctrlaltdel...";
    my $content = LWP::Simple::get 
	"http://www.ctrlaltdel-online.com/index.php?t=archives&date=last";
    if (defined $content)
    { 
	if ($content =~ m@(\./images/comics/.*.jpg)@i)
	{
	    my $image = "http://www.ctrlaltdel-online.com/" . $1;
	    my $fname = $image;
	    $fname =~ s#/./##;
	    $fname =~ s/http:\/\/www.ctrlaltdel-online.comimages\/comics\///;

	    if ( -e "ctrlaltdel/$fname")
	    {
		display "latest ctrlaltdel exists...";
		return "ctrlaltdel/" . $fname;
	    }
	    display "getting ctrlaltdel...";
	    mkdir "ctrlaltdel/";
	    download $image;
	    move ($fname, "ctrlaltdel");
	    return "ctrlaltdel/" . $fname;
	} else {
	    return "";
	}
    } else { return ""; }
}

sub dieselsweeties {
    display "scanning dieselsweeties...";
    my $content = LWP::Simple::get "http://www.dieselsweeties.com";
    if (defined $content)
    {
	if ($content =~ m@(/hstrips/.*.gif)@i)
	{
	    my $image = "http://www.dieselsweeties.com$1";
	    my $fname = $image;
	    $fname =~ s/.*\///;
	    if ( -e "dieselsweeties/$fname")
	    {
		display "latest dieselsweeties exists...";
		return "dieselsweeties/" . $fname;
	    }
	    display "getting dieselsweeties...";
	    mkdir "dieselsweeties/";
	    download $image;
	    move ($fname, "dieselsweeties");
	    return "dieselsweeties/" . $fname;
	} else {
	    return "";
	}
    } else { return ""; }
}

sub modestdestiny {
    display "scanning modestdestiny...";
    my $content = LWP::Simple::get "http://www.squidi.net/index.php";
    if (defined $content)
    {
	if ($content =~ m@(amd/comic/.*.png)@i)
	{
	    my $image = "http://www.squidi.net/$1";
	    my $fname = $image;
	    $fname =~ s/http:\/\/www.squidi.net\/amd\/comic\///;
	    if ( -e "modestdestiny/$fname")
	    {
		display "latest modestdestiny exists...";
		return "modestdestiny/" . $fname;
	    }
	    display "getting modestdestiny...";
	    mkdir "modestdestiny/";
	    download $image;
	    move ($fname, "modestdestiny");
	    return "modestdestiny/" . $fname;
	} else {
	    return "";
	}
    } else { return ""; }
}

sub userfriendly {
    @lt = localtime;
    $lt[5] += 1900; $lt[4] += 1;
    $yyyymmdd = sprintf('%04d%02d%02d', @lt[5,4,3]);
    display "scanning userfriendly...";
    my $content = LWP::Simple::get "http://ars.userfriendly.org/cartoons/?id=$yyyymmdd";
    if (defined $content)
    {
	if ($content =~ m@(www.userfriendly.org/cartoons/archives/.*/.*.gif)@i)
	{
	    my $image = "$1";
	    my $uri = "http://$image";
	    $uri =~ m/(uf0*.*.gif)$/;
	    $fname = $1;
	    if ( -e "userfriendly/$fname")
	    {
		display "latest userfriendly exists...";
		return "userfriendly/" . $fname;
	    }
	    display "getting userfriendly...";
	    mkdir "userfriendly/";
	    download $image;
	    move ($fname, "userfriendly/");
	    return "userfriendly/" . $fname;
	} else { display "no matches"; exit;
	    return "";
	}
    } else { return ""; }
}

sub eightbittheatre {
    display "scanning eight bit theatre...";
    my $content = LWP::Simple::get "http://www.nuklearpower.com/latest.php";
    if (defined $content)
    {
	if ($content =~ m@(http://www.nuklearpower.com/comics/.*.png)@i)
	{
	    my $image = "$1";
	    my $fname = $image;
	    $fname =~ s/.*\///;
	    if ( -e "8bittheatre/$fname")
	    {
		display "latest eight bit theatre exists...";
		return "8bittheatre/" . $fname;
	    }
	    display "getting 8bittheatre...";
	    mkdir "8bittheatre/";
	    download $image;
	    move ($fname, "8bittheatre/");
	    return "8bittheatre/" . $fname;
	} else { display "no matches"; exit;
	    return "";
	}
    } else { return ""; }
}

sub dinosaur {
    display "scanning dinosaur comics...";
    my $content = LWP::Simple::get "http://www.qwantz.com/";
    if (defined $content)
    {
	if ($content =~ m@(comics/.*.png)@i)
	{
	    my $image = "$1";
	    my $fname = $image;
	    $fname =~ s/.*\///;
	    if ( -e "dinosaur/$fname")
	    {
		display "latest dinosaur comics exists...";
		return "dinosaur/" . $fname;
	    }
	    display "getting dinosaur...";
	    mkdir "dinosaur/";
	    download "http://www.qwantz.com/$image";
	    move ($fname, "dinosaur/");
	    return "dinosaur/" . $fname;
	} else { display "no matches"; exit;
	    return "";
	}
    } else { return ""; }
}

if ($favourites =~ "userfriendly")     { push @fetched, userfriendly(); }
if ($favourites =~ "dilbert")          { push @fetched, dilbert(); }
if ($favourites =~ "foxtrot")          { push @fetched, foxtrot(); }
if ($favourites =~ "calvin")           { push @fetched, calvin(); }
if ($favourites =~ "rwwr")             { push @fetched, rwwr(); }
if ($favourites =~ "pennyarcade")      { push @fetched, pennyarcade(); }
if ($favourites =~ "ctrlaltdel")       { push @fetched, ctrlaltdel(); }
if ($favourites =~ "dieselsweeties")   { push @fetched, dieselsweeties(); }
if ($favourites =~ "modestdestiny")    { push @fetched, modestdestiny(); }
if ($favourites =~ "8bittheatre")      { push @fetched, eightbittheatre(); }
if ($favourites =~ "dinosaur")         { push @fetched, dinosaur(); }
if ($favourites =~ "redmeat")          { push @fetched, redmeat(); }
 
sub show_link
{
    # $_[0] is the strip name, $_[1] is the actual link
    return "\<a href=\"$_[0]\"\>\n \<img border=0 src=\"$_[1]\"\>\n\</a\>\<hr\>";
}

#generating html...
display "generating html...";
open (HC, ">comics.html");
print (HC "<html> <head> <title>Autogenerated comics list (with archive links)</title> </head>\n");
print (HC "<body bgcolor=\"#000000\" text=\"#ffffff\">");
foreach my $images (@fetched) {
    my ($archive, $src);
    ($archive, $src) = split (/\//, $images);
    display ("$archive - $src");
    if ($thumbnail_index == 0)
    {
	print (HC "<h3>$archive</h3>\n");
    } else {
	print (HC "<h3>");
	print (HC "<a href=\"" . $archive . "/thumbs.html\">" . $archive . "</a>");
	print (HC "</h3>\n");
    }
    print (HC show_link($archive, $images));
}
print (HC "\</body\>\n\</html\>");
close (HC);

display ("updating configuration...");
open (CONFIG, ">.config");
print (CONFIG "redmeat_count $config{redmeat_count}\n");
print (CONFIG "redmeat_hash $config{redmeat_hash}\n");
print (CONFIG "redmeat_lastday $lastrunday\n");
close (CONFIG);
qx {cat .config};

foreach my $comic (@fetched)
{
    my ($dirname, $trash) = split (/\//, $comic);
    @images = `ls $dirname | grep -v thumbs.html`;
    open (TF, ">$dirname\/thumbs.html");
    print (TF "<html><head><title>$dirname stored</title></head><body>\n");
    foreach my $file (@images)
    {
	$file =~ s/\n//;
	print (TF "\<a href=\"$file\"\>\<img src=\"$file\" border=0 width=\"$thumbnail_scale\%\" style=\"margin:16px\"\>\</a\>");
    }
    close (TF);
}

if (defined $browser)
{
    qx {$browser};
}

die 0;
# testing section
