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


use Getopt::Std;
use PDM::ConfigFile;
use GUIAdmin;
use System;
use strict;
use Data::Dumper;
my $CAT = "a5k";

sub usage() {
  print <<EOF;
  This program can be used on a slave to automatically discover a5k devices
  and update the configuration file on both the slave and the master. 
  The monitoring frequency can also be set.
  This allows scripting of storade installation on hosts with storage devices
  without having to use the GUI on the master.

  Usage: ras_discover -d <type> -f <frequency>  -h
          type : $CAT
          frequency: monitoring frequency (mins).
	  -h help - show this help information.
  Example: ras_discover -d a5k -f 30
EOF
}

System->set_home("/opt/SUNWstade");
my($renv, $devices, $hosts, $notifs) = PDM::ConfigFile->read();
System->set_renv($renv);
my (%opts);

if (!getopts("hd:f:?", \%opts) || $opts{"?"} || $opts{h}) {
    usage();
    exit();
}

if (!$opts{d} && !$opts{f}) {
   usage();
   exit();
}

my $MASTER= Util->findMaster();
if (!$MASTER) {
  print "Error: this is not a slave!\n";
  exit(1);
}
my $RASPORT  = System->getConfigPort("/opt/SUNWstade");
my $TYPES  = $opts{d};
my $FREQ  = $opts{f};
my @new;

my @T = split(/\|/, $TYPES);

my $test = 0;
foreach my $TYPE (@T) {
  if (index("|$CAT|","|$TYPE|") < 0) {
     print "Error: invalid device-type: $TYPE!\n";
     usage();
     exit(1);

  } elsif (lc($TYPE) eq "a5k") {
     $renv->{categories} .= "|a5k" if (index($renv->{categories}, "a5k") < 0);
     @new = discover_a5k($renv, $devices);
     $test = 1;

  } else {
     $renv->{categories} .= "|$TYPE" if (index($renv->{categories}, $TYPE) < 0);
  }
}

$renv->{categories} .= "|host" if (index($renv->{categories}, "host") < 0);
$renv->{categories} .= "|san" if (index($renv->{categories}, "san") < 0);

my $p = {
             type      => "HOSTINFO", 
             hostname  => $renv->{hostname}, 
	     datahost  => "Y",
             categories=> $renv->{categories}
         };

if ($FREQ) {
  $renv->{frequency} = $FREQ;
  $p->{frequency}    = $FREQ;
}
push(@new, $p);

my $rc = Util::Http->postCommand("$MASTER:$RASPORT", "Slave::add_devices&ipname=$renv->{hostname}",
                          Dumper(\@new), 30);
my ($x, $cnt);
if ($rc =~ /OK\s*$/) {
  if ($test) {
     for ($x=0; $x <= $#$devices; $x++) {
        next if ($devices->[$x]{host} ne $renv->{hostname});
        if (index($rc, $devices->[$x]{key}) < 0) {
           $devices->[$x] = undef; # take out if already on master
        } else {
           $cnt++;
        }
     }
  }

  if ($cnt == undef) {
    print "No devices were discovered.\n";
  }
  else {
    PDM::ConfigFile->write($renv, $devices, $hosts, $notifs);
    print "Configuration updated: $cnt device(s)\n";
  }
} else {
  print "Error: failed to update master($MASTER): $rc!\n";
}

# need to update master also



sub discover_a5k {
  my ($renv, $devs) = @_;
  my (%F, $newones, @new,  $found); 
  for ($x=0; $x <= $#$devs; $x++) {
     my $d = $devs->[$x];
     my $h1 = $d->{host} || $renv->{hostname};
     next if ($h1 ne $renv->{hostname});
     if ($d->{type} eq "a5k") {
        $F{$d->{wwn}} = $x+1;
     }
  }

  my $rc = GUIAdmin::get_luxDevices({ TYPE => "A5K", FILTER => "SENA"});
  if (substr($rc,0,2) ne "OK") {
     print "Error: $rc";
     return undef;
  }
  my @L = split(/\n/, substr($rc,3) );
  my $new_dev= $#$devs ;

  foreach my $l (@L) {
     my ($name, $wwn, $hba) = split(/=/, $l);
     $found++;
     if ($F{$wwn}) {
        push(@new, $devs->[$F{$wwn}-1]);
     } else {
        $newones++;
        $new_dev++;
        $F{$wwn} = 1;
        $devs->[$new_dev]{_name}    = "device" . $new_dev;
        $devs->[$new_dev]{type}     = "a5k";
        $devs->[$new_dev]{class}    = "storage.a5k";
        $devs->[$new_dev]{wwn}      = Util->rtrim($wwn);
        $devs->[$new_dev]{key}      = Util->rtrim($wwn);
        $devs->[$new_dev]{active}   = 'Y';
        $devs->[$new_dev]{name}     = $name;
        $devs->[$new_dev]{host}     = $renv->{hostname};
        push(@new, $devs->[$new_dev]);
     }
  }
  print "found $newones new device(s).\n" if ($newones);
  return @new;
}

