#!/opt/SUNWstade/bin/perl -I/opt/SUNWstade/lib
use System;
use AutoForm;
use Getopt::Std;
use GUI::ProcMgr;

System->set_home(System->home_dir());
sub usage {
  print "Usage:\n";
  print "se_configcheck -o \"dev=IP:port\"\n";
  print "IE: se_onfigcheck -o \"dev=10.0.0.1:6789\"\n";

}

if (!getopts("o:", \%opts)) {
    usage();
    exit(1);
}
my $opt = $opts{o};
my $TIMEOUT = 60 * 20;

$opt =~ s/\"//g;
my @opts = split(/\|/, $opt);
foreach my $o (@opts) {
  my($name,$val) = split(/\=/, $o);
  if(($name !~ /dev/) && ($name !~ /verbose/)){
    print "Invalid option: $name\n";
    usage();
    exit(1);
  }
  $opts{$name} = $val;
}


if ($opts{dev}) { #CALLED ON MASTER/SLAVE , relay to RACK
   #
   # se_configcheck   -o verbose=1|"dev=172.20.35.107:6789"
   #
   my $ipno = $opts{dev};
   my $test_command = "se_configcheck";
   my $opts = "-o \"verbose=1\"";
   my($renv, $devs, $hosts, $notifs) = PDM::ConfigFile->read();
   System->set_renv($renv);
   #my $email= $notifs->[0]{email};
   my $dev;
   foreach my $d (@$devs) {
     if ($d->{type} eq "se" && $d->{ipno} eq $ipno && 
       (!$d->{host} || $d->{host} eq $renv->{hostname}) ) {
       $dev = $d;
       last;
     }
   }

   # Try Pinging the device before test run
   my ($dev_ip, $dev_port) = split(/\:/, $o); 
   if (!Util->ping2($dev_ip, 10)) {
      print "Cannot ping $dev_ip.\nLost Out of Band communication to the device.";
      exit(1);
   }
   # End of ping
   
   ($err,$pid) = Scheduler->run('ST', $ipno, $test_command, $opts,
                undef, 1, 0, "", 1, 
               {
                 target   => $dev->{key},
                 dev_type => "se",
                 node     => '',
                 key      => $dev->{key},
                 comp     => "oob",
               }, 10, {NOFORK => 1} );

   if ($err) {
     print "ERROR: $err\n" ;
   } elsif ($pid !~ /\d+/) {
     print "ERROR: Diagnostic '$test_command' could not start on $ipno\n";
   } else {
     my($status, $TIME);
     while (1) {
        sleep (10);
        $TIME += 10;
        Scheduler->syncProcess("ST", $pid, $ipno);
        ($info, $pro, $out, $err) = Scheduler->read("ST", $ipno, $pid);
        $status = GUI::ProcMgr::_status($info);
        last if ($status =~ /Done/);
        last if ($TIME > $TIMEOUT);
     }
     my $ix = rindex($status, ">");
     $status = substr($status,$ix+1);
     my($p_pid,$p_start,$p_date,$p_comm) = split(/\t/, $pro->[0]);

     print "------------------------------------------\n";
     print "Diagnostic: $p_comm \n";
     print "IP Address: $info->{host}\n";
     print "Start Date: $info->{start_date}\n";
     print "End   Date: $info->{end_date}\n";
     print "Status    : $status \n";
     print "Device ID : $info->{info}{key}\n";
     print "PID       : $p_pid\n";
     print "------------------------------------------\n\n";

     foreach my $run (@$out) {
        print $run->{output} . "\n";
     }
   }
      

} else {         # CALLED ON RACK

  $options = $opts{o};

  my $args    = AutoForm->parseTestArgs($options);
  my $command = System->get_home() . "/sysbin/config_solution";
  exec $command, "-c", "-d", "-e";
}
