#! /usr/bin/perl

my ($zone, $net, $node, $point) = (2, 463, 68, 0);

$debug = 0;

if (@ARGV==0)
{	print "Usage: $0 <addr>\n";
	exit(0);
}
$addr = shift(@ARGV);
if ($addr =~ /^(?:(?:(?:(\d+):)?(\d+))?\/)?(\d+)(?:\.(\d+))?$/)
{	$zone = $1 if defined($1);
	$net = $2 if defined($2);
	$node = $3 if defined($3);
	$point = 0;
	$point = $4 if defined($4);
} elsif ($addr =~ /^\.(\d+)$/)
{	$point = $1;
} else
{	print "$addr is not correct FTN-addr!\n";
	exit(1);
}
$raddr = "$zone:$net/$node";
$raddr .= "\\.$point" if $point;
$raddr .= "\\\@";
$debug && print "\$raddr: \"$raddr\"\n";

while (<>)
{
	next unless /^. \d\d ... \d\d:\d\d:\d\d \[(\d+)] (.*)$/;
	($pid, $line) = ($1, $2);
	chomp($line);
$debug && printf "print %2d [%d] line: %s\n", $print{$pid}, $pid, $line;
	if ($line =~ /^rc\((\d+)\)=\d+$/)
	{	print $_ if $print{$1}==1;
		delete($print{$1});
		delete($start{$1});
		delete($wasspace{$1});
$debug && print "[$1] ended\n";
		$print{$pid} = 0;
		delete($start{$pid});
		next;
	}
	if ($line =~ /^done / && !$wasspace{$pid})
	{	print $_ if $print{$pid}==1;
		delete($print{$pid});
		delete($start{$pid});
		delete($wasspace{$pid});
$debug && print "[$pid] ended\n";
		next;
	}
	if ($line =~ /^started (?:client|server) #\d+, id=(\d+)$/)
	{	$print{$1} = -1;
		$start{$1} = $_;
		$wasspace{$1} = 1;
		$print{$pid} = 0;
		delete($start{$pid});
$debug && print "[$1] started\n";
		next;
	}
	if ($line =~ /^(call to|addr:) / && $line =~ /$raddr/)
	{	$print{$pid}=1;
$debug && print "[$1] our, print it\n";
		print $start{$pid};
		print $_;
		delete($start{$pid});
		next;
	}
	if (!defined($print{$pid}))
	{	$start{$pid} = "";
		$print{$pid} = -1;
$debug && print "[$pid] unknown\n";
	}
	$wasspace{$pid} = 1 if /^ /;
	if ($print{$pid} == 1)
	{	print $_;
	} elsif ($print{$pid} == -1)
	{	$print{$pid}=0 if $line =~ /^pwd protected session/;
		$print{$pid}=0 if $line =~ /^(sending|receiving|done) /;
		if ($print{$pid}==0)
		{	delete($start{$pid});
$debug && print "[$pid] not our, skip it\n";
		} else
		{	$start{$pid} .= $_;
$debug && print "[$pid] unknown, added to \$start\n";
		}
	}
}
