#!/usr/bin/perl
# usage: callsign cs1 [cs2 ...]
# author: eo <eo@qrq.com>

use strict;
use Socket;

# Configuration vars
# my $host = 'zone.oh7rba.ampr.org';  # callsign server (from 44. net)
my $host = 'blues.pspt.fi';  # callsign server from Internet (tnx NC7J)
my $port = 1235;              # port number

$| = 1;  # not that it matters

# make sure the user supplied at least
# one callsign to lookup
die "usage: $0 callsign [callsign ...]"
  unless @ARGV;

# resolve hostname
my $inet_addr = gethostbyname($host) ||
  die "Can't resolve $host: $!";

# build a sockaddr_in struct
my $packed_addr = sockaddr_in($port, $inet_addr);

# create the socket
socket(TO_SERVER, PF_INET,
       SOCK_STREAM, getprotobyname('tcp'))  ||
  die "unable to create socket: $!";


# connect to the server
connect(TO_SERVER, $packed_addr) ||
  die "Couldn't connect to $host:$port: $!";


# always bag the first arg
shift;          # see ya

# send it our list of callsigns on one line
# separated by spaces
my $args = join(" ", @ARGV, "\n");
syswrite(TO_SERVER, $args, length $args, 0);

# read the response in 4K chunks
# until EOF
my $buff = undef;
while(sysread(TO_SERVER, $buff, 4096, 0)) {
    $buff =~ s/\r//g;
    print $buff;
}

# bail
close(TO_SERVER) ||
  die "Error closing connection to $host: $!";

