#!/usr/bin/perl

# Program : tuned
# Author  : D. Faber (dirk-jan@flits102-126.flits.rug.nl)
# Date    : 24 june 1998
# Usage   : tuned host port [html]
# Example : tuned flits102-126.flits.rug.nl 8888 html 
# tuned is connecting to a NrServer and is showing which
# clients are currently conneted to the server and the 
# frequentie they are tuned in to.
# When given the 'html' argument it will generate a table
# in html. You can use it in an shtml program, using the code:
# <!--#exec cmd="tuned 129.125.102.126 8888 html"-->

if (@ARGV < 2) {
  die "usage: tuned hostname port [html]";
}

$hostname = $ARGV[0];
$port     = $ARGV[1];

use Net::Telnet ();

$NetStreamer = new Net::Telnet (Telnetmode => 0);
$NetStreamer->open(Host => $hostname,
                   Port => $port);

## Read the server information
$server = $NetStreamer->getline;

## Read the transmitting channels
$NetStreamer->print("list_receiver\n\n");
($_) = $NetStreamer->waitfor('/bad_command/i');

@info = split(/\n/, $_);

if ($ARGV[2] eq "html") {
print <<HEAD;
<TABLE WIDTH=100%>
<TR><TH>Host</TH>
    <TH>Port</TH>
    <TH>Server</TH>
</TR>
<TR><TD>$hostname</TD>
    <TD>$port</TD>
    <TD>$server</TD>
</TR>
<TR><TH>Frequentie</TH>
    <TH>Ip-adres</TH>
</TR>
HEAD

for ($i = 0; $i <= $#info; $i++) {
  ($_, $_, $ip, $freq) = split(/ /, $info[$i]);
  ($ip, $_) = split(/:/,$ip);
print <<CHANNELINFO
<TR><TD>$freq</TD>
    <TD>$ip</TD>
</TR>
CHANNELINFO
}

print <<INFOFOOT;
</TABLE>
INFOFOOT
} else {
print <<HEAD;
 Host   : $hostname
 Port   : $port
 Server : $server
+------+----------------------+
| Freq | Ip-adres             |
+------+----------------------+
HEAD
for ($i = 0; $i <= $#info; $i++) {
  ($_, $_, $ip, $freq) = split(/ /, $info[$i]);
  ($ip, $_) = split(/:/,$ip);
printf "| %4s | %-20s |\n", $freq,$ip;
}

print "+------+----------------------+\n";
}
