#!/afs/athena/contrib/perl/perl


$*=1;
&zephyr_init;
while(<GETIN>){
    if(/::::/){
	$msg = "";
	($blank, $from, @message) = split(/:+/, $_);
	$message = join(":", @message);
	$msg .= $message;
	while(<GETIN>){
	    if(/;;;;/){&parsemessage($msg,$from);last;}
	    $msg.=$_;
	    print($msg);
	}
    }
}

sub parsemessage {
    local($message, $from) = @_;
    print("Got a message from $from: $message\n");
    return if ((time()-$last) < 10);

    eval(`cat /afs/sipb/user/mkgray/bin/helpd.eval`);
}				# 
sub whats {
    local($acron, $from) = @_;
    print("Doing a whats lookup\n");
    $resp = `whats $acron`;
    if($resp){
	&send("$from -> $acron stands for\n$resp");
    }
}

sub use_latex {
    local($name)= @_;
    &send("$name ->  You should use LaTeX.\n");
}

sub zephyr_init {

open(GETIN, "zwgc -ttymode -f .desc -subfile .subs |")
        || die "Failed to start zwgc";

print "Zwgc started.";
# wait for zwgc to get going

$SIG{'INT'} = 'sighandler';
$SIG{'HUP'} = 'sighandler';
$SIG{'QUIT'} = 'sighandler';

$Signature = 'Perl Help Demon';

open(SENDOUT,"|zwrite -d -q -n -s '$Signature' -c help.test");
print SENDOUT "\nThe Perl Help Demon is\nIN\n";
close(SENDOUT);
}

sub sighandler {
        exit(0);
}

sub send {
				# 
    local($message) = @_;	# 
    $last = time();
    open(SENDOUT,"|zwrite -d -q -n -s '$Signature' -c help.test");
    print SENDOUT "$message\n";
    close(SENDOUT);
}
