#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html

BEGIN { $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr'; }

use Getopt::Long;

use lib "$::XCATROOT/lib/perl";
use Cwd;
use File::Basename;
use xCAT::Client;
use xCAT::MsgUtils;
use xCAT::Usage;
use strict;

sub updatenode_usage
{
    my $usage_string = xCAT::Usage->getUsage("updatenode");
    print "$usage_string\n";
}

my $bname = basename($0);
my $cmdref;
$cmdref->{command}->[0] = $bname;
$cmdref->{cwd}->[0] = cwd();
my $data;
# allows our plugins to get the stdin of the cmd that invoked the plugin
if ( (($^O =~ /^linux/i) && ($ENV{'SHELL'} =~ /\/ksh$/))  || !defined($ENV{'TERM'}) )
{
    my $rin="";
    my $rout;
    vec($rin,fileno(STDIN),1)=1;
    my $nfound=select($rout=$rin,"","",1);
    if ($nfound)
    {
        while ( <STDIN> ) { $data.=$_; }
        $cmdref->{stdin}->[0]=$data;
    }
}
else
{
    if (-p STDIN) {
        while ( <STDIN> ) { $data.=$_; }
        $cmdref->{stdin}->[0]=$data;
    }
}


Getopt::Long::Configure("posix_default");
Getopt::Long::Configure("no_gnu_compat");
Getopt::Long::Configure("bundling");
#GetOptions('h|help'    => \$::HELP,
#             'v|version' => \$::VERSION);
# display the usage if -h or --help is specified

my $tmp=" ".join(' ', @ARGV);
if (!($tmp =~ / (--help|-h|-v|--version)/)) {
  my $arg=shift(@ARGV);
  # Set the noderange
  if ($arg !~ /^-/) {
    my @tempnr = ();
    foreach my $nr (split(/,/, $arg)) {
      if ($nr =~ /^\^(.*)$/) {
        my $nrf = $1;
        if ($nrf !~ /^\//) { #relative path
          $nrf = Cwd::abs_path($nrf);
        }
        $nrf = "\^" . $nrf;
        push @tempnr, $nrf;
      } else {
        push @tempnr, $nr;
      }  
    }
    $arg = join(',',@tempnr);
    $cmdref->{noderange}->[0]=$arg;
  } else {
    print "The noderange should be the first argument.\n";
    &updatenode_usage();
    exit 1;
  }
}

push (@{$cmdref->{arg}}, @ARGV);

# if trying to update security, get the password 
# to access the target node firstly
if (
    !GetOptions(
				'A|updateallsw'    => \$::ALLSW,
                'c|cmdlineonly'    => \$::CMDLINE,
				'd=s'              => \$::ALTSRC,
                'h|help'           => \$::HELP,
                'v|version'        => \$::VERSION,
                'V|verbose'        => \$::VERBOSE,
                'F|sync'           => \$::FILESYNC,
                'f|snsync'         => \$::SNFILESYNC,
                'S|sw'             => \$::SWMAINTENANCE,
                's|sn'             => \$::SETSERVER,
                'P|scripts:s'      => \$::RERUNPS,
                'k|security'       => \$::SECURITY,
                'o|os:s'      	   => \$::OS,
                'user=s'           => \$::USER,
                'devicetype=s'     => \$::DEVICETYPE,
    )
  ) {
    &updatenode_usage();
    exit 1;
}

if ($::HELP)
{
  &updatenode_usage();
  exit 0;
}

# display the version statement if -v or --verison is specified
if ($::VERSION)
{
  my $version = xCAT::Utils->Version();
  print "$version\n";
  exit 0;
}
if (($::FILESYNC) && ($::SNFILESYNC)){  # only one
     my $msg = "Choose either -f to sync the service nodes, or -F  to sync the nodes not both.";
     xCAT::MsgUtils->message("E", $msg);
     exit 1;
} 


my $current_userid = getpwuid($>);
$ENV{DSH_FROM_USERID} = $current_userid;
my $to_userid;
if ($::USER)
{
    $to_userid = $::USER;
}
else
{
    $to_userid = $current_userid;
}
$ENV{DSH_TO_USERID} = $to_userid;

if ($::SECURITY) {
    my $msg;
    if (!($ENV{'DSH_REMOTE_PASSWORD'}))
    {                         # if not already set
        # prompt for the password for the userid on the node that will be setup
        my $userpw;
        $msg =
          "Enter the password for the userid: $to_userid on the node where the ssh keys \nwill be updated:\n";
        xCAT::MsgUtils->message("I", $msg);
        system("stty -echo");    # turn off keyboard
        chop($userpw = <STDIN>);
        system("stty echo");     # turn on keyboard

        if ($userpw eq "")
        {                        # did not enter a password
            $msg = "Did not enter a password will abort the security update.";
            xCAT::MsgUtils->message("E", $msg);
            exit 2;
        }
        else
        {                        # password entered pass to the server
            $ENV{DSH_REMOTE_PASSWORD} = $userpw;

        }
    }
}

foreach (keys %ENV) {
    if (/^DSH_/ || /^XCAT/) {
        push @{$cmdref->{environment}}, "$_=$ENV{$_}";
    }
}

xCAT::Client::submit_request($cmdref,\&xCAT::Client::handle_response);
exit $xCAT::Client::EXITCODE;