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

use Getopt::Std;
use Diags::T3Connection;
use strict;

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

my $OffLineQuestion = "WARNING - Volume data will be offline ". 
                      "while OFDG is running.\n Continue ? [N]: ";
my %Options;
my $ok = getopts('qseykruvdlfno:', \%Options);
my $numargs=keys(%Options);
my $t3=undef;

#check to see if usage needs to be displayed
if( !$ok || $Options{"u"} eq 1 ){
  print "you entered something wrong!\n";
  printUsage();
  exit();
}

#parse all -o options or any specific options
#disable all Options and specify valid parameters
 
my %Parameters=undef;
@Parameters{ "dev", "usr", "passwd" }= 
                (undef, "root", undef);

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

my @tmpParams = split(/\|/,$Options{"o"});
#loop through params
foreach $i (@tmpParams){ 
  ($key, $value) = split(/=/,$i);

  $match=undef;
  foreach $validOption (keys(%Parameters)){
    if ($key eq $validOption){
      $match = $validOption;
    }
  }
  #if the key does not match a valid option then the match does not get difined
  if(! (defined($match))){
     print "$key is an invalid option.\n".
     exit(1);
  } else  { #finally stick the value in the @#$'n hash
    $Parameters{$key}=$value; 
  }
}

  #check to make sure values are OK
my $param=undef;
while(($param,$value) = each(%Parameters)) {
  if($param=~/health_check|fast_test|fast_find|find/){
    if(!($value =~/(en)|(dis)able/i)){
      print "$param=$value is an invalid option.\n".
      exit(1);
    }
  } elsif($param=~/syslog/){
    if( (!($value =~/all/i)) && (!($value =~ /ofdg/i))  && (!($value =~ /Disable/i)) ){
      print "$param=$value is an invalid option.\n".
      exit(1);
    }
  }
}

#if debug print out parameters
if($Options{"d"}){ 
  print "INPUT PARAMETERS:\n";
  while (($key,$value) = each(%Parameters)){
    print "key: $key value:$value \n";
  }
}

#Done Parsing -o options
#looks Like we are ready to start issuing commands to the T3
#So let's declare a T3Connection to use

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

if(! (defined($Parameters{usr})) ){
  print "Please Enter User Name for $Parameters{dev}\n";
  chomp($Parameters{usr}=<STDIN>);
}

if(! (defined($Parameters{passwd})) ){
  print "Please Enter the password for $Parameters{dev}\n";
  chomp($Parameters{passwd}=<STDIN>);
}

my $t3 = new T3Connection($Parameters{dev}, 
                          $Parameters{usr}, 
			  $Parameters{passwd});

if(($t3->init())){ 
  if($t3->{sysModel} eq "T4"){ $SYSMODEL = "6120";
  }else{ $SYSMODEL = $t3->{sysModel};}
  print "$SYSMODEL appears to already have some HTML files working\n";
  if ($Options{"v"}){
    print $t3->{error}."\n"; 
  }
}else{
  print "Cannot initialize connection to $Parameters{dev}\n";
  exit(-1);
}
print "Atempting to blast $SYSMODEL files! \n";
$t3->blastWebFiles();
exit(1);

sub printUsage{
  print "$0 -[uy] -o\"dev=xxx.xxx.xxx.xxx|usr=root|passwd=XXX\"\n";
}
