#!/afs/athena/contrib/perl/perl
# usage:  snoop -x42 -i <file> | ./filter.pl | more

$printing = 1;

$src_tag = 'A';

while (<>) {
    if (/^[\s\d]{2}\d{1,2}\s/) {		# block header line
	if ($waiting_for_a_body) {
	    &unroll(0);
	}
	($z, $packet, $time, $source, $arrow, $dest, $type, $stuff)
	    = split(/\s+/," $_", 8);
	unless ($src{$source}) {
	    $src{$source} = $src_tag;
	    $src_tag++;
	}
	if ($type ne "UDP") {
	    $nonUDP .= sprintf("%03d: [%1s] type $type [$stuff]\n",
			       $packet, $src{$source});
	}
	$waiting_for_a_body=1;
    } elsif (/^\s*$/) {				# empty line
	if ($body) {
	    if ($seen{$source.$body}) {
		if ($seen{$source.$body} != $dejavu) {
		    &unroll(-1);
		    $roll=$packet;
		}
		$dejavu = $seen{$source.$body};
	    } else {
		$seen{$source.$body} = $packet;
		&unroll(-1);  print "\n" if ($unrolled && $printing);
		$label = sprintf("%03d: [%1s]", $packet, $src{$source});
		$pbody=$body;
		substr($pbody,$[,length($label)) = $label;
		print $pbody,"\n" if $printing;
		$unrolled = 0;
	    }
	}
	$body = "";
    } else {					# block body line
	$waiting_for_a_body=0;
	s/\t/        /g;
	$body .= $_;
    }
}

print "\nNON-UDP PACKETS:\n$nonUDP" if $nonUDP;

print "\nHOSTS:\n";
while (($name, $tag) = each %src) {
    print "  [$tag]  $name\n";
}

# deal with "rolled up" lists of bodies
sub unroll {
    if ($roll) {
	local($p) = $packet+@_[0];
	if ($roll == $p) {
	    printf "%03d == %03d\n", $roll, $dejavu;
	} else {
	    printf "%03d..%03d == %03d\n", $roll, $p, $dejavu;
	}
	undef $roll, $dejavu;
	$unrolled = 1;
    }
}




