#! /usr/bin/perl -w

if ($#ARGV != 0) {
print << "EOF";
$0 - convert timestat output files to a gnuplot input file

Usage: $0 orderfile < timestats-file > gnuplot-file

All values will be converted to seconds ( with fractions ).
Orderfile should be a file containing the filenames in the desired order
and can be found in the corresponding BALIBASE subdirs.

(c) Goetz Schwandtner 2002
EOF
    exit -1;
}

######################################################################
# read in order file
######################################################################
open( ORDER, "<$ARGV[0]");

# array containing all filenames read
@numtoname = ();
# hash for back conversion (filename -> number)
%nametonum= ();

$i= 0;

while ($line= <ORDER>) {
    chop ($line);
# if a line does not entrirely consist of whitepaces, it contains a filename
    if ($line !~ /^\s*$/) {
	$numtoname[$i]= $line;
	$nametonum{$line}= $i;
	$i++;
    }
}

# now read time values
# we only use the entry "real" which specifies the elapsed time, not
# user ("user") or system ("sys") time, since that is what is 
# most interesting for us

# entry gives the index in the numtoname field
$entrynum= -1;

# array to store the values, in order of "nametonum"
@times= ();

while ($line= <STDIN>) {
    chop($line);
    if ($line =~ /^real\s*(\d*)m(\d*\.\d*)s$/ ) {
	# get user time and store it in corresponding entry
	# the minutes are stored in $1, the seconds in $2
	die "Input file format error!" if $entrynum== -1;
	$times[$entrynum]= $1*60+$2;
    } elsif ($line =~ /^sys\s*(\d*)m(\d*\.\d*)s$/ ) {
	# ignore the lines with entry "sys ..."
    } elsif ($line =~ /^user\s*(\d*)m(\d*\.\d*)s$/ ) {
	# ignore the lines with entry "user ..."
    } elsif ($line =~ /^\s*$/) {
	# ignore whitespace only line
    } else {
	# now there must be a file name in $line, check it
	if (!defined($nametonum{$line})) {
	    die "Found undefined file name: $line";
	}
	$entrynum= $nametonum{$line};
    }
}

######################################################################
# print gnuplot output
######################################################################

for ($i= 0; $i < $#numtoname; $i++) {
    if ( defined $times[$i] ) {
	print $i." ".$times[$i]."\n";
    }
}
