#!/usr/bin/perl

my $hptlog = '/home/fido/logs/hpt.log';

$ENV{TMP} =~ /([\\\/])/;
my $slash = $1;
my $stattpl = $ENV{TMP} . $slash . 'hptstat.tpl';
my ( $line, $msgs, $key, $i );

if ( open( FL, "<$hptlog" ) ) {
    print "Reading $hptlog\n";
    $i = 0;
    while( $line = <FL> ) {
	$i++;
	next unless $line =~ /^\d \d\d:\d\d:\d\d  echo area ([^ ]+) - (\d+) msgs/;
	$msgs{$1} += $2;
    }
    close(FL);
    print "$i lines read.";
} else { print STDERR "Can't open $hptlog\n"; }


if (open(FS, ">$stattpl")){
    print "Writing $stattpl\n";
    print( FS "\x01CHRS 866 2\n\n " . sprintf(" %-40s Messages\n---------------------------------------------------\n",'Area') );
    foreach $key ( sort { $msgs{$b} <=> $msgs{$a} } keys %msgs ) {
        print( FS sprintf(" %-40s %5s\n",$key,$msgs{$key}) );
    }
    print( FS "\n");
    close(FS);
} else { print STDERR "Can't open $stattpl\n"; }

system("/usr/bin/hpt -c /home/fido/etc/hpt.cfg post -s \"EchoStat\" -e \"CRIMEA.ROBOTS\" -z \"Evil Robot\" -o \"Lame Users Breeding, Crimea.\" -d -x -f loc,npd $stattpl");
