#!/usr/bin/env perl
#
# Copyright (c) 2019-2020 Intel, Inc.  All rights reserved.
#
# Copyright (c) 2019      Cisco Systems, Inc.  All rights reserved
# $COPYRIGHT$
#
# Additional copyrights may follow

use strict;

my @tests = ("-n 4 --ns-dist 3:1 --fence \"[db | 0:0-2;1:0]\"",
             "-n 4 --ns-dist 3:1 --fence \"[db | 0:;1:0]\"",
             "-n 4 --ns-dist 3:1 --fence \"[db | 0:;1:]\"",
             "-n 4 --ns-dist 3:1 --fence \"[0:]\"",
             "-n 4 --ns-dist 3:1 --fence \"[b | 0:]\"",
             "-n 4 --ns-dist 3:1 --fence \"[d | 0:]\" --noise \"[0:0,1]\"",
             "-n 4 --job-fence -c",
             "-n 4 --job-fence",
             "-n 2 --test-publish",
             "-n 2 --test-spawn",
             "-n 2 --test-connect",
             "-n 5 --test-resolve-peers --ns-dist \"1:2:2\"",
             "-n 5 --test-replace 100:0,1,10,50,99",
             "-n 5 --test-internal 10",
             "-s 1 -n 2 --job-fence",
             "-s 1 -n 2 --job-fence -c");
#             "-s 2 -n 2 --job-fence",
#            "-s 2 -n 2 --job-fence -c");

my $test;
my $cmd;
my $output;
my $status = 0;
my $testnum;
my $timeout_cmd = "";

# We are running tests against the build tree (vs. the installation
# tree).  Autogen gives us a full list of all possible component
# directories in PMIX_COMPONENT_LIBRARY_PATHS.  Iterate through each
# of those directories: 1) to see if there is actually a component
# built in there, and 2) to turn it into an absolute path name.  Then
# put the new list in the "mca_bast_component_path" MCA parameter env
# variable so that the MCA base knows where to find all the
# components.
my @myfullpaths;
my $mybuilddir = "@PMIX_BUILT_TEST_PREFIX@";
my $mypathstr = "@PMIX_COMPONENT_LIBRARY_PATHS@";
my @splitstr = split(':', $mypathstr);
foreach my $path (@splitstr) {
    # Note that the component is actually built in the ".libs"
    # subdirectory.  If the component wasn't built, that subdirectory
    # will not exist, so don't save it.
    my $fullpath = $mybuilddir . "/" . $path . "/.libs";
    push(@myfullpaths, $fullpath)
        if (-d $fullpath);
}
my $mymcapaths = join(":", @myfullpaths);
$ENV{'PMIX_MCA_mca_base_component_path'} = $mymcapaths;

my $wdir = $mybuilddir . "/test";
chdir $wdir;

$testnum = $0;
$testnum =~ s/.pl//;
$testnum = substr($testnum, -2);
$test = @tests[$testnum];

# find the timeout or gtimeout cmd so we can timeout the
# test if it hangs
my @paths = split(/:/, $ENV{PATH});
foreach my $p (@paths) {
    my $fullpath = $p . "/" . "gtimeout";
    if ((-e $fullpath) && (-f $fullpath)) {
        $timeout_cmd = $fullpath . " --preserve-status -k 500 450 ";
        last;
    } else {
        my $fullpath = $p . "/" . "timeout";
        if ((-e $fullpath) && (-f $fullpath)) {
            $timeout_cmd = $fullpath . " --preserve-status -k 500 450 ";
            last;
        }
    }
}

$cmd = $timeout_cmd . " ./pmix_test " . $test . " 2>&1";
print $cmd . "\n";
$output = `$cmd`;
print $output . "\n";
print "CODE $?\n";
$status = "$?";

exit($status >> 8);
