#! /usr/bin/perl

open(DUMP, "acua dump |") || die "couldn't open input pipe";
$min = -1;
while (<DUMP>) {
  ($login, $phNo, $priority, $tLeft, $tLimit, $credit, $sLimit,
   $bTx, $bTxMetric,
   $bRx, $bRxMetric,
   $bTxLimit, $bTxLimitMetric,
   $bRxLimit, $bRxLimitMetric,
   $bLimit, $bLimitMetric,
   $bStxLimit, $bStxLimitMetric,
   $bSrxLimit, $vSrxLimitMetric,
   $bSlimit, $bSlimitMetric,
   $expireAction, $expireTime) = split;
  $nUsers++;
  if ($bTxMetric eq "GB") {
    $bytes = $bTx * 1024**3;
  } elsif ($bTxMetric eq "MB") {
    $bytes = $bTx * 1024**2;
  } else {
    $bytes = $bTx * 1024;
  }
  if ($bRxMetric eq "GB") {
    $bytes += $bRx * 1024**3;
  } elsif ($bRxMetric eq "MB") {
    $bytes += $bRx * 1024**2;
  } else {
    $bytes += $bRx * 1024;
  }
  $bytes{$login} = $bytes;
  if ($min == -1) { $min = $max = $bytes; }
  elsif ($bytes - $min < 0) { $min = $bytes; }
  elsif ($bytes - $max > 0) { $max = $bytes; }
  $tot += $bytes;
}
close(DUMP);

foreach $f (sort { $bytes{$a} < $bytes{$b} } keys bytes) {
  printf "%-11s%s\n", $f . ":", &bytes2ASCII($bytes{$f});
}

if ($nUsers == 0) { exit 0; }
$avg = $tot / $nUsers;
printf "\nMinimum:   %s\n", &bytes2ASCII($min);
printf "Average:   %s\n", &bytes2ASCII($avg);
printf "Maximum:   %s\n", &bytes2ASCII($max);
printf "Total:     %s\n", &bytes2ASCII($tot);

sub bytes2ASCII {
  my $b = $_[0];
  if ($b - 1024**3 >= 0) {
    sprintf "%7.2f GB", $b / 1024**3;
  } elsif ($b - 1024**2 >= 0) {
    sprintf "%7.2f MB", $b / 1024**2;
  } else {
    sprintf "%7.2f KB", $b / 1024;
  }
}
