#!/usr/bin/perl -w

use Getopt::Long;
use POSIX "sys_wait_h";
use File::Basename;

#globals
my $session_file;
my %config;
my $rc = 0;
my $pid;

if (init()) {
    exit 1;
}

if ($args{'ping'}) {
    $rc = ping_test(\%config);
    if ($rc) {
	print STDERR "WARNING: ping test failed on node $rc, trying to recover\n";
	$rc = recover_from_bad(\%config, $rc);
	exit($rc);
    }
}

if ($args{'one-all'}) {
    $rc = one_all_test(\%config);
    if ($rc) {
	print STDERR "WARNING: basic remote command one to all test failed on node $rc, trying to recover\n";
	$rc = recover_from_bad(\%config, $rc);
	exit($rc);
    }
}

if ($args{'each-all'}) {
    $rc = each_all_test(\%config);
    if ($rc) {
	print STDERR "WARNING: extended remote command each to all test failed on node $rc, trying to recover\n";
	$rc = recover_from_bad(\%config, $rc);
	exit($rc);
    }
}

exit(0);

sub each_all_test {
    print STDERR "WARNING: test not implimented yet\n";
    return(0);
}

sub one_all_test {
    my $href = shift;

    my @nodes = (@{$href->{'IONODES'}}, @{$href->{'COMPNODES'}});

    if ($href->{'UNIQUEMETA'}) {
	@nodes = (@nodes, @{$href->{'MGR'}});
    }

    my $cmd = "%node echo hello world";
    $cmd = "%node uname -a";

    $rc = do_remote_command($href->{'RCMDPROG'}, 8, 20, $cmd, undef, undef, @nodes);
    return($rc);
}

sub ping_test {
    my $href = shift;

    my @nodes = (@{$href->{'IONODES'}}, @{$href->{'COMPNODES'}});

    if ($href->{'UNIQUEMETA'}) {
	@nodes = (@nodes, @{$href->{'MGR'}});
    }
    my $cmd = "-c 1 %node >/dev/null 2>&1";
    $rc = do_remote_command("ping", 8, 5, $cmd, undef, undef, @nodes);
    return($rc);
}
    

sub usage {

    print<<EOF;
Usage: $prog_name [option]
-s -session       session file for PAV to use
-sd -sessiondir   directory containing session file 'pvfs_autosession'
-rsh              remote shell command to use (default 'ssh')
-rcp              remote copy command to use (default 'scp')
-h -help          display this message
EOF
}

sub init {
    GetOptions(\%args,
	       's|session:s',
	       'sd|sessiondir:s',
	       'rsh:s',
	       'rcp:s',
	       'r|root:s',
	       'h|help',
	       'ping',
	       'one-all',
	       'each-all',
	       'pvfs-vol',
	       'pvfs-size'
	       );

    if ($args{'h'}) {
	usage();
	return(1);
    }

    if ($args{'s'}) {
	$session_file = $args{'s'};
    } elsif ($args{'sd'}) {
	$session_file = $args{'sd'} . "/pvfs_autosession";
    } else {
	usage();
	return(1);
    }

    if (!-f $session_file) {
	print STDERR "ERROR: cannot find session file: $session_file\n";
	return(1);
    }

    %config = ('RCMDPROG' => "ssh",
	       'RCPPROG' => "scp",
	       'PROGROOT' => "./"
	       );

    $config{'PROGROOT'} = $args{'r'} || real_dir($0) || $config{'PROGROOT'};
    my $prog_root = $config{'PROGROOT'};
    require "$prog_root/pav_lib.pl";

    $rc = read_sessionfile($session_file, \%config);
    if ($rc) {
	print STDERR "ERROR: cannot read session file\n";
	return(1);
    }

    $config{'RCMDPROG'} = $args{'rsh'} || $config{'RCMDPROG'};
    $config{'RCPPROG'} = $args{'rcp'} || $config{'RCPPROG'};

    $prog_name = $0;
    $pid = $$;

    return(0);
}

sub real_dir {
   my ($file, $reldir, $suffix) = fileparse(shift);
   my ($realdir, $envpwd);

   if ($reldir =~ /^\//) {
      $realdir = $reldir;
   } else {
      if (!$ENV{PWD}) {
         chomp($envpwd = `pwd`);
      } else {
         $envpwd = $ENV{PWD};
      }
      $realdir = $envpwd . "/$reldir";
   }
   $realdir .= '/';
   $realdir =~ s#/./#/#g;
   $realdir =~ s#//#/#g;
   return($realdir);
}
