eval 'exec perl -S $0 ${1+"$@"}'
    if 0;
#
# Contributed by Giao Nguyen, http://daedalus.cs.berkeley.edu/~gnguyen
#

sub Usage
{
    $0 =~ s/.*\/([^\/]+)$/$1/;
    print <<EOF;
Split ns trace file into files for individual connection
Usage:  $0 [options] files
options: -d   delim:  deliminator for spliting the data fields or columns
	 -c comment:  comment to print at the end along with total throughput
	 -f	:  force new file for each connection
	 -psize size of packet in bytes
	 -tt	trace type
	 -pt	packet type	(list)
	 -fid   flow id 	(list)
	 -ctt	trace type column
	 -cpt	packet type column
	 -ctime	time column
	 -csrc	source address column
	 -cdst	destination address column
EOF
    exit;
}

sub getopt
{
    local($val);
    &Usage if ($#ARGV < 0);
    while ($_ = $ARGV[0], /^-/) {
	shift @ARGV;
	if (/^-d$/) {
	    $delim = shift @ARGV;
	}
	elsif (/^-(pt|fid)$/) {
	    # concat new value to get list of values for a field in the trace
	    $val = shift @ARGV;
	    s/-(\w+)/\$$1/;
	    eval "$_ = $_ . '$val '";
	}
	elsif (/^-(tt|psize|c|ctt|cpt|ctime|csrc|cdst)$/) {
	    $val = shift @ARGV;
	    s/-(\w+)/\$$1/;
	    eval "$_ = '$val'";
	}
	elsif (/^-(f)$/) {
	    s/-(\w+)/$1/;
	    $opt{$_} = 1;
	}
	else {
	    last;
	}
    }
}


# default information on the trace
$delim = " ";			# deliminator between fields of the trace
$psize = 1000;			# packet size in bytes
$tt = '-';			# trace type
# Column numbers for each field in the trace
$ctt = 1;
$ctime = 2;
$cpt = 5;
$cfid = 8;
$csrc = 9;
$cdst = 10;
$cseq = 11;
# the file descriptor number to started with
$fdcount = 4;

&getopt;			# get the command line arguments

# Set a couple of defaults if not given at the command line
$pt = 'tcp'  if !$pt;
$fid = 0  if !$fid;
# decrement column number for Perl internal usage
$ctt--; $ctime--;
$cpt--; $cfid--; $csrc--; $cdst--; $cseq--;


# process the trace file
while (<>)
{
    chop;
    @col = split(/$delim/);
    $count{$col[0]}++;
    next if ($col[0] ne $tt);
    next if !grep(/$col[$cpt]/, $pt);
    next if !grep(/$col[$cfid]/, $fid);

    $src = $col[$csrc];
    $dst = $col[$cdst];
    $key = "$col[$cpt].$src.$dst";
    $time{$key} = $col[$ctime];
    $pktcnt{$key}++;
    $seqno{$key} = $col[$cseq];

    if ($opt{f}) {
	$fd = $opened{$key};
	if (! $fd) {
	    $opened{$key} = $fd = $fdcount++;
	    open($fd, ">$ARGV.$key");
	}
	printf $fd "%0.6f\t%d\n", $col[$ctime], $col[$cseq];
    }
}


# print the per-connection information to STDOUT
@keys = sort (keys %time);
foreach $key (@keys) {
    $t = $time{$key};
    $s = $seqno{$key};
    if ($s > $pktcnt{$key}) {
	$s = $pktcnt{$key};
    }
    $maxtime = $t  if ($t > $maxtime);
    $maxpkt += $s;
    printf("%s\t%0.6f\t%d\t%0.6f\t%0.6f\n",
	   $key, $t, $s, $s/$t, 0.008 * $psize * $s/$t);
}

# print summary information to STDERR
if ($maxtime > 0) {
    printf STDERR " %d\t%0.6f\t%s\t",
	$#keys+1, 0.008 * $psize * $maxpkt / $maxtime, $c;

    @keys = sort (keys %count);
    foreach $key (@keys) {
	print STDERR $key, $count{$key}, " ";
    }
    print STDERR "\n";
}
