#!/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 . ']++;';
      $total{$array}++;
      eval $cmd;
    } else {
      print STDERR "Mismatch '$_'\n";
    }
  }
  close(DATA);
}

open(GRAPH, "| xgraph") || die "Couldn't start 'xgraph'!\n";
print GRAPH "TitleText: % of runs completed\n";
foreach $array (@arraylist) {

  print GRAPH "\"$array\"\n";
  for ($i = 0; $i < 49; $i++) {
    $cmd = '$tmpval = ($' . $array . '[' . $i . '] / ' . $total{$array} . ') * 100;';
    eval $cmd;
    print GRAPH "$i $tmpval\n";
  }
  print GRAPH "\n";
}

exit 0;
