#!perl

$baseurl="";
$fromurl="";
$tourl="";
$startofurl="";

$debug=0;

if ($ARGV[0] eq '-debug') {
	shift;
	$debug=1;
}

if ($#ARGV >= 0) {
	$baseurl=$ARGV[0];
} else {
	print STDERR "Usage: $0 [svnfromurl] svntourl\n";
	exit(1);
}
if ($#ARGV >= 1) {
	$fromurl=$ARGV[0];
	$tourl=$ARGV[1];
}
if ($fromurl eq "" || $tourl eq "") {
	open SVN, "svn list $baseurl|";
	while (<SVN>) {
		chomp;
		if (m{^([^/]*)/}) {
			$version=$1;
			$thisisnewer=1;
			@thisone=split(/[\/\.]/, $baseurl."/".$1);
			@oldone=split(/[\/\.]/, $tourl);
			#print "@thisone >? @oldone\n" if ($debug);
			for($i=0;$i<=$#thisone && $i<=$#oldone;$i++) {
				#print "$thisone[$i] >? $oldone[$i]\n" if ($debug);
				if ($thisone[$i] < $oldone[$i]) {
					$thisisnewer=0;
					last;
				}
			}
			if ($thisisnewer) {
				$fromurl=$tourl;
				$tourl=$baseurl."/".$1;
				#print "Comparing $fromurl to $tourl\n" if ($debug);
			}
		}
	}
	close SVN;
}

print "Comparing $fromurl to $tourl\n" if ($debug);

if ($fromurl eq "" || $tourl eq "") {
	print STDERR "Cannot locate the from and to urls\n";
	print STDERR "Usage: $0 [svnfromurl] svntourl\n";
	exit(1);
}

print "svn log -v $fromurl\n" if ($debug);
open SVN, "svn log -v $fromurl|";
while (<SVN>) {
	if (m{([/\\].*) \(from ([^:]*):([0-9]*)\)}) {
		$base=$1;
		$path=$2;
		$rev=$3;
		$path =~ tr{\\}{/};
		$base =~ tr{\\}{/};
		while (rindex($fromurl, $base) < 0) {
			#print "$base not found in $fromurl\n" if($debug);
			if ($base =~ m{^(.*)/[^/]*}) {
				$base=$1;
			} else {
				last;
			}
		}
		$startofurl=substr($fromurl, 0, rindex($fromurl, $base));
		print "FROM:Base:$startofurl Path:$path Revision:$rev\n" if($debug);
		$fromrevisions{$path}=$rev;
	}
}
close SVN;

if ($tourl eq "HEAD") {
	foreach $path (keys %fromrevisions) {
		print "TO:Path:$path Revision:HEAD\n" if($debug);
		$torevisions{$path} = "HEAD";
	}
} else {
	print "svn log -v $tourl\n" if ($debug);
	open SVN, "svn log -v $tourl|";
	while (<SVN>) {
		if (m{([/\\].*) \(from ([^:]*):([0-9]*)\)}) {
			$base=$1;
			$path=$2;
			$rev=$3;
			$path =~ tr{\\}{/};
			$base =~ tr{\\}{/};
			while (rindex($tourl, $base) < 0) {
				#print "$base not found in $tourl\n" if($debug);
				if ($base =~ m{^(.*)/[^/]*}) {
					$base=$1;
				} else {
					last;
				}
			}
			$startofurl=substr($tourl, 0, rindex($tourl, $base));
			print "TO:Base:$startofurl Path:$path Revision:$rev\n" if($debug);
			$torevisions{$path}=$rev;
		}
	}
	close SVN;
}

print "All log entries from $fromurl to $tourl\n";
foreach $path (keys %torevisions) {
	$torev=$torevisions{$path};
	$fromrev=$fromrevisions{$path};
	$fromrev=0 if ($fromrev eq "");
	$fromrev++;
	if ($torev >= $fromrev || $torev eq "HEAD") {
		print "Changes in $startofurl$path from $fromrev to $torev\n";
		system("svn log -r $fromrev:$torev $startofurl$path");
	}
}



