#! /usr/bin/perl -w

# this converter should be used on the timestats created by running the
# hmm programs for alignment benchmark

while ($line=<>) {
    if ($line =~ /^hmmt:/) { # found mark for the timestats for the first prg
	# reset the values
	$realmin= 0;
	$realsec= 0;
	$usermin= 0;
	$usersec= 0;
	$sysmin=  0;
	$syssec=  0;
	# "bool" variable to check whether "hmma:" label was already parsed
	$hmmafound= 0;
    } elsif ($line =~ /^hmma:/) { # found second mark, now prepare for output
	$hmmafound= 1;
	
	# several regexps for parsing the different time summaries
    } elsif ($line =~ /^real\s*(\d*)m(\d*\.\d*)s/ ) {
	$realmin+= $1;
	$realsec+= $2;
	# now handle carry-over:
	$carry= int($realsec/60);
	$realmin+= $carry;
	$realsec-= $carry*60;
    } elsif ($line =~ /^user\s*(\d*)m(\d*\.\d*)s/ ) {
	$usermin+= $1;
	$usersec+= $2;
	# now handle carry-over:
	$carry= int($usersec/60);
	$usermin+= $carry;
	$usersec-= $carry*60;
    } elsif ($line =~ /^sys\s*(\d*)m(\d*\.\d*)s/ ) {
	$sysmin+= $1;
	$syssec+= $2;
	# now handle carry-over:
	$carry= int($syssec/60);
	$sysmin+= $carry;
	$syssec-= $carry*60;
	# sys time summary is the last one, so if second mark was already
	# read, write new summary to file:
	if ($hmmafound == 1) {
	    printf( "real	%dm%.3fs\n",$realmin,$realsec);
	    printf( "user	%dm%.3fs\n",$usermin,$usersec);
	    printf( "sys 	%dm%.3fs\n",$sysmin,$syssec);
	}
    } elsif ( $line =~ /msf/) {
	# we have to remove one newline created by the calls of time in the
	# crali_hmmer script, so remove the one following the filename:
	chop($line);
	print $line;
    } else {
	# simply print all other lines:
	print $line;	
    }
}
