#!/usr/bin/perl
use Data::Dumper;

$url = shift;
if ($url !~ /\/$/) { die "Need a URL with trailing slash (i.e. not a page link)"; }

$url =~ /\/((?:http:\/\/)?[^\/]*)\/$/;
$realurl = $1;
$realurl =~ /(?:http:\/\/)?(.*)/;
$site = $1;
$url =~ /\/([0-9]{6})[0-9]+\//;  # year and month
$datecode = $1;

print "Fetching site $site at $realurl from $url\n";

system("wget -m -Uopera $url");
my $oldlist = '';

my @list;

while (1) {

@list = `find .`;
@list = grep /\.htm[l]?$/i, @list;
#print Dumper @list;
@list = grep /\/$datecode [0-9]+\//ix, @list;
#print Dumper @list;
$newlist = Dumper(@list);
#print "$newlist $oldlist";
if ($newlist eq $oldlist) { last; }

foreach my $file (@list) {
	if ($file =~ /\.htm[l]?$/i) {
		open $fh, "<$file" or die;
		local $/ = undef;
		$data = <$fh>;
		close $fh;
		open $fh, ">$file" or die;
		
		$data =~ s/(<\s*BASE.*?HREF\s*=["]?).*?(["]?>)/$1$url$2/gis;

		$data =~ s/(<\s*a.*?href=\s*["]?)(?:http:\/\/)?$site [\/]?(.*?["]?>)/$1$url$2/gixs;
		$data =~ s/(<\s*(?:img|embed).*?src=\s*["]?)(?:http:\/\/)?$site [\/]?(.*?["]?>)/$1$url$2/gixs;
		$data =~ s/(<\s*body.*?background=\s*["]?)(.*?["]?>)/$1$url$2/gis;

		# kill ftp links
		$data =~ s/(<\s*a.*?href=\s*["]?)ftp:\/\/.*?(["]?>)/$1#$2/gixs;

		@parse = split /(<\s*a.*?href=\s*["]?.*?["]?>.*?<\/a>)/i, $data;
		if (scalar(@parse) > 1) {
			$data = '';
			foreach my $item (@parse) {
				if ($item =~ /(<\s*a.*?href=\s*["]?http:\/\/web\.archive\.org.*["]?>.*?<\/a>)/i) {
					$data .= $item;
				}
				else {
					# neutralize offsite links since wget -i can't use --exclude-domains
					$item =~ s/(<\s*a.*?href=\s*["]?)(http:\/\/.*?)(["]?>)/$1$url$2$3/ig;
					$item =~ s/(<\s*a.*?href=\s*["]?)(www\..*?)(["]?>)/$1$url$2$3/ig;
					$data .= $item;
				}
			}
		}
		$data =~ s/<\s*a.*?href\s*=\s*["]?$realurl(.*?)["]?>/<a href="$url$1">/igs;
		print $fh $data;
		close $fh;
		$cmd = "wget --dns-timeout=10 --connect-timeout=10 --tries=2 -r -F -nc -Uopera -i $file";
		print $cmd;
		system($cmd);
	}
}
$oldlist = $newlist;

}


foreach my $file (@list) {
	print "Postprocessing $file\n";
	open $fh, "<$file" or die;
	local $/ = undef;
	$data = <$fh>;
	close $fh;
	@mystat = stat $file;
	$mytime = $mystat[9];
	# relativize links
	$data =~ s/<\s*BASE.*?HREF\s*=["]?.*?(["]?>)//gi;
	$data =~ s/http:\/\/web.archive.org\/web\/[0-9]+\/(?:http:\/\/)?$site\///g;
	# remove alexa cruft
	$data =~ s/<SCRIPT language="Javascript">\n<!--\n\n\/\/ FILE ARCHIVED ON.*?<\/script>//gsi;
	open $fh, ">$file" or die;
	print $fh $data;
	close $fh;
	utime $mytime, $mytime, $file;
}

