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

# 
# currently only works on the master.
#
use strict 'vars';
use System;
use Util::Http;
use Util;
use Getopt::Std;
use PDM::ConfigFile;
use Data::Dumper;

my(%opts);

my $HOME      = "/opt/SUNWstade";

System->set_home($HOME);
my $RASPORT   = System->getConfigPort($HOME);
System->set_rasport($RASPORT);

my $MASTER    = Util->findMaster();
my $COMMANDS  = ",set_countrycode,ether_list,ether_add,ether_delete,extract_list,extract_run,";

sub usage {
  print "
 Usage   : sys_admin command [options]
 Commands:
  ether_list     : display /etc/ethers file.
  ether_add      : add an ether address.
  set_countrycode: Set Country .
  ether_delete   : delete an ether address.
  extract_list   : List available extract file.
  extract_run    : Run the extract program.
  \n\n";
}
#  event_list     : Show X most recent alerts.

my $command = shift @ARGV;

if ($command eq "help" || $command eq "usage") {
   &usage();
   exit;
}

if (index($COMMANDS, ",$command,") < 0) {
   &usage();
   exit;
}
my($renv, $devices, $hosts, $notifs) = PDM::ConfigFile->read();

if(!$renv){
   print "Must run ras_install before executing this command\n";
   exit(1);
}

System->set_renv($renv);

&$command();
print "\n";
exit(0);

sub ether_list {
  if (!getopts("h", \%opts) || $opts{'h'} ) {
     print "Usage: sys_admin ether_list [-h] \n";
  }
  open(O, "/etc/ethers");
  my $l;
  while ($l = <O>) {
    print $l;
  }
  close(O);
}

sub print_codes {
  my($F) = @_;
  open(O, $F);
  my ($cnt, $l);
  while ($l = <O>) {
    my @a = split(/\:/, $l);
    print sprintf("%-16.16s", $a[0]);
    print "\n" if (++$cnt %6 == 0);
  }
  print "\n";
  close(O);
}

sub set_countrycode  {
   my %L;
   my $F = "/opt/se6x20/bin/locale/C/country_codes";
   if (!getopts("hc:", \%opts) || $opts{'h'} || !$opts{c}) {
      print "Usage: sys_admin set_countrycode -c Canada \n";
      &print_codes($F);
      exit(1);
   }
   open(O, $F);
   my ($cnt, $l, $found);
   while ($l = <O>) {
     my @a = split(/\:/, $l);
     if ($a[0] eq $opts{c}) {
        $found = 1; last;
     }
   }
   close(O);
   if ($found) {
     $renv->{country_code} = $opts{c};

     system("/opt/se6x20/bin/setCountryCode \"$opts{c}\" 2>/tmp/set_country_code.log");

     PDM::ConfigFile->write($renv, $devices, $hosts, $notifs);
     print "\nConfig file updated!\n";
   } else {
     print "Error: code '$opts{c}' not found \n";
   }
}



sub ether_add {
  if (!getopts("n:e:h", \%opts) || $opts{'h'} || !$opts{n} || !$opts{e} ) {
     print "Usage: sys_admin ether_add -n name -e ether [-h] \n";
     print "       Name: sw1a, sw1b ...\n";
     exit(1);
  }
  my $N = &valid_names();
  my $name  = $opts{n};
  if (!exists($N->{$name})) {
     print "Error: Name $name not in /etc/host file \n";
     exit(1);
  }
  my $ether = uc($opts{e});
  if ($ether !~ /^[0-9A-F\:]+$/) {
     print "Error: Invalid ether: $ether\n";
     exit(1);
  }
  my $E = &read_ether();
  $E->{$name} = $ether;
  &write_ether($E);
}

sub ether_delete {
  if (!getopts("n:h", \%opts) || $opts{'h'} || !$opts{n} ) {
     print "Usage: sys_admin ether_delete -n name [-h] \n";
     print "       Name: sw1a, sw1b ...\n";
     exit(1);
  }
  my $name  = $opts{n};
  my $E = &read_ether();
  if (!exists($E->{$name})) {
    print "Error: Ether entry for $name does not exists!\n";
    exit(1);
  }
  delete $E->{$name};
  &write_ether($E);
}


sub read_ether {
  my $l;
  my %E;
  open(O, "/etc/ethers");
  while ($l = <O>) {
     chop($l);
     my($e, $n) = split(/\s+/, $l);
     $E{$n} = $e;
  }
  close(O);
  return \%E;
}

sub valid_names {
  my(%N, $l);
  open(O, "/etc/hosts");
  while ($l = <O>) {
     chop($l);
     #next if ($l !~ /192.168/);
     my($ip, $n) = split(/\s+/, $l);
     $N{$n} = $ip;
  }
  return \%N;
}


sub write_ether {
  my($E) = @_;
  my $out;
  foreach my $e (sort keys %$E) {
     $out .= "$E->{$e}\t$e\n";
  }
  open(O, ">/etc/ethers");
  print O $out;
  close(O);
}

sub extract_list {
  if (!getopts("h", \%opts) || $opts{'h'}) {
     print "Usage: extract_list [-h] \n";
     exit(1);
  }
  my $f = "%-17.17s %-80.80s \n";

  print "\n";
  printf($f, "Date","URL");
  printf($f, &lines(2));

  opendir(O, System->get_home() . "/DATA/Download/Solution");
  my @L = readdir(O); closedir(O);
  my $hostname = "<host>";

  foreach my $file (@L) {
     next if ($file !~ /extract_(\d.*)\.tar/);
     my $d = $1;
     my $ix = index($d, "_");
     $d = substr($d,$ix+1);
     printf($f, $d, "http://$hostname:7654/Download/Solution/$file");
  }
  print <<EOF;


1) To Download using a browser, use the URL directly.

2) To Download using the wget shareware HTTP utility:

   For SE6320, SE3910, SE3960, SE6910 and SE6960 use:
   \# wget --proxy=off "URL" --http-user=<default=ras> --http-passwd=<default=agent>
   where the URL is http://$hostname:7654/Download/Solution/Extract.tar

   For SE6920 use:
   \# wget --proxy=off "URL" --http-user=<default=guest> --http-passwd=<default=guest>
   where the URL is https://$hostname:6789/Download/Solution/Extract.tar



EOF

}

sub extract_run {
  if (!getopts("hd:t:", \%opts) || $opts{'h'}) {
     print "Usage: extract_run -d [debug=1|2] -t [array password] [-h] \n";
     exit(1);
  }
  
  my $opts = "-d $opts{d}" if ($opts{d});
  $opts .= " -t $opts{t}" if ($opts{t});

  my $com = System->get_home() . "/sysbin/se_extract -r $opts -b";
  system($com);
  print "se_extract started in background!\n";
}

  

sub lines {
  my ($cnt) = @_;
  my (@L, $x);
  for ($x=1; $x <= $cnt; $x++) {
     push(@L, "-----------------------------------------------------------");
  }
  return @L;
}

