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

require '/afs/athena/user/m/k/mkgray/perl/sgi-chat2.pl';

$SIG{'HUP'} = 'huphandler';
sub start_kermit {
    print("Starting kermit...\n\r") if $verbose;
    $fh = &chat'open_proc("/usr/athena/bin/kermit");
    print("Kermit started\n\r") if $verbose;
    $blah =&chat'expect(10, 'Kermit>', '');
    print("Setting line\n\r") if $verbose;
    &chat'print("set line /dev/term/a\n\r");
    $blah = &chat'expect(5, 'Kermit>', '$&');
    print $blah if $verbose;
    print("Setting speed\n\r") if $verbose;
    &chat'print("set speed 9600\n\r");
    $blah =  &chat'expect(5, 'Kermit>', '$&');
    printf $blah if $verbose;
    print("Connecting\n\r") if $verbose;
    &chat'print("c\n");
    $blah = &chat'expect(5, 'Kermit>', '$&');
    print $blah if $verbose;
}


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

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

    $new = &chat'expect(5, '.*\n', '$&');
    $buf{$fh} .= $new;
    $buf{$fh} =~ /^(.*)\n/;
    $buf{$fh} = "";
    print("Leaving fgets with \"$1\"\n\r") if $verbose;
    return($1) if $1;
    "";
}

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

    print $fh $txt."\n\r";
    select(undef, undef, undef, .5);
    print ">>> ",$txt,"\n\r" if $verbose;
    $got = &myfgets($f) if $kermit;
}

$buf = "";

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

    print("Waiting for something...\n\r") if $verbose;
    if($kermit){
	$new = &chat'expect(2, '.*\n', '$&');
    }
    else{
	$didread = read($sread, $new, 1);
	$rct++;
	if($rct > 100){
	    &cmd("");
	    $rct = 0;
	}
	if(!defined($didread)){
	    print "Error: $!\n";
	}
    }
    print("Got something \"$new\"\n\r") if $verbose;
#Problem    It's waiting

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

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

	if($line =~ /DISPLAY:..,03\d\d\d\-\d\d\d\-\d\d\d\d/)
	{
	    print("Incoming call!\n\r") if $verbose;
	    print("$line\n\r") if $verbose;
	    $line =~ s/DISPLAY://;
	    $line =~ s/^\d\d//;
	    $inline = $&;
	    print("Line: $&\n\r") if $verbose;
	    $line =~ s/^,03//;
	    $line =~ /\d\d\d\-\d\d\d\-\d\d\d\d/;
	    $num = $&;
	    print("Number: $&\n\r") if $verbose;
	    &screen($inline, $num);
	print("Completed call screen\n\r") if $verbose;
	}
	elsif($line=~ /DISPLAY:(..).*INCOMING/){
		$online = $1;
		&zphone("Incoming call on line $online\n", "Incoming call, outside");
	}
	elsif($line=~ /DISPLAY:(..).*PRIVATE/){
	    $online = $1;
	    &zphone("Incoming call on line $online\n", "Private number");
	}

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


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

select((select($fh),$|=1)[0]); select(STDOUT);$|=1;

$verbose = 1;
$kermit = 0;
$port = "/dev/ttyd2";

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

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

## POSIX-style

if($kermit){
    &start_kermit();
}
else{
    $fh = "SERIN";
    $sread = "SERREAD";
    open($fh, ">$port");
    open($sread, "$port");
}

if(!fork()){
    while(1){
	sleep(30*60);
	&reinit;
    }
}

#system("stty min 1 time 0 -icrnl -inlcr -icanon -iexten -echo cs8 -parenb");

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

# setup isdn
print("Setting up ISDN\n\r") if $verbose;

&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") if $verbose;
# 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" if $verbose;

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

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

sub screen {
    local($line, $number) = @_;
    $notify = "@b(Incoming call on line $line from)\n";
    if($number =~ /3\-7760/){
	&load_phonebook();
    }
    while(($exp, $desc) = each %phonebook){
	if($number =~ /$exp/){
	    $notify .= $desc."\n";
	    print("Notify:  \"$notify\"\n") if $verbose;
	}
    }

    print("Notification: $notify\n") if $verbose;
    if(!$notify){$notify = "Unknown caller\n";$number="unknown";}
	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, "Outside");}
}

sub load_phonebook {
    open(PHONEBOOK, "/afs/sipb/user/mkgray/phone-pub/list");
    print("Loading phonebook...\n") if $verbose;
    while(<PHONEBOOK>){
	$_ =~ /^\S+/;
	$exp = $&;
	$_ =~/\s.+/;
	$desc = $&;
	$desc =~ s/^\s+//;
	$phonebook{$exp}=$desc;
	}
}

sub reinit {
&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
}

sub huphandler {
    &reinit;
    &load_phonebook;
}
