#!/usr/bin/perl -w

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

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

if (init()) {
    exit 1;
}

$rc = test_remote(\%config);
print "TEST_REMOTE: $rc\n";
#done

exit(0);

sub test_remote {
    my $href = shift;
    
    my @ionodes = @{$href->{'IONODES'}};
    my $rc = 0;

    my $remote_dir = dir_parent($href->{'WORKINGDIR'});

    my $cmd = "%node uname -a\n";
    $cmd = "%node ls /foogt";

    $rc = do_remote_command($href->{'RCMDPROG'}, 8, 30, $cmd, \&pre_func, undef, @ionodes);

    print "HERE!\n";
#    while(1) {
#	sleep(15);
#    }

#    while($count<10) {
#	sleep(1);
#	$count++;
#    }

    return($rc);
}

sub pre_func {
	my $node = shift;
				
	my $cmd = "echo $node pre-func\n";
#	$cmd = "ls -l /foog";

	my $rc = system("$cmd");

	return($rc);

}
	
sub post_func {
	my $node = shift;
	
	my $cmd = "echo $node post-func\n";
	my $rc = system("$cmd");
	return($rc);

}
	


sub usage{}

sub init {
    GetOptions(\%args,
	       's|session:s',
	       'sd|sessiondir:s',
	       'rsh:s',
	       'rcp:s',
	       'r|root:s',
	       'h|help'
	       );

    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'} || $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);
}

