#!/usr/local/bin/perl5

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

$|=1;

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


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

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

while($doagain) {
    $doagain=0;

    foreach $termserver (@ts) {
	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.";
	}
	
	for $modem (0..29) {  
	    if ($stopnow) { goto STOP; }
	    if ( ! defined $modems{"$termserver S$modem"} ) {
	      print "S$modem..";
	      chat::print("attach S$modem\n");
		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");		   
		    
		    $modems{"$termserver S$modem"}=$mtype;
		    
		  chat::print(sprintf("%c",29));
		} else {
#		    print "$termserver S$modem is busy.\n";
		    $doagain++;
		}
		
		if (chat::expect(5,">",1)) {
		    
		} else {
		    die "Couldn't get back out of modem mode!";
		}
	        
                chat::print("show S$modem\n");
		listenr(5,"Modem Config") =~ /\n[^\n]+Modem Config: (\S+)\s+(\S+)[^\n]+\n/;
		$mtable{"$termserver S$modem"}="$1/$2";
	    }
	}

      chat::print("quit\n");
    
#print "Closing connection to $termserver\n";
      chat::close($net);
    }

foreach (sort keys %modems) {
    print "$_: $modems{$_} ($mtable{$_})\n";
}
    sleep 300;
    print "Trying again, $doagain modems left.\n" if ($doagain);
}
exit(0);

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

## 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
                );
}

