#! /usr/bin/perl

open TMP, ">tmp";

chdir "./chains";
opendir DIR, ".";
@files = readdir DIR;
closedir DIR;

print "processing S:* files\n";

foreach $file (@files)
{
    if (-d $file)
    {
	;
    }
    elsif ($file =~ "S.")
    {
#	print "processing $file\n";
	
	open FILE, "<$file";

	$l = <FILE>;

	$l =~ /^L=\s*(.+?)d=\s*(.+?)f=\s*(.+?)m=\s*(.+?)p=\s*(.+?)a=\s*(.+?)$/;

	$L = $1;
	$d = $2;
	$f = $3;
	$m = $4;
	$p = $5;
	$a = $6;
	
#	print "$L $d $f $m $p $a\n";

	$N = $L*$d;

	print TMP "$N $d $f $a $L\n"; 

	# create a gnuplot file
	
	close FILE;
    }
}

close TMP;

chdir "..";

print "sorting results file\n";

# sort file by <N,d>
system "sort -n -k 1,1n -k 2,2n tmp > tmp2; mv tmp2 tmp";

# now generate plot file for use by gnuplot

print "generating plot file plot, gnuplot file onion.plt\n";

open TMP, "<tmp";
open PLOT, ">plot";
open PLT, ">onion.plt";

print PLT "unset key;\\\n";
print PLT "set xlabel \"fraction of malicious nodes\";\\\n";
print PLT "set ylabel \"source anonymity\";\\\n";
print PLT "set title \"Anonymity in Key Distribution\";\\\n";

print PLT "plot\\\n";

$N_ = 0;
$d_ = 0;

$index = 0;

while ($l = <TMP>)
{
    ($N, $d, $f, $a, $L) = split / +/, $l;

    if ($N != $N_ ||
	$d != $d_)
    {
	$d_ = $d;
	$N_ = $N;
	
	print PLOT "\n\n";
	print PLOT "# N=$N d=$d\n";

	if ($index)
	{
	    print PLT ",\\\n";	    
	}
	
	$lw = 1 + ($N * 1.0 / 400) * 5;

	print PLT "\"plot\" index $index with lines lw $lw";

	$index++; 
    }

    print PLOT "$f $a\n";
}

close TMP;
close PLOT;
close PLT;



open TMP, "<tmp";
open PLOT, ">plot1";
open PLT, ">onion1.plt";

print PLT "set key on;\\\n";
print PLT "set xlabel \"fraction of malicious nodes\";\\\n";
print PLT "set ylabel \"source anonymity\";\\\n";
print PLT "set title \"Anonymity in Key Distribution\";\\\n";

print PLT "plot\\\n";

$N_ = 0;
$d_ = 0;

$index = 0;

while ($l = <TMP>)
{
    ($N, $d, $f, $a, $L) = split / +/, $l;

    if ($N != 12 &&
	$N != 36 &&
	$N != 48)
    {
	next;
    }

    if ($N != $N_ ||
	$d != $d_)
    {
	if ($N != $N_)
	{
	    $lt++;
	    $lw = 0; 
	}

	if ($d != $d_)
	{
	    $lw++;
	}

	$d_ = $d;
	$N_ = $N;

	print PLOT "\n\n";
	print PLOT "# N=$N d=$d\n";

	if ($index)
	{
	    print PLT ",\\\n";	    
	}
	
	print PLT "\"plot1\" index $index with lines lt $lt lw $lw t \"N=$N d=$d\"";

	$index++; 
    }

    print PLOT "$f $a\n";
}

close TMP;
close PLOT;
close PLT;


open TMP, "<tmp";
open PLOT, ">plot2";
open PLT, ">onion2.plt";

print PLT "set key on;\\\n";
print PLT "set xlabel \"stages (L)\";\\\n";
print PLT "set ylabel \"source anonymity\";\\\n";
print PLT "set title \"Anonymity in Key Distribution\";\\\n";

print PLT "plot\\\n";
print PLT "\"plot2\" with lines t \"f=0.1 d=3\"\n";

while ($l = <TMP>)
{
    ($N, $d, $f, $a, $L) = split / +/, $l;

    if ($f != 0.1 ||
	$d != 3)
    {
	next;
    }

    print PLOT "$L $a\n";
}

close TMP;
close PLOT;
close PLT;

system "sort -n -k 1,1n plot2 > tmp2; mv tmp2 plot2";



# sort by <L,f,d>
system "sort -n -k 5,5n -k 3,3n -k 2,2n tmp > tmp2; mv tmp2 tmp";

open TMP, "<tmp";
open PLOT, ">plot3";
open PLT, ">onion3.plt";

print PLT "set key on;\\\n";
print PLT "set xlabel \"split factor (d)\";\\\n";
print PLT "set ylabel \"source anonymity\";\\\n";
print PLT "set title \"Anonymity in Key Distribution\";\\\n";

print PLT "plot\\\n";

$L_ = 0;
$f_ = 0;
$lt = 0;
$lw = 0;

$index = 0;

while ($l = <TMP>)
{
    ($N, $d, $f, $a, $L) = split / +/, $l;

    if ($L != 4 &&
	$L != 6 &&
	$L != 8)
    {
	next;
    }

    if ($f != 0.05 &&
	$f != 0.2 &&
	$f != 0.4)
    {
	next;
    }

    if ($L != $L_ ||
	$f != $f_)
    {
	if ($L != $L_)
	{
	    $lt++;
	    $lw = 0; 
	}

	if ($f != $f_)
	{
	    $lw++;
	}

	$f_ = $f;
	$L_ = $L;

	print PLOT "\n\n";
	print PLOT "# L=$L f=$f\n";

	if ($index)
	{
	    print PLT ",\\\n";	    
	}
	
	print PLT "\"plot3\" index $index with lines lt $lt lw $lw t \"L=$L f=$f\"";

	$index++; 
    }

    print PLOT "$d $a\n";
}

close TMP;
close PLOT;
close PLT;


system "rm -f tmp";
