#!/usr/bin/perl

if ($ARGV[0]) {
	$skip = shift @ARGV;
}

while (<>) {
	chomp;
	s/test nomatch/nomatch/;
	@line = split(/\s+/);
	next if $line[1] eq 'resize';

	($name = $line[0]) =~ s/://;
	next if $name eq $skip;
	$name =~ s/_/ /g;

	# type: what n memory n time n	
	push(@{$result->{$line[1]}->{$name}->{$line[3]}}, $line[4]);
	push(@{$result->{$line[1]}->{$name}->{$line[5]}}, $line[6]);
}
	
foreach my $key (keys %$result) {
	# insert test nomatch readd del
	foreach my $what (qw(memory time)) {
		if ($what eq 'memory'
		    && ! grep($_ eq $key, qw(insert readd))) {
		    	next;
		}
		$title = '';
		$plot = '';
		$csv = '';
		foreach my $name (keys %{$result->{$key}}) {
			$title .= "'-' title \"$name\" with linespoints, ";
			$n = 1;
			$csv .= "$name";
			foreach (@{$result->{$key}->{$name}->{$what}}) {
				$plot .= 1000000*$n . " $_\n";
				$n++;
				$csv .= ", $_";
			}
			$plot .= "e\n";
			$csv .= "\n";
		}
		open(CSV, ">$key-$what.csv");
		print CSV $csv;
		close(CSV);
		open(PLOT, "|gnuplot");
		($pagetitle = $key) =~ s/^(.)/uc($1)/e;
		$pagetitle .= " $what";
		chop $title; chop $title;
		print PLOT <<TXT;
set terminal png small
set title "$pagetitle"
set ylabel "$what"
set xlabel "elements"
set output "$key-$what.png"
plot $title
$plot
TXT
		close(PLOT);
	}
}
  