#!/usr/bin/perl -w 

use strict;

my $dump_file="/var/spam/userflux/userflux_DB.txt";
my %total;

#------------------------------------------------------------------------------
sub load_table()
{
  print "LOADING TABLE ";
  if (not open (FIDUMP,"<$dump_file") ) {
    print "*** Can't read $dump_file. !!!\n";
    }
  else {
    my $gcount=0;   my $rcount=0;
    while (<FIDUMP>) {
      chomp;
      my ($rtype,$key,$val)=split "\t";
      if ($rtype eq 'G') { 
        my ($tpuntero,$ipuntero,@circulo)=split /\|/,$val;
        my $total=0;
        for my $n (@circulo) { $total += $n; }
        $total{$key}=$total;
        $gcount++; 
        }
      if ($rtype eq 'N') { print "[$val $key] "; }
      }
    print "readed $gcount from entries\n";
    close FIDUMP;
    }
}
#------------------------------------------------------------------------------
sub byval {
  $total{$a} <=> $total{$b};
  }
  
#------------------------------------------------------------------------------

load_table();

my $grantotal=0;
for my $key (sort byval (keys %total) ) { 
  printf "%5d $key\n",$total{$key}; 
  $grantotal+=$total{$key};
  }
print "$grantotal TOTAL MESSAGES\n";
