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

$ENV{'KRBTKFILE'} = "/usr/tmp/tkt_phone.$$";
print("Parent ($$) spawning child\n");
$pid = fork();
print("This process is $pid.  Pid $$\n");
if(!$pid){
    while(1){
	&zphone("Getting tickets.", "tickets");
	if(`/afs/net/tools/@sys/krbtgp -s -keyfile /etc/athena/srvtab phone-broadcast@SIPB.MIT.EDU` =~ /not found/)
	{
	    &zphone("Help me!!! No tickets!!!!\nkrbtgp doesn't like me.  How depressing.", "emergency");
	}
	&zphone("Getting tokens", "tickets");

	if(`aklog -cell sipb`)
	{
	    &zphone("I can't get tokens!  Wahhh. :-(", "emergency");
	}
	sleep(25500);
    }
}


sub fixterm {
    system("stty cooked");
    exit(0);
}

sub myfgets {
    local($fh) = @_;
    local($new);

    while ($buf{$fh} !~ s/^(.*)\n//) {
	sysread($fh,$new,1024);
	$buf{$fh} .= $new;
    }

    return($1);
}

sub cmd {
    local($txt) = @_;

    print PORT $txt,"\n";
    print ">>> ",$txt,"\n" if $verbose;
    &myfgets("PORT");		# echo
    while(1) {
	$resp = &myfgets("PORT");
	print "<<< ",$resp,"\n" if $verbose;
	if ($resp eq "ERROR") {
	    print PORT "ATS88?\n";
	    print ">>>>>> ATS88?\n" if $verbose;
	    &myfgets("PORT");	# echo
	    $resp = &myfgets("PORT");
	    print "<<<<<< ",$resp,"\n" if $verbose;
	    $resp = ($errorcode{$resp} || $resp);
	    print "cmd \"$txt\" failed: $resp\n";
	    &myfgets("PORT");	# blank line
	    &myfgets("PORT");	# OK
	    last;
	} elsif ($resp eq "") {
	    # do nothing
	} elsif ($resp ne "OK") {
	    # Assume it's response from an ISDN query
	    &unparseisdn($resp);
	} else {
	    last;
	}
    }
}

$buf = "";

sub dealwithisdn {
    local($new,$line,$key,$args,@args);

    sysread(PORT,$new,1024);
#Problem    It's waiting

    $buf .= $new;
#    print("***new***: $new\n");
#    print("***buf***: $buf\n");

    while($buf =~ s/^(.*)\n//) {
	$line = $1;
	print("**line***: $line\n") if $line;
	print "<<< ",$line,"\n" if $verbose;

	if($line =~ /DISPLAY:..,03\d\d\d\-\d\d\d\-\d\d\d\d/)
	{
#	    print("Incoming call!\n");
#	    print("$line\n");
	    $line =~ s/DISPLAY://;
	    $line =~ s/^\d\d//;
	    $inline = $&;
#	    print("Line: $&\n");
	    $line =~ s/^,03//;
	    $line =~ /\d\d\d\-\d\d\d\-\d\d\d\d/;
	    $num = $&;
#	    print("Number: $&\n");
	    &screen($inline, $num);
#	print("Completed call screen\n");
	}

#	&unparseisdn($line);
    }
#print("Current Buffer: $buf\n");
}


$port = shift(@ARGV) || die "No port specified.\n";

open(PORT,"$port") || die "Couldn't open port: $!\n";
select((select(PORT),$|=1)[0]); $|=1;

$verbose = 0;

$SIG{"INT"} = "fixterm";

## BSD-style
#system("stty cbreak -echo 9600 -even -odd > $port");
#system("stty cbreak -echo -even -odd");

## POSIX-style
system("stty min 1 time 0 -istrip igncr -icanon -iexten -even opost onlcr -echo 9600 cs8 -parenb > $port");
#system("stty min 1 time 0 -icrnl -inlcr -icanon -iexten -echo cs8 -parenb");

vec($rin,fileno(STDIN),1) = 1;
vec($rin,fileno(PORT),1) = 1;

# setup isdn
print("Setting up ISDN\n");

#&cmd("ATZ");			# reset phone
#&cmd("AT&&I");			# reset isdn
#&cmd("AT&&E1");			# enable extended codes
#&cmd("AT&O1");			# make channel 1 current
#&cmd("AT%A0=3");		# assign it to voice
#&cmd("AT&D3");			# follow DTR
print("Done with setup\n");
# monitor all

#&cmd("AT&&X0,1,0,1\n");		# CA/feature buttons
#&cmd("AT&&X0,2,0,1\n");		# Keypad buttons
#&cmd("AT&&X0,3,0,1\n");		# Fixed feature buttons
#&cmd("AT&&X0,4,0,1\n");		# Local buttons
#&cmd("AT&&X0,5,0,1\n");		# Switchhooks
#&cmd("AT&&X0,11,0,1\n");	# Display
#&cmd("AT&&X0,12,0,1\n");	# Lamps
#&cmd("AT&&X0,15,0,1\n");	# Tones
#&cmd("AT&&X0,16,0,1\n");	# Volume control
#&cmd("AT&&X0,17,0,1\n");	# Adjunct control

print "Go for it.\n";

while (1) {
#    select($rout=$rin,undef,undef,undef);
#    if (vec($rout,fileno(PORT),1)) {
#	print(">>>\n");
	&dealwithisdn();
#	print("<<<\n");
#    } else {
#	print ".";
#    }
}

sub zphone {
        local($message, $number) = @_;
        open (ZME, "|/afs/sipb/user/warlord/bin/zwrite -n -q -c 'sipb.phone' -i '$number' -s 'Incoming Call'");
        print(ZME $message);
        close(ZME);
}

sub screen {
    local($line, $number) = @_;
    $notify = "@b(Incoming call on line $line from)\n";
    open(PHONEBOOK, "/afs/sipb/user/mkgray/phone/list");
    while(<PHONEBOOK>){
	$_ =~ /^\S+/;
	$exp = $&;
#	print("Comparing [$number] to [$exp]\n");
	if($number =~ /$exp/) {
	    $_ =~/\s.+/;
	    $desc = $&;
	    $desc =~ s/^\s+//;
	    $notify .= $desc."\n";
	}
    }
#    print("Notification: $notify\n");
    if($notify eq "@b(Incoming call on line $line from)\n"){$notify .= "Unknown caller\n";}
	if($number =~ /617-225/){
    	&zphone($notify, "Dorm room x5");}
	elsif($number =~ /617-253/){
    	&zphone($notify, "MIT Office x3");}
	elsif($number =~ /617-258/){
    	&zphone($notify, "MIT Other x8");}
	else{&zphone($notify, "Non MIT Source: $number");}
}
