#!/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",
					TIMEOUT,"no_modem");
		    
		    $modems{"$termserver S$modem"}=$mtype;

		    if ($mtype eq "sportster") {
		    chat::print("ATI7\r\n");
		    $ati7=listenr(5,"OK"); # clear out previous ati3
		    $ati7=listenr(5,"OK");
                    foreach(split /[\n\r]/,$ati7) {
                       if (/EPROM date/) {
                           ($epromdate{"$termserver S$modem"})=/EPROM date\s+(\S+)$/;
		       }
                       if (/DSP date/) {
                           ($dspdate{"$termserver S$modem"})=/DSP date\s+(\S+)$/;
                       }
                       if (/EPROM rev/) {
                           ($epromrev{"$termserver S$modem"})=/EPROM rev\s+(\S+)$/;
                       }
                       if (/DSP rev/) {
                           ($dsprev{"$termserver S$modem"})=/DSP rev\s+(\S+)$/;
                       }
		    }	    
		  }
		  chat::print(sprintf("%c",29));
		    print "ok..";
		} else {
		    print "busy..";
		    $doagain++;
		}

  	         chat::print("\n");

		if (chat::expect(5,">",1)) {
		    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);
    }

    print "\n";
foreach (sort keys %modems) {
    print "$_: $modems{$_} ($mtable{$_}): date ($epromdate{$_}/$dspdate{$_}) rev ($epromrev{$_}/$dsprev{$_})\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
                );
}

