#!/usr/bin/perl -w
use Getopt::Std;

use warnings;

my %options=();
getopts("ahso",\%options);

if (defined($options{"h"}) || (scalar(@ARGV) == 0))
{
	die("simple_convert [options] filename\noptions are:\n\t-h - this help message\n-s - scale numeric results by the number of keys \n-o - offset everything from do_nothing\n -a use files ending in number and average them\n");
}

$scale = 0;
if (defined($options{s})) { $scale = 1; } 

$offset = 0;
if (defined($options{o})) { $offset = 1; } 

my $average = 0;
if (defined($options{a})) { $average = 1; }

opendir(DIR, "../data/simple_logs/") || die("cant opendir");
my @nothing_files = grep { /do_nothing_random\.org/ } readdir(DIR);
closedir(DIR);

if ($offset) # read in the offset results
{
	for(my $i = 0; $i < @nothing_files; $i++)
	{
		my $nothing_file = $nothing_files[$i];
		$argument = "../data/simple_logs/$nothing_file";
		unless (-e $argument) { die("file '$argument' does not exist\n"); }

		#get the actual filename
		$filename = "do_nothing";

		# parse the log
		open(LOG, $argument);
		@lines = <LOG>;
		chomp @lines;
		close(LOG);

		# skip date
		shift(@lines);

		# skip "run as" and line of dashes
		shift(@lines);
		shift(@lines);

		@simulators = ();
		while(@lines)
		{

			# find out all the sizes and the simulator name
			$simulator_sizes = shift(@lines);
			if ($simulator_sizes eq "Command list: ") { last;} # the end
			$simulator_sizes =~ /(\S+)\s+(.*)/;
			$simulator = $1;
			push @simulators, $simulator;

			@sizes = split(/\|/, $2);
			foreach $size (@sizes) {$size =~ s/\s//g;} # remove whitespace
			%seen = ();
			@sizes = grep { ! $seen{$_} ++ } @sizes; # uniq it

			# get the parameters
			@local_params = ();
			while($_ = shift(@lines))
			{
				if ($_ =~ /\-\-/) {last;}

				push @local_params, $_;
			}
			@{$params{$simulator}} = @local_params;

			# get the results
			@local_keywords = ();
			while($_ = shift(@lines))
			{
				if ($_ =~ /\-\-/) {last;} # seperator is ------------- etc
				$_ =~ /(\S+)\s+(.*)/;
				$keyword = $1;
				push @local_keywords, $keyword; # save it for later
				@local_results = split(/\|/, $2);

				# now put them into the big associative array
				foreach $n (@sizes)
				{
					foreach $param (@local_params)
					{
						$result = shift(@local_results);
						if (defined($result))
						{
							$result =~ s/\s//g; # remove whitespace
							unless ($result =~ /\D/)
							{
# TODO average this
# TODO add them all.
								$nothing{$simulator}{$param}{$n}{$keyword} += $result;
								if ($i == @nothing_files - 1) # last iteration, using @nothing_files in scalar context
								{
									$nothing{$simulator}{$param}{$n}{$keyword} /= @nothing_files;
#									print "nothing{$simulator}{$param}{$n}{$keyword} = $nothing{$simulator}{$param}{$n}{$keyword}\n";
								}
							}
						}
					}
				}
			}
			@{$keywords{$simulator}} = @local_keywords;
		}
	}
}

# start to actually do thing
foreach $argument (@ARGV)
{
	unless (-e $argument) { die("file '$argument' does not exist\n"); }
	chop $argument; # take the 0 at the end off it

	#get the actual filename
	@path = split(/\//, $argument);
	$filename = pop(@path);
	#HACK  TODO
	$filename =~ s/_random.org//g;

	# make a directory for the logs
	mkdir "../data/converted_simple_logs/$filename";

	my $i = 0; # which file
	while(1)
	{
		# parse the log
#		print "opening $argument$i\n";
		open(LOG, $argument."$i") or last;
#		print "opened\n";
		@lines = <LOG>;
		chomp @lines;
		close(LOG);

		# skip date
		shift(@lines);

		# skip "run as" and line of dashes
		shift(@lines);
		shift(@lines);

		@simulators = ();
		while(@lines)
		{

			# find out all the sizes and the simulator name
			$simulator_sizes = shift(@lines);
			if ($simulator_sizes eq "Command list: ") { last;} # the end
			$simulator_sizes =~ /(\S+)\s+(.*)/;
			$simulator = $1;
			push @simulators, $simulator;

			@sizes = split(/\|/, $2);
			foreach $size (@sizes) {$size =~ s/\s//g;} # remove whitespace
			%seen = ();
			@sizes = grep { ! $seen{$_} ++ } @sizes; # uniq it

			# get the parameters
			@local_params = ();
			while($_ = shift(@lines))
			{
				if ($_ =~ /\-\-/) {last;}

				push @local_params, $_;
			}
			@{$params{$simulator}} = @local_params;

			# get the results
			@local_keywords = ();
			while($_ = shift(@lines))
			{
				if ($_ =~ /\-\-/) {last;} # seperator is ------------- etc
				$_ =~ /(\S+)\s+(.*)/;
				$keyword = $1;
				push @local_keywords, $keyword; # save it for later
				@local_results = split(/\|/, $2);

				# now put them into the big associative array
				foreach $n (@sizes)
				{
					foreach $param (@local_params)
					{
						$result = shift(@local_results);
						if (defined($result))
						{
							$result =~ s/\s//g; # remove whitespace
							if (!($result eq "xxxxxxxxxxxxx"))
							{
	#							print "results{$simulator}{$param}{$n}{$keyword}{$i} = $result\n";
								$results{$simulator}{$param}{$n}{$keyword}{$i} = $result;
							}
						}
					}
				}
			}
			@{$keywords{$simulator}} = @local_keywords;
		}
		$i++;
	}
	my $tmp;
	foreach $simulator (@simulators)
	{
#		print "simulator = $simulator\n";
		foreach $param (@{$params{$simulator}})
		{
#			print "param = $param\n";
			$comment = sprintf("%19s ", "#set_size_in_keys");
			$output = "";
			foreach $n (@sizes)
			{
#				print "n = $n\n";
#				$output .= "$n ";
				$output .= sprintf("%19s ", $n);
				foreach $keyword (@{$keywords{$simulator}})
				{
#					print "keyword = $keyword\n";
					if ($average)
					{
						$result = 0;
						for($i = 0; $i <= 10; $i++)
						{
							$tmp = $results{$simulator}{$param}{$n}{$keyword}{$i};
#							print "\$tmp = $tmp, \$i = $i, \$result = $result\n";
							if (defined($tmp)) 
							{
								if (!($tmp =~ m/\D/))
								{
#									print "adding $tmp to $result\n";
									$result += $tmp;
								}
								else # its not numeric data
								{
									$result = $tmp;
									last;
								}
							}
							else # its over
							{
								if ($i == 0)
								{
									$result = undef;
								}
								else
								{
									$result /= $i;
#									print "after dividing by $i, getting $result\n";
								}
								last;
							}
						}
					}
					else
					{
						$result += $results{$simulator}{$param}{$n}{$keyword}{0};
					}
					if (defined($result))
					{
						# check if the result (before division) is a number 
						my $number = (!($results{$simulator}{$param}{$n}{$keyword}{0} =~ m/\D/));
						if ($n == $sizes[0]) # only do once
						{
#							$comment .= " $keyword";
							$comment .= sprintf("%36s ", $keyword);
						}
						if ($offset && $number && !($argument =~ /do_nothing/))
						{
							#print "BAH" unless defined ($nothing{$simulator}{$param}{$n}{$keyword});
							$result -= $nothing{$simulator}{$param}{$n}{$keyword};
							#print "subtracting an offset of $nothing{$simulator}{$param}{$n}{$keyword} to get $result\n";
						}
						if ($scale && $number)
						{
#							$output .= ($result/$n)." ";
#							print "scaling $result by $n and getting ".($result/$n)."\n";
							$output .= sprintf("%36f ", $result/$n);

						}
						else
						{
#							$output .= "$result ";
							$output .= sprintf("%36s ", $result);
#							print "outputting $result\n";
						}

					}
				}
				$output .= "\n";
			}
			$param =~ s/\ /_/g;
			$fullname = "$filename\_$simulator\_$param";
			open(OUT, "> ../data/converted_simple_logs/$filename/$fullname");
			print OUT $comment."\n";
			print OUT $output;
			close(OUT);
		}
	}
}
