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

use Getopt::Std;
use Diags::MinnowUtil;
use strict;

use System;
# Force output to be written
$| = 1;

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

my($renv) = PDM::ConfigFile->read();

System->set_renv($renv);


$SIG{INT} = \&killTest;
$SIG{TERM} = \&killTest;

my $RETRYS = 3;    #how many times to retry the 3510 if no reply
my $RETRYWAIT = 3;  #how many sec's to wait between retrys
my $SYSMODEL = "3510";  #This is the Name to output for the device


my @FaultyFRU=undef;
my @error_tests;
my @pass_tests;
my $OffLineQuestion = "WARNING - To avoid system hanging, ". 
                      " You would better run w/r buffer test offline .\n Continue ? [N]: ";
 
my %Options;
my $ok = getopts('qseykruvdlfno:', \%Options);
my $numargs=keys(%Options);
my $t3=undef;

#check to see if user needs to be told this is a offline test
if ($Options{"y"} ne 1){
  print $OffLineQuestion."\n";
  my $ans=<STDIN>;
  while(($ans =~ /[Yy]/) != 1){
    if($ans eq "\n" || $ans =~ /[Nn]/){
      print "Not Continuing\n";
      exit();
    }  
    print $OffLineQuestion."\n";
    $ans=<STDIN>;
  }
  print "Continuing\n";
}


#parse all -o options or any write read buffer test specific options
#disable all Options and specify valid parameters
my %ofdgParameters=undef; 
@ofdgParameters{"dev",
                "passwd",
		"channel"
                }= (undef,undef,undef);


#split up indivdual parameters
#these are variable needed to do this:
my $i;
my $key;
my $value;
my $match;
my $validOption;
my $verbose = 0;

if ($Options{"v"}){
  $verbose = 1;
}
my @tmpParams = split(/\|/,$Options{"o"});
#loop through params
foreach $i (@tmpParams){ 
  ($key, $value) = split(/=/,$i);
  # Remove any spaces from key and values.
  $key =~ s/\s*//g;
  $value=~ s/\s*//g;


  $match=undef;
  foreach $validOption (keys(%ofdgParameters)){
    if ($key eq $validOption){
      $match = $validOption;
    }
  }
  #if the key does not match a valid option then the match does not get defined
  if(! (defined($match))){
     print "$key is an invalid option.\n".
           "write read buffer test did not execute.\n". 
           "write read buffer test exiting.\n";
     exit(1);
  } else  { #finally stick the value in the @#$'n hash
    $ofdgParameters{$key}=$value; 
    # print "$key is :"+ $key ;
    # print " $value is :"+ $value +"\n";
  }
}

#check to make sure values are OK
my $param=undef;
while(($param,$value) = each(%ofdgParameters)) {
  if($param =~ /wrbtest|compare|stop-on-error/){
    if(!($value =~/(En)|(dis)able/i)){
      print "$param=$value is an invalid option.\n".
            "write read buffer test did not execute.\n". 
            "write read buffer test exiting.\n";
      exit(1);
    }
  } 
}

#if debug print out parameters
if($Options{"d"}){ 
  print "INPUT PARAMETERS:\n";
  while (($key,$value) = each(%ofdgParameters)){
    print "key: $key value:$value \n";
  }
}
#Done Parsing -o options

#start issuing commands to minnow device 
#so let's declare a MinnowUtil to use

while(! (defined($ofdgParameters{dev}) )){
  print "$ofdgParameters{dev}\n";
  print "Please Enter the IPaddress or name of the device you whish to test:\n";
  chomp($ofdgParameters{dev}=<STDIN>);
}

my $minnow = new MinnowUtil($ofdgParameters{dev}); 

if(!($minnow->connect())){ 

  if ($Options{"v"}){
    print $minnow->{error}."\n"; 
  }
  print "Unable to establish communication with the device.\n";
  print "PROBABLE-CAUSE:\n";
  print "\t This problem can be caused by a very slow network, or \n";
  print "\t because the ethernet connection to this device was lost. \n";
  print "RECOMMENDED-ACTION:\n";
  print "\t 1. Check Ethernet connectivity to the affected device\n";
  print "\t 2. Verify device is booted correctly\n";
  print "\t 3. Verify correct TCP/IP settings on device \n";
  exit(1);
}

if ($Options{"v"}){
  $minnow->set_verbose();
}

if ($Options{"d"}){
#  $minnow->set_debug();
}

#if -k Kill current Tests
if($Options{"k"}){ 
killTest();
}


print "Show loop map for the 3510 device\n";
my $status = $minnow->runLoopMapTest(\%ofdgParameters);
if ($status == -1) {
     push(@error_tests, "Show loop map test failed"); 
     exit (1);
} else {
     push(@pass_tests, "Show loop map test passed");
}


sub testpassed{
  if($minnow->{ondgError} =~ /PASS/i){
    return 1;
  }else{
    return 0;
  }
}


sub summary {
  print "Summary:\n";
  if(!defined(@pass_tests)&&!defined(@error_tests)){
    print "No tests were ran.\n";
  }
  print "Show loop map is finished\n";
  print "\n\n";
}



sub killTest{
  print " Kill function will be added later \n";
  return 0;
}


END {
  summary();
}

