#!/usr/bin/perl

=head1 NAME
  HostStatDiffs - show changes for TuneBot summary files

=head1 SYNOPSIS
  HostStatDiffs [file]..

=head1 DESCRIPTION
  This reads the HostStat-* files generated  by  HostStats  from  the
  hst/*  files,  and produces a list of the URLs for which the number
  of tunes changed.

  The output is a list of hosts, with the current tune count and the
  changes in the counts of tunes/titles/files for each host.

=head1 OPTIONS

=head1 EXAMPLES

=head1 FILES

=head1 BUGS

=head1 SEE ALSO

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

$| = 1;
($P = $0) =~ s"^.*/"";
$V = $ENV{"V_$P"} || 1;

for $fnam (@ARGV) {
	unless (open(F,$fnam)) {
		print STDERR "$0: Can't read \"$fnam\" ($!)\n";
		next;
	}
	++$fnum;		# Count the files.
	for $l (<F>) {
		if (($xcnt,$tcnt,$fcnt,$host) = ($l =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/)) {
			$host =~ s/\.+$//;
			print "$P: x=$xcnt t=$tcnt f=$fcnt $host\n" if $V>1;
			$h{"$host"} ++;
			$x{"$host:$fnum"} = $xcnt;
			$t{"$host:$fnum"} = $tcnt;
			$f{"$host:$fnum"} = $fcnt;
		}
	}
}

print "   TUNES   diff |   TITLES   diff |    FILES   diff | HOST\n";
for $h (sort keys %h) {
	for ($n=1; $n<=$fnum; $n++) {
		$x{"$h:$n"} = 0 unless defined $x{"$h:$n"};
		$t{"$h:$n"} = 0 unless defined $t{"$h:$n"};
		$f{"$h:$n"} = 0 unless defined $f{"$h:$n"};
	}
	$xdif = $x{"$h:$fnum"} - $x{"$h:1"};
	$tdif = $t{"$h:$fnum"} - $t{"$h:1"};
	$fdif = $f{"$h:$fnum"} - $f{"$h:1"};
	printf "%8d %6d | %8d %6d | %8d %6d | %s\n",
		$x{"$h:$fnum"},$xdif,$t{"$h:$fnum"},$tdif,$f{"$h:$fnum"},$fdif,$h;
}
