#!/usr/bin/perl

=head1 NAME
  SummaryDiffs - show changes for TuneBot summary files

=head1 SYNOPSIS
  SummaryDiffs [file]..

=head1 DESCRIPTION
  This reads the Summary.* files generated by TuneBot and
  produces a list of the URLs for which the number of tunes
  changed.


  We have two ways of showing the change, determined by the
  $want variable:  'minmax' shows the lowest and highest
  number of tunes found for each URL (but there's no way
  of seeing whether the number went up or down).  'change'
  means to show the first and last count

=head1 OPTIONS

=head1 EXAMPLES

=head1 FILES

=head1 BUGS

=head1 SEE ALSO

=head1 AUTHOR
  John Chambers <jc@trillian.mit.edu>
=cut


$| = 1;
$want = 'minmax';	# Show the min and max values.
$want = 'change';	# Show the first and last values.

for $f (@ARGV) {
	unless (open(F,$f)) {
		print STDERR "$0: Can't read \"$f\" ($!)\n";
		next;
	}
	++$file;		# Count the files.
	for $l (<F>) {
		if (
			(($n,$x,$u) = ($l =~ /^\s*(\d+) ([<+:]) (.*)\s*$/))
		||	(($n,$x,$u) = ($l =~ /^\s*(\d+)( )(.*)\s*$/))
		) {
			next if ($x eq '<' || $x eq ':');
			if ($want eq 'minmax') {
				if (defined $U{$u}) {
					$U{$u} .= ",$n";
				} else {
					$U{$u} = $n;
				}
			} elsif ($want eq 'change') {
				$U{$u}++;	# Number of times each URL encountered.
				if ($file == 1) {
					$F{$u} = $n;
				} else {
					$L{$u} = $n;
				}
			} else {
				print STDERR "$0: Don't know what is wanted.\n";
				exit 1;
			}
		}
	}
}

for $u (sort keys %U) {
	if (@x = split(',',$U{$u})) {
		if ($want eq 'minmax') {
			$lo = 999999999;
			$hi = 0;
			while (@x) {
				$n = shift @x;
				$lo = $n if $n < $lo;
				$hi = $n if $n > $hi;
			}
			printf("%7d %7d %s\n",$lo,$hi,$u) if $lo != $hi;
		} elsif ($want eq 'change') {
			$n1 = $F{$u} || 0;
			$n2 = $L{$u} || 0;
			printf("%7d %7d %s\n",$n1,$n2,$u) if $n1 != $n2;
		}
	}
}
