eval 'exec perl -I ./bin -S $0 ${1+"$@"}'		#-*-Perl-*-
	if 0;

require 5.001;

($progname) = ($0 =~ m!([^/]+)$!);
sub usage {
	die <<ENDUSAGE;
usage: $progname -s node1 [options] [trace file...]

get trace files that match certain criteria related to source, dest or flow-id

options:
	-o outfile	write subset trace to output file
	-b		bi-directional, i.e. gather lines 
	-d node2	specify destination
	-f flowid	specify flow id
ENDUSAGE
}

$usage = "usage: $progname [-b] [-o outfile] -s node1 [-d node2] [-f flowid] [trace files...]\n";

$opt_b = 0;
$opt_f = -1;
require 'getopts.pl';
(&Getopts('bo:s:d:f:') && $opt_s) || usage;

open(STDOUT, ">$opt_o") if ($opt_o);
while (<>) {
    /^v/ && do {
	print $_;
	next;
    };
    @F = split;
    if ($F[2] == $opt_s) {				# if src matches
	if (($opt_f != -1) && $F[7] != $opt_f) {
		next;
	}
	print $_ unless ($opt_d && $F[3] != $opt_d);	# print unless dst & !match
	next;
    }
    if ($opt_b && $F[3] == $opt_s) {			# else if bi && dst fld mch
	if (($opt_f != -1) && $F[7] != $opt_f) {
		next;
	}
	print $_ unless ($opt_d && $F[2] != $opt_d);	# prt unless dst & !mch sfld
	next;
    }
}
close(STDOUT);
exit;
