#!/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 $local_p   = $INC[0];
my($ix)    = rindex($local_p, "/");
my(%opts);

my $HOME      = substr($local_p,0,$ix);

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

my $MASTER    = Util->findMaster();
my $COMMANDS  = ",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.
  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 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;

TO DOWNLOAD using wget:
  \# wget --proxy=off "URL" --http-user=<default=ras> --http-passwd=<default=agent>

TO Download using a browser, use the URL directly.

Use the following url to download the most recent tar file:
   http://$hostname:7654/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;
}

