#!/usr/bin/perl


if ( $ARGV[0] eq "-d" ) { 
	$DEBUG=1;
	shift;
}

if ( @ARGV ) { 
	$start = $ARGV[0];
	$old_last = $start - 1;
} elsif ( -f "$ENV{HOME}/.changes.last" ) { 
	open(O, "$ENV{HOME}/.changes.last" );
	$old_last = <O>;
	close O;
	$start = $old_last+1;
} else { 
	$old_last = $start = 10000;
}

print "start is $start\n" if $DEBUG;
open (I, "svn log -v -r $start:HEAD svn://svn/zcode |" ) || die "svn: $!";
print "svn log running ... \n" if $DEBUG;

while (<I>) { 
	print if $DEBUG;
	chop;
	if ( /^r([0-9]+) \| / ) { 
		$last=$1 if $1 > $last;
		print "last is $last\n" if $DEBUG;
	}
	if ( /^r[0-9]{5} / ) { 
		$pflag=1;
		next;
	}
	if ( /^Changed paths:/ && ( $pflag == 1 ) ) { 
		$pflag++;
		next;
	}
	next unless $pflag == 2;
	if ( /^\s*$/ ) { 
		$pflag = 0;
		next;
	}
	if ( m-/zcode/trunk- ) { 
		$ups{trunk}++;
	} elsif ( m-/zcode/branches/([^ :/]+)(/|:| |$)- ) { 
		$ups{$1}++;
	} elsif ( m-/zcode/assets/- ) { 
		$mods{assets}++;
	} elsif ( m-/zcode/tags/- ) { 
		# ignore
	} else { 
		die "cannot parse $_"
	}
	if ( m-/zcode/(branches/[^/]+|trunk)/([^ /:]+)[ :/]- ) { 
		$mods{$2}++;
	}
}

if ( $last eq "" ) { 
	print "No Changes since $old_last\n";
	exit 0;
}

if ( -f "$ENV{HOME}/views/trunk/BRANCHES" ) { 
	open (I, "$ENV{HOME}/views/trunk/BRANCHES") || die $!;
	warn "using $ENV{HOME}/views/trunk/BRANCHES\n";
} else { 
	open (I, "svn cat svn://svn/zcode/trunk/BRANCHES |") || die $!;
	warn "using svn\n";
}
my %BRANCHES;
while (<I>) { 
	next if /retired|ABANDONED/;
	next if /^#/;
	my ( $b, undef ) = split(/	/);
	$BRANCHES{$b}++;
}
close (I);

print "files changed by branch from $start to $last\n";
foreach $b ( keys %ups ) { 
	printf "%5d %s\n", $ups{$b}, $b;
	print "******** BRANCH $b is not listed in BRANCHES\n" if $BRANCHES{$b} eq "" && $b ne "trunk";
}
print "\nfiles changed by module\n";
foreach $b ( keys %mods ) { 
	printf "%5d %s\n", $mods{$b}, $b;
}

if ( $last >= $start ) { 
	open (O, ">$ENV{HOME}/.changes.last");
	print O $last;
	close O;
} else { 
	warn "last is less than start" if $DEBUG;
}

__END__

Changed paths:
   M /zcode/branches/zinc_beta4/ZV/mozilla/firefox3.5.3/firefox.exe

