#!/usr/bin/perl -w

my $y_range = 156000000;

$script = <<END_SCRIPT;
set term postscript eps
set boxwidth 0.25 absolute
set style fill pattern 0 border -1
set format y "%8.0f"
set yrange [0:$y_range]
set ticscale 0 0
set offsets .1, .1, 0, 0
set bmargin 3
END_SCRIPT

#set yrange [0:87100000]

$max_branches = 0;
$cols_pre_graph = 7;

@ARGV = grep {!/curve|data|script|CVS|eps|process/} @ARGV;

foreach $file (@ARGV)
{
	%results = ();
	@branch_names = ();
	$filename = $file;
	$filename =~ s/counters\///;
	$base_filename = lc($filename);
	$base_filename =~ s/[ \-\(\)]/_/g;
#	print $base_filename."\n";

	$data_filename = "../data/counter_gnuplot_data/$base_filename\_data";
	$script_filename = "../data/counter_gnuplot_generated_scripts/$base_filename\_script";

	# get the branch names and values
	open(BASE, $file);
#	print $file."\n";
	@lines = <BASE>;
	@lines = grep {!/^\#/} @lines;
	while(1)
	{
		$line = shift(@lines); # sort name, branch name
		if (!defined($line)) { last; } # check for EOF
		$line =~ /\Q$filename\E - (.*):/;
		$branch_name = $1;
		push(@branch_names, $branch_name);

		$line = shift(@lines); # column titles - SKIP
		for($i = 0; $i < 5; $i++) # read all the counts
		{
			$line = shift(@lines);
			$line =~ /(\w+):\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)/;
			$this_result = $2 / 10.0;
			$results{$branch_name}{$1} = $this_result;
			if ($this_result > $max_branches)
			{
				$max_branches = $this_result;
			}

		}
	
		shift(@lines); # current state
		shift(@lines); # blank line
	}
	close(BASE);

	#write the gnuplot data file and script
	$file_count = 0;
	while(scalar @branch_names > 0) # dont start a new file if theres no more branches to graph
	{
		open(DATA, ">".$data_filename.$file_count);
		print DATA "#branch correct incorrect taken not_taken\n";
		$xtics = "set xtics (";
		$index = 0;
		for($i = 0; $i < $cols_pre_graph; $i++) # 6 lines per graph
		{
			$branch_name = shift @branch_names;
			if (!defined($branch_name))
			{
				print DATA "0 0 0 0 0\n";
				$xtics .= "\"\" $index, ";
			}
			else
			{
				if ($i % 2) { $xtics .= "\"\\n$branch_name\" $index, "; } # stagger the labels
				else { $xtics .= "\"$branch_name\" $index, "; }

				print DATA $results{$branch_name}{"branch_count"};
				print DATA " ".$results{$branch_name}{"correct_count"};
				print DATA " ".$results{$branch_name}{"incorrect_count"};
				print DATA " ".$results{$branch_name}{"taken_count"};
				print DATA " ".$results{$branch_name}{"not_taken_count"};
				print DATA "\n";
			}
			$index++;
		}
		close(DATA);

		# write the script file
		$script_output = $script;
		$script_output .= "set title \"$base_filename branch predictor results $file_count\"\n";
		$script_output .= "set output '../data/plots/counter_$base_filename\_$file_count.eps'\n";

		#add the xtics
		$script_output .= $xtics;

		# get rid of the extra ', '
		chop $script_output;
		chop $script_output;
		$script_output .= ")\nplot \\\n";

		$script_output .= "'$data_filename$file_count' using (\$0-.15):(\$2+\$3) title 'incorrect' with boxes lt -1,\\\n";
		$script_output .= "'$data_filename$file_count' using (\$0-.15):2 title 'correct' with boxes lt -1,\\\n";
		$script_output .= "'$data_filename$file_count' using (\$0+.15):(\$4+\$5) title 'not taken' with boxes lt -1,\\\n";
		$script_output .= "'$data_filename$file_count' using (\$0+.15):4 title 'taken' with boxes lt -1";

		open(SCRIPT, ">".$script_filename.$file_count);
		print SCRIPT $script_output;
		close(SCRIPT);

		$file_count++;
	}

}

printf "%20s $max_branches\n", "max branches is:";
printf "%20s $y_range\n", "y range is:";
