#!/usr/sww/bin/perl

# die if they didn't give us work to do
#
die "Please enter a list of files (or extensions) to report on!\n"
  if (@ARGV == 0);

# read all the files
#
for ($i = 0; $i < @ARGV; $i++) {

  # try to open the file
  #
  $file = $ARGV[$i];
  if (!open(DATA, $file)) {
    if (!open(DATA, 'longrun.'.$file)) {
      print STDERR "Couldn't read data file $file!\n";
      next;
    }

    # they only specified the extension
    #
    $file = 'longrun.'.$file;
  }

  # figure out what the array should be called
  #
  $file =~ /.*\.([^.]*$)/;
  $array = $1;
  push(@arraylist, $array);

  while (<DATA>) {
    if (/[ 0-9]*: Seed=[0-9.]*, Last=([0-9]*), Best=[0-9]*, Time=([0-9.]*)/) {
      ($last,$time) = ($1,$2);
      $cmd = '$' . $array . '[' . $last . ']++;';
      eval $cmd;
      $timetot[$last] += $time;
      $timecnt[$last]++;
    } else {
      print STDERR "Mismatch '$_'\n";
    }
  }
  close(DATA);
}

foreach $array (@arraylist) {
  for ($i = 0; $i < 50; $i++) {
    $cmd = '$' . $array . '[' . $i . '] += 0;';
    eval $cmd;
    $cmd = '$total{"' . $array . '"} += $' . $array . '[' . $i . '];';
    eval $cmd;
  }
  print "\t$array";
}
print "\t| Avg. Time  Avg. Gen. Time\n";

for ($i = 0; $i < 50; $i++) {
  printf "%2d:", $i;
  foreach $array (@arraylist) {
    $cmd = 'print "\t",$' . $array . '[' . $i . '];';
    eval $cmd;
  }
  if ($timecnt[$i] > 0) {
    $timeavg = $genavg = $timetot[$i]/$timecnt[$i];
    $genavg /= $i if ($i > 0);
    $gentot += $timetot[$i];
    $gencnt += $timecnt[$i] * $i;
  } else {
    $timeavg = $genavg = $timetot[$i];
  }
  if ($timetot[$i] > 0) {
    $timetot += $timetot[$i];
  }
  if ($timeavg > 0.0) {
    printf "\t| %9.4f    %9.4f\n",$timeavg, $genavg;
  } else {
    print "\t|\n";
  }
}

print "TOTALS";
foreach $array (@arraylist) {
  print "\t",$total{$array};
}
$timetot /= $timecnt if ($timecnt > 0);
$gentot /= $gencnt if ($gencnt > 0);
printf "\t| %9.2f    %9.4f\n",$timetot, $gentot;
print "\n";

exit 0;
