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


$last = 0;
&zephyr_init;
while(<GETIN>){
    if(/::::/){
	$msg = "";
	($blank, $from, $message) = split(/:+/, $_);
	$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");
    $now = time();
    if($message =~ /SIS/){
	if(($now - $last)  > 300){
	    &send("There had been an extended delay of the SIS outage.  We hope that \nit will be working again in the next two days\n\nSorry for the inconvenience.\n");
	    $last = $now;
	}

    }

}				# 
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) = @_;	# 
    open(SENDOUT,"|zwrite -d -q -n -s '$Signature' -i help");
    print SENDOUT "$message\n";
    close(SENDOUT);
}
