#!/usr/bin/perl -I/opt/SUNWstade/lib
#<copyright>
# ----------------------------------------------------------
# Sun Proprietary/Confidential Code
# Copyright 2001, Sun Microsystems, Inc. All rights reserved.
# ----------------------------------------------------------
#</copyright>

use strict;
use Inventory;
use System;
use Getopt::Std;
use Util;
use TO;
use Debug;
use MIME::Base64;
use Util::Http;
use Agent::VE;
use Agent::T3;
use Agent::6120;
System->set_test(1);

my $local_p = $INC[0];
my $ix  = rindex($local_p, "/");
my $DIR = substr($local_p,0,$ix);
Debug->level(2);

use PDM::ConfigFile;

#  3975,6975: 2 gig switch
#  65=t4, 75=minnow

my(%opts);

my $LIST   = $Inventory::INDY1 . $Inventory::INDY2 . $Inventory::MASER;
$LIST =~ s/,,/,/g;

my $T3TO = 30;  # timeout getting t3 tokens

sub usage {
   print "\n  config_solution -c|-m <expected model> -S <serial#> -p <ve password> -t <array password> -h -T timeout_seconds\n";
   print "      -h  : help \n";
   print "      -c  : validate only (Compare, no update) \n";
   print "      -s  : run normal, keep the current model (Same) \n";
   print "      -S  : serial# \n";
   print "      -p <password>  : password \n";
   print "      -m <expected model> \n";
   print "      -f <filename>  # write config to this file name\n";
   print "      -t <password> : Save Array password\n";
   print "      -T  : timeout in seconds (optional) \n";
   print "      -H  : html flag \n";
   print "      -d  : debug \n";
   print "  Example : config_solution -m 6910 \n";
   print "  Models  : $Inventory::INDY1 \n";
   #print "            $Inventory::INDY2 \n";
   print "            $Inventory::MASER \n";
}

sub secret_usage {
   print "      -i <ip_prefix>  : enter a different subnet prefix, default is ifconfig for dmfe1\n";
}

my $HOME = substr($local_p,0,$ix);
System->set_home($HOME);
my $GOLDEN = Inventory->goldenName();

if (!getopts("S:T:t:df:sci:m:sHhp:", \%opts)) {
    print "Invalid argument(s) \n";
    usage();   
    exit(1);
}

if ($opts{h}) {
   usage();
   exit(1);
}
if (!-f "$HOME/DATA/rasagent.conf") {
  print "Configuration file (DATA/rasagent.conf) does not exists, run bin/ras_install first!\n\n";
  exit(1);
}

my $HTML   = $opts{H};
my $PASS   = $opts{p} || "";
my $DEBUG  = $opts{d};
my $T3_PASS = $opts{t};
my $TO      = $opts{T};
my $SERIAL  = $opts{S};

my $saved_pass = System->getPassword("array");
if (!$saved_pass && !$T3_PASS) {
   print "Warning: Storage array password is not set, use -t to set.\n";
}
System->setPassword("array", $T3_PASS) if (defined $T3_PASS);

my $PREFIX = $opts{i} || Util->ipPrefix(System->ifconfig("dmfe1")) || "192.168";

my $MODEL;
if ($opts{c} || $opts{s}) { # find model in snapshot and compare or keep(-s)
  $MODEL = Inventory->findCurrentModel();
  if (!$MODEL && $opts{s}) {
     print "Error: Cannot use this option, MODEL not defined!\n\n";
     exit(1);
  }
  if ($opts{s}) {
     print "Current model: $MODEL\n";
  }
} else {
  $MODEL  = uc($opts{m});
  if (!$MODEL) {
    while (index($LIST, ",$MODEL,") < 0) {
       
       print "Valid models: $LIST\n" if ($MODEL);
       print "Enter Model (q=quit): ";
       $MODEL = <>;
       chop($MODEL);
       $MODEL = uc($MODEL);
       print "Model Entered: \"$MODEL\"\n";
       exit(1) if ($MODEL eq "Q");
    }
  }
  if (index($LIST, ",$MODEL,") < 0 ) {
     print "Error: Invalid model!:\"$MODEL\"\n";
     usage();
     exit(1);
  }
}

my $notifs;
my $hostname = System->hostname();

my ($model, $trace_out, $trace_out2);
my $devs = "";
my $short = $hostname;
my $ix = index($short, ".");
if ($ix > 0) {
  $short = substr($short,0,$ix);
}

my($l);
my($err, $summary, $inv);

if (index($Inventory::MASER, ",$MODEL,") >= 0) {  # maserati
  ($trace_out, $inv) = Inventory->readMaserati($short, $PREFIX, $DEBUG, $HTML, $TO);
  ($err, $trace_out2, $summary) = $inv->validateMaserati($MODEL, 1);
} else {
  ($trace_out, $inv) = Inventory->readIndy( $short, $PREFIX, $PASS, $DEBUG, $HTML, $TO);
  ($err, $trace_out2, $summary) = $inv->validateIndy($MODEL, 1);
  
}
print $summary;

if ($err) {
   print "ERRORS FOUND: Snapshot cannot be saved! \n";
   if (-f $GOLDEN && !$opts{c}) {
     rename $GOLDEN, "$GOLDEN.last";
   }
   exit(1);

} else {
   print "NO ERRORS FOUND!\n";
   $inv->{serial_no} = $SERIAL;
   if (!$opts{c}) {
     $inv->writeInventory();
     $inv->writeSnmpConfig();
     print "Snapshot $GOLDEN saved! \n";

     $inv->writeConfig("write");
     print "Config rasagent.conf saved! \n";

     my $hostname = System->hostname();
     TO->clearTopo($hostname);
     my $to = TO->readTopo($hostname,1);
     TO->copyTopo($hostname,"MERGE-MASTER");

     my $errors = $to->errorsHash();
     my $e_txt ;
     foreach my $e (keys %$errors) {
        print "$e = $errors->{$e} \n"; 
     }
     print "Topology created!\n";
   } else {
     print "Configuration verification succeeded!\n";
   }

} 

print "Done. \n\n";



sub switchPorts {
  my($ip) = @_;
  my @l = `$DIR/bin/sanbox chassis_status $ip`;
  if ("@l" =~ /Ports:\s+(\d+)/) {
     return $1;
  } else {
     return undef;
  }
}


sub switchWWN {
  my($ip) = @_;

  my @l = `$DIR/bin/sanbox version $ip`;
  if ("@l" =~ /WWN:\s+([0-9a-f]+)/) {
      return $1;
  } else {
      return undef;
  }
}

sub ping {
  my($ip) = @_;

  open(PING, "/usr/sbin/ping $ip 3|");
  my $ans = <PING>; close(PING);
  if ($ans =~ /is alive/) {
     return 1;
  } else {
     return 0;
  }
}



