#!/usr/bin/perl -I/opt/SUNWstade/lib 

use PDM::ConfigFile;
use Getopt::Std;
use Net::Telnet;
use MIME::Base64;
use Util;

sub usage {
  print <<EOF;

  Usage: changeArrayPasswords  -o [oldpassword] -n [newpassword] -f 
             -o [oldpassword]: optional, use saved password if omitted 
             -n [newpass]    : optional, use saved password if omitted
             -n BLANK        : will clear the password
             -o BLANK        : will use a blank old password
  Example: 
	 changeArrayPasswords -o old -n new    : change array password from old to new
         changeArrayPasswords -n new           : Use saved old password and change to new
         changeArrayPasswords -o old           : Change array with old password to saved value
         changeArrayPasswords -o BLANK         : Change blank array(s) password to saved value

EOF
}

if (!getopts("o:n:?h", \%opts) || $opts{'?'} || $opts{h}) {
    &usage();
    exit();
}

System->set_home("/opt/SUNWstade");

my $old = $opts{o};
my $new = $opts{n};

if (!$old && !$new) {
    &usage();
    exit();
}
if (!$old) {
    $old = System->getPassword("array");
    print "Old: Using saved password for old value!\n" if ($old);
} elsif ($old eq "BLANK") {
    $old = "";
    print "Old: Using blank password for old value!\n" if ($old);
}
if ($new eq "BLANK") {
    $new = "";
    print "Clearing the array(s) password!\n";
} elsif (!$new) {
    $new = System->getPassword("array");
    print "New: Using saved password for new value!\n" if ($new);
}

my($renv, $devs, $hosts,$notifs) = PDM::ConfigFile->read();

$t3s = &t3_from_hosts();

$exit = 0;
$ok   = 0;
my %PASS;

foreach my $t3 (@$t3s) {
   if (Util->ping($t3->{ipno}, 1)) {
     print "Changing $t3->{name}/$t3->{ipno}\n";
     my $rc = &change($t3, $old, $new);
     if ($rc) {
       print " -> Error: $rc\n";
       $exit++;
     } else {
       print " -> Done.\n";
       $PASS{$t3->{ipno}} = $new;
       $ok++;
     }
   } else {
     print "Cannot ping $t3->{ipno} \n";
   }
}

print "\nUpdated $ok array(s) successfully.\n ";

if ($exit) {
  print "Error(s) found while updating $exit array(s). \n";
  #exit($exit);
}

my($renv, $devs, $hosts,$notifs) = PDM::ConfigFile->read();
$update = 0;
foreach my $ipno (keys %PASS) {
   foreach my $d (@$devs) {
     if ($d->{ipno} eq $ipno) {
        $d->{telnet} = Util->encodeAll($new);
        $update = 1;
     }
   }
}

if ($update) {
   print "Updating rasagent.conf\n";
   PDM::ConfigFile->write( $renv, $devs, $hosts,$notifs);
}

print "Updating System/passwords/array\n";
System->setPassword("array", $new);

exit(0);


sub t3_from_hosts {
  my(@L, $l);
  open(O, "/etc/hosts");
  while ($l = <O>) {
    chop($l);
    next if (substr($l,0,1) eq "#");
    my($ip, $name) = split(/\s+/, $l);
    next if (!$ip);
    if ($name =~ /t3/ || $name =~ /t4/ || $name =~ /array/) {
       push(@L, {ipno => $ip, name => $name});
    }
  }
  close(O);
  return \@L;
}

sub change {
  my($t3, $old, $new) = @_;
  $old = "" if (!defined($old));
  $new = "" if (!defined($new));

  my($t) = new Net::Telnet (
             errmode => "return",
             Timeout   => 10,
             Prompt    => '/<\d+\>/',
             );

  if (!defined($t->open($t3->{ipno}))) {
      return "Cannot open $t3->{ipno}";
  }
  $t->login("root", $old);
  if ($t->errmsg()) {
     return $t->errmsg();
  }
  my @l  = $t->cmd(String => "passwd", Prompt => '/\:/');
  my @l2 = $t->cmd(String => $old,     Prompt => '/\:/');
  my @l3 = $t->cmd(String => $new,     Prompt => '/\:/');
  my @l4 = $t->cmd(String => $new,     Prompt => '/<\d+\>/');
  $t->cmd("exit");
  return "change failed" if ("@l4" =~ /sorry/);
  return 0;

}
