#! /usr/bin/perl

# $Id: 10runtime,v 1.1 2009-02-13 16:21:16 potyra Exp $
#
# Copyright (C) 2004-2009 FAUmachine Team <info@faumachine.org>.
# This program is free software. You can redistribute it and/or modify it
# under the terms of the GNU General Public License, either version 2 of
# the License, or (at your option) any later version. See COPYING.

use strict;
use Time::Local;

my $result_dir = $ARGV[1]."/";

opendir BASE, "$result_dir" or die "can't stat directory $result_dir\n";

my $logfile = "$result_dir"."log.faum-expect";

open LOG, "$logfile" or writetime(0);
	my $start = 0;
	my $end = 0;
	my $end2 = 0;

	while (my $line = <LOG> ) {
		if ( $start == 0) {
			$start = &gettimestamp($line);
		}
		if ( $line =~ /TIME_START/ ) {
			$start = &gettimestamp($line);
		}
		if ( $line =~ /TIME_END/ ) {
			$end = &gettimestamp($line);
		}
		$end2 = &gettimestamp($line);
	}

	if ( $end == 0 ) {
		$end = $end2;
	}

	writetime($end - $start);
close LOG;

sub writetime() {
	my $runtime = shift;

	open RUNTIME, "> ".$result_dir."runtime";
		print RUNTIME $runtime;
	close RUNTIME or die "Could not write ".$result_dir."runtime";

	exit 0;
}

sub gettimestamp() {
	my $line = shift;

	my @data = split ' ', $line;
	my ($year, $month, $day) = split '-', $data[1];
	my ($time, $fract) = split '\.', $data[2];
	my ($hour, $min, $sec) = split ':', $time;

	return &Time::Local::timelocal($sec, $min, $hour, $day, $month-1, $year);
}

