#-*-CPerl-*-

$hof{"megatokyo"} = 0;

#Megatokyo http://www.megatokyo.com/

#Design:
#
#First use a function to determine if the specified date exists,
#then have it return just a hash to update the rli with the correct info.

sub megatokyo {
    my $time = shift;
	my @ltime = gmtime($time);
    return undef if $time < mkgmtime(0,0,0,14,7,100); #1st comic: Aug 14, 2000
    my $date = strftime("%Y-%m-%d", @ltime);
    my $baseurl = "http://www.megatokyo.com";
    my $rec = {
	'title' => "Megatokyo",
	'author' => "Fred Gallagher & Rodney Caston",
	'type' => 'gif',
	'main' => $baseurl,
	'archives' => "$baseurl/index.php",
	'base' => "$baseurl/",
	'page' => "index.php",
	'func' => sub {
			my $input_page = shift;
			my @relurls;
			if (grep(/$date/, $input_page)) {
				$_ = $input_page;

				# Get the strip number.
				# Looks like this may be broken, or possibly 0000-00-00 is the
				# code for "today" now...
				m/$date\s\[(\d+)\]/;
				my $strip_num = $1;

				if(length($strip_num) < 4) {
					$strip_num = add_zero($strip_num, 4);
				}
				# Strip leading 0 from comics.
				#$strip_num =~ s/^0// if grep(/^0/, $strip_num); 
				push(@relurls, {
								'page' => strftime("index.php?strip_id=$strip_num",@ltime),
								'exprs' => {
											'comic'=>["(strips/$strip_num.(gif|jpg|jpeg))"],
											'caption'=>["$date\\s\\[\\d+\\]\\s(.+?)\\<\\/option\\>"],
										   },
								'func' => undef
				});
			} else {
				push(@relurls, {
								'page' => undef,
								'func' => undef,
								'base' => undef,
								'status' => 0
							   }
					);
			};
			return @relurls;
		}
	};
    return $rec;
}

sub add_zero {
	my $strip_num = shift;
	my $total = shift;
	$strip_num = "0".$strip_num;
	if(length($strip_num) < 4) {
		$strip_num = add_zero($strip_num, 4);
	}
	return $strip_num;
}

1;

