#!/usr/bin/perl
#

for (@ARGV) {push (@directories, $_) }
if (!@directories) {
    push(@directories,".");
}

require "find.pl";

#$[ = 1;			# set array base to 1
#$, = ' ';		# set output field separator
#$\ = "\n";		# set output record separator
#$, = "\t";

$column = 0;
$inWebstoneStats = 0;

directory: for (@directories) {
    &find($_);
}

sub parsefile { 
    open(FILE, $_);
    $timestamp = `pwd`;
    line: while (<FILE>) {
      chop;	# strip line feed
      if ($inWebstoneStats) {
	  if (/^(.*):[ \t]*(\d*\.?\d*|\.\d?)[ \t]*(.*)/) {
	      if ($3) {
		$title[$column] = $1 . " (" . $3 . ")";
	      }
	      else { $title[$column] = $1 }
	      $data[$column] = $2;
	      $column++;
	  }
	  if (/^Thruput/) {
	      &printdata();
	      $inWebstoneStats = 0;
	      $column = 0;
	  }
      }
      if (/^WEBSTONE/) {
	  $inWebstoneStats = 1;
          $title[$column] = "Timestamp";
	  local(@junk) = split('/', $timestamp);
	  $timestamp = pop @junk;
	  chop $timestamp;
          $data[$column] = $timestamp;
	  #print "$timestamp\t";
	  #print "$inWebstoneStats \t$column \t$title[$column] \t$data[$column]\t";
          $column++;
	  #print "$column\n";
      }
  } # end line
}

sub printdata {
    #$\ = '';		# set output record separator
    if (!$printedTitles) {
	for (@title) {
	    #print "$title[$_]\t";
	    #chop;
	    print "$_\t";
        }
	$printedTitles = 1; 
        print "\n";
    }
    for (@data) {
	#print "$data[$_]\t";
	print "$_\t";
    }
    print "\n";
}

sub wanted {
    /run/ && &parsefile($_);
}
