#!/usr/bin/perl
use strict;

die "\ngetwhat.pl [d|t] signaure\n" if ($#ARGV < 1);

my $cmd = $ARGV[0];
my $sig = $ARGV[1];
my ($in, $had_stream, $start) = (0, 0, 0);
my $node;

chomp($node = `hostname`);

while (<STDIN>) {
    if ($cmd eq "t") {
	$had_stream = 1;
	if (/(.+) : begin -.* sig = (.+)$/) {
	    if ($2 eq $sig) {
		$in = 1;
		$node = $1;
	    } else {
		$in = 0;
	    }
	}
	if ($in) {
	    if (/, ([0-9.]+) Kbps/) {
		print "$node : $1 Kbps\n";
		exit;
	    }
	}
    } elsif ($cmd eq "d") {
	if (/\[GetHostAndUrl\] sig = (.+), begin/) {
	    if ($sig eq $1) {
		$start = 1;
	    } elsif ($start == 1 && !($sig eq $1)) {
		exit;
	    } else {
		$start = 0;
	    }
	}
	if ($start) {
	    print;
	    exit if (/done/);
	    exit if (/gave up/);
	    exit if (/failed getting/);
	}
    }
}

if ($cmd eq "t") {
    if (!$had_stream) {
	print "$node : file not exist\n";
    } else {
	if ($in) {
	    print "$node : crashsed\n";
	} else {
	    print "$node : no-record\n";
	}
    }
}
