#!/usr/bin/perl
#
#NAME
#  HostStats [files]
#
#SYNOPSIS
#  HostStats >HostStatsData-`dt`
#
#DESCRIPTION
# Generate a summary of host statistics from the last few runs of abcbot.
#
# If the command like includes file names, we read them for the info;
# otherwise we read everything in the hst/ subdirectory.
#
#OPTIONS
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>

$| = 1;
($me = $0) =~ s".*/"";
require "Vopt.pm";
&Vopt($ENV{"V_$me"} || $ENV{"D_$me"} || $ENV{"T_$me"} || '1');

for $a (@ARGV) {
	if (($flg,$opt) = ($a =~ /^i([-+]+)(.*)/)) {
		print V "$me: Opt \"$flg\" \"$opt\"\n" if $V>2;
	} else {
		push @file, $a;
	}
}

unless (@file) {
	@file = `ls hst/*`;
}

print "  Tunes  Titles   Files Host\n";
file:
for $f (@file) {
	chomp $f;
	unless (open(F,$f)) {
		print V "$me: Can't read \"$f\" ($!)\n" if $V>0;
		next file;
	}
#	$lnks =
	$ndxs =
	$ttls =
	$fils = 0;
	$host = '';
	for $l (<F>) {
		if ($l =~ / T X:(\d+) T:(\d+) F:(\d+) H:(.*)\s*$/) {
			$ndxs = $1;
			$ttls = $2;
			$fils = $3;
			$host = $4;
			print V "$me: X: $ndxs T: $ttls F: $fils H: $host\n" if $V>1;
		} elsif ($l =~ / T L:(\d+) X:(\d+) T:(\d+) F:(\d+) H:(.*)\s*$/) {
#			$lnks = $1;
			$ndxs = $2;
			$ttls = $3;
			$fils = $4;
			$host = $5;
			print V "$me: X: $ndxs T: $ttls F: $fils H: $host\n" if $V>1;
		}
	}
	if ($ndxs>0 || $ttls>0) {
		printf "%7d %7d %7d %s\n", $ndxs, $ttls, $fils, $host;
#		$lnksum += $lnks;
		$ndxsum += $ndxs;
		$ttlsum += $ttls;
		$filsum += $fils;
	}
}
printf "%7d %7d %7d TOTAL\n", $ndxsum, $ttlsum, $filsum;
