#!/usr/sww/bin/perl

# get list of files
#
if (@ARGV == 0) {
  @filelist = ('longrun.orig', 'longrun.old', 'longrun.solo');
} else {
  @filelist = @ARGV;
}

for ($i = 0; $i < @filelist; $i++) {

  # try to open the file
  #
  $file = $filelist[$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]*)/) {
      $cmd = '$' . $array . '[' . $3 . ']++;';
      eval $cmd;
    } else {
      print STDERR "Mismatch '$_'" if (! /^Initial seed = /);
    }
  }
  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 "\n";

for ($i = 0; $i < 50; $i++) {
  printf "%2d:", $i;
  foreach $array (@arraylist) {
    $cmd = 'print "\t",$' . $array . '[' . $i . '];';
    eval $cmd;
  }
  print "\n";
}

print "TOTALS";
foreach $array (@arraylist) {
  print "\t",$total{$array};
}
print "\n";

exit 0;
