#!/usr/local/bin/perl5

use Socket;
require "/home/j/josh/opt/lib/chat2.pl" || die "Hey! $!";

$|=1;

@ts=@ARGV;
$password="PASSWORD";


$SIG{'INT'}='handler';
$SIG{'TERM'}='handler';

sub handler { 
   print "Shuttin' down.."; 
   $stopnow=1; 
   $SIG{'INT'}='handler';
   $SIG{'TERM'}='handler';
}

foreach $termserver (@ts) {
    openpm($termserver);

    
    for $modem (0..29) {  
	if ($stopnow) { goto STOP; }
	$mtype=scanport($modem);
	$modem{$termserver}{$modem}=$mtype
    }

    closepm();

}

print "\n";
foreach $pm  (sort keys %modem) {
    foreach $port (0..29) {
	my($p)=sprintf ("S%d",$port);
	print "$pm/$p: $modem{$pm}{$port}\n";
    }
}
exit(0);

STOP:
print "Stopping..\n";
&chat::print("quit\n");
&chat::close($net);

sub closepm {
      &chat::print("quit\n");
      &chat::close($net);
  }

sub openpm {
    print "\n$termserver: ";
    $net=&chat::open_port("$termserver",23);
    if (! defined $net) { die "unable to open telnet connection to $termserver! ($!)"; }
    
    if (&chat::expect(5,"ogin:",1)) {
	&chat::print("!root\n");
    } else {
	die "no login: prompt.";
    }
    
    if (&chat::expect(5,"ord:",1)) {
	&chat::print("$password\n");
    } else {
	die "no password: prompt.";
    }
    $a=&chat::expect(5,">",1,"Invalid",2);
    if ($a==2) { die "Invalid password"; }
    if ($a==1) {
    } else {
	die "Couldn't get command prompt.";
    }
}

sub scanport {
    my($modem)=@_;
    print "S$modem..";
    &chat::print("attach S$modem\n");
    my($mtype)="";
    if (&chat::expect(5,
		      "Couldn't configure attach",0,
		      "Connected -",1)) {
	&chat::print("ATI3\r\n");
	$mtype=&chat::expect(5,
			     "Practical","practical",
			     "Sportster","sportster",
			     "V34_DS","cardinal",
			     "E.C.","intel",
			     "OK","unknown",
			     "ERROR","unknown",
			     "in:","sun_console",
			     "ord:","sun_console",
			     TIMEOUT,"no_modem");
	
    } else { return ("busy"); }
    &chat::print(sprintf("%c",29));
		
    if (&chat::expect(5,">",1)) {
	
    } else {
	die "Couldn't get back out of modem mode!";
    }
	    
    $mtype;
}


## Stuff from randall's GetWeather script.
sub listen {
    local($secs) = @_;
    local($return,$tmp) = "";
    while (length($tmp = &telnet_read($secs))) {
	print $tmp if $trace;
	$return .= $tmp;
    }
    $return;
}

sub listenr {
    local($secs,$regex) = @_;
    local($return,$tmp) = "";
    while (length($tmp = &telnet_read($secs))) {
	print $tmp if $trace;
	$return .= $tmp;
	last if $return =~ /$regex/i;
    }
    $return;
}

sub telnet_read {
    local($secs) = @_;
    &chat'expect($secs,
                '^\377[\375\376](.|\n)',
                q#&chat'print ("\377\374".$1); redo LOOP#,
                        # WON'T do these do/don't requests
                '^\377[\373\374](.|\n)', 'redo LOOP',
                        # ignore these will/won't changes
                '^\377\377', '"\377"',
                        # escaping the IAC
                '^\377(.|\n)', 'redo LOOP',
                        # ignoring these
                '^[^\377]+', '$&'
                        # return these
                );
}

