#!/usr/bin/perl

sub get_file_data {
    my ($filename) = @_;


    use strict;
    use warnings;

    print $filename; 

    my @filedata=();
    
    unless(open(GET_FILE_DATA, $filename)) {
	print STDERR "Could not open file \"$filename\"\n\n";
	exit;
    }
    
    @filedata=<GET_FILE_DATA>;
    close(GET_FILE_DATA);

    return @filedata;
}

sub extract_from_file {
    
    my (@file_data) = @_;

    use strict;
    use warnings;

    my $data = '';
    
    foreach my $line (@file_data) {
	if($line=~m/^Forward/) {
	    $data.=$line;
	}
	elsif($line=~m/^Backward/) {
	    $data.=$line;
	}
	elsif($line=~m/^One-way/) {
	    $data.=$line;
	}
	elsif($line=~m/^Integral/) {
	    $data.=$line;
	}
	elsif($line=~m/^Relaxation/) {
	    $data.=$line;
	}
	else {
	    next;
	}
    }

    return $data;
}


my @file_data=();
my $data='';

@temps=("15", "25", "35");
@res_dir=("met", "eth", "pol");
@fraction=("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
@copy=("1","2","3");

foreach $temp (@temps) {
    chdir("bulk"."$temp");
    foreach $dir (@res_dir) {
	chdir("$dir");
	foreach $frac (@fraction) {
	    if($frac==0) {
		chdir("$frac");
	    }
	    else {
		chdir("$frac"."0");
	    }
	    foreach $cpy (@copy) {
		chdir("$cpy");
		@file_data=get_file_data(glob("HB-$dir-$temp-$frac-$cpy.o*"));
		$data.=extract_from_file(@file_data);
		chdir("..");
	    }
	    chdir("..");
	}
	chdir("..");
    }
    chdir("..");
}

my @fields=split(/\s+/,$data);

foreach (@fields) {
    print "$_\n";
}


exit;

