#!/usr/athena/bin/perl 


$ai = shift;
$md = shift;
$type = shift;
if ($type eq "RED") {
    $tlabel = "RED";
} elsif ($type eq "dt") {
    $tlabel = "DropTail";
} else {
    print "usage ai md RED|dt\n";
    exit 0;
}
print stderr "ai $ai md $md type $tlabel\n";
$f1 = "f1:$ai:$md:$type";
$f2 = "f2:$ai:$md:$type";

$f1drop = "f1d:$ai:$md:$type";
$f2drop = "f2d:$ai:$md:$type";
$gnufile = "gnu-$ai-$md-$type.gnu";

print stderr "running ns cc.tcl -ai $ai -md $md -queuetype $type\n";
system("ns cc.tcl -ai $ai -md $md -queuetype $type");
print stderr "splitting flows\n";
open(flow1,">$f1");
open(flow2,">$f2");
open(flow1drop,">$f1drop");
open(flow2drop,">$f2drop");
open(gnu,">$gnufile");
open(in, "out.tr");

$total1 = 0;
$total2 = 0;
while(<in>) {
    chop;
    ($action, $t, $s, $d, $type, $size, $flags, $flow, $ipsrc, $ipdst, $seq, $id) = split();
    if ($action eq "r") {
	if ($s == 2 && $d == 3) {
	    if ($flow == 1) {
		$total1++;
		print flow1 "$t $seq $total1\n";
	    } elsif ($flow == 2) {
		$total2++;
		print flow2 "$t $seq $total2\n";
	    }
	}
    }
    elsif ($action eq "d") {
	if ($s == 2 && $d == 3) {
	    if ($flow == 1) {
		print flow1drop "$t $seq\n";
	    } elsif ($flow == 2) {
		print flow2drop "$t $seq\n";
	    }
	}
    }

}
print gnu "#set term postcript default\n";
print gnu "set label 1 '$tlabel AI=$ai,MD=$md' at 0.5,2000\n";
print gnu "plot '$f1','$f2',\"< awk '{print \$1,\$3}' $f1\",\"< awk '{print \$1,\$3}' $f2\",'$f1drop','$f2drop' with dots\n";
print gnu "pause -1\n";
print "generated $gnufile\n";
