#!/usr/local/bin/perl
# written by Mark Eichin
# $Id: callsign.pl,v 1.3 93/12/31 09:12:51 bert Exp $

# in progress.
# based on geo.pl

require 'chat2.pl';

$query = "N1DPU"; # $query=$ENV{'QUERY_STRING'};
$|=1;

print("Content-Type: text/html\n\n");
$query =join(' ', @ARGV) if $ARGV[0];
print("\n");
&do_callsign($query);

sub listen {
        local($secs) = @_;
        local($return,$tmp) = "";
        while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
                print $tmp if $trace;
                $return .= $tmp;
        }
	$return;
}
# Call-Sign: N1DPU                    Class: TECHNICIAN
# Previously: KA1MRJ                  Class: NOVICE
# Real Name: MARK W EICHIN            Birthday: FEB 7, 1967
# Mailing Address: 15 POLARIS DR, NEW MILFORD, CT  06776
# Station Address: SAME AS MAILING ADDRESS
# Valid From: JUL 12, 1985            To: JUL 12, 1995
# Records Last Processed: JUL 12, 1985

sub do_callsign {
    local($request)=@_;
    $request =~ s/\+/ /g;

    if(!$request){
	 print "<isindex><h1>Enter the amateur radio callsign you wish to look up</h1>";
	 print "e.g. \"N1DPU\" <p>";
	 print "This server contains mostly information on locations within the US\n";
	 print "and is provided by the University of Buffalo.<p>";
	 print "Gateway written by <A HREF=\"/people/eichin/mwe.html\">Mark Eichin</A>\n";
	 return;
    }
print "<isindex>";

&chat'open_port('callsign.cs.buffalo.edu', 2000) || die "open: $!";
$_=&listen(2);
# particularly bogus possible behaviour. 
## < 0x0   fffb01fffb03
## RCVD will ECHO
## SENT do ECHO
## RCVD will SUPPRESS GO AHEAD
## SENT do SUPPRESS GO AHEAD
## > 0x0   fffd01fffd03

if($_ eq "\377\373\001\377\373\003") {
    &chat'print("\377\375\001\377\375\003");
}

while(!/Callbook/){
$_=&listen(5);
}
print("<h1>Amateur License Callbook Query:  \"$request\"</h1><pre>\n");

if($request =~ /^info$/i) {
    &chat'print("more 0\n\r");
    &chat'print("info\n\r");
} else {
    &chat'print("call $request\n\r");
}
$_=&listen(3);
split(/\n/);
shift; # skip the echo'ed query
foreach $dataline (@_){
	chop($dataline);
	print("$dataline\n") if ($dataline ne ">>");
}
&chat'print("quit\n\r");
print "</pre>";
}
