-- -*- lua -*-

require("fileOps")

-- Skip the CMake test if we can't find a new enough version of CMake
local skip_test = true
local cmake_path = "/bin/false"
local cmake_ret = os.execute([[echo "cmake_minimum_required(VERSION 3.6)" > version_check.cmake && cmake -P version_check.cmake 2>/dev/null]])
os.remove("version_check.cmake")

-- Need to test both true and 0 to account for API changes for os.execute
-- between Lua 5.1 and 5.2
if (cmake_ret == true) or (cmake_ret == 0) then
   -- Get the full path to CMake to use in the test script since it might be
   -- stripped out of the path by the time the test is run
   cmake_path = findInPath("cmake", os.getenv("PATH"))
   skip_test = false
end

testdescript = {
   owner   = "rtm",
   product = "modules",
   description = [[
	 Test cmake module interface
   ]],
   keywords = {"cmake" },

   active = not skip_test,
   testName = "cmake",
   job_submit_method = "INTERACTIVE",

   runScript = [[CMAKE="]]..cmake_path..[["
     . $(projectDir)/rt/common_funcs.sh

     unsetMT
     initStdEnvVars
     MODULEPATH_ROOT=$(testDir)/mf;       export MODULEPATH_ROOT
     MODULEPATH="$MODULEPATH_ROOT/Core";  export MODULEPATH

     rm -rf init
     mkdir init
     sed -e 's|@PKG@/libexec/lmod|lua;$(projectDir)/src/lmod.in.lua|g' \
              < $(projectDir)/init/cmake > init/cmake

cat > $(testName).cmake << EOF
include("$(outputDir)/init/cmake")

module_avail(AVAIL_MODS)
message("AVAIL_MODS: \${AVAIL_MODS}")

module_list(CURRENT_MODS)
message("CURRENT_MODS: \${CURRENT_MODS}")
message("ENV{FOO}: \$ENV{FOO}")
message("ENV{BAR}: \$ENV{BAR}")

module(load foo)

module_list(CURRENT_MODS)
message("CURRENT_MODS: \${CURRENT_MODS}")
message("ENV{FOO}: \$ENV{FOO}")
message("ENV{BAR}: \$ENV{BAR}")

module_swap(foo foo/1.0)

module_list(CURRENT_MODS)
message("CURRENT_MODS: \${CURRENT_MODS}")
message("ENV{FOO}: \$ENV{FOO}")
message("ENV{BAR}: \$ENV{BAR}")

module(swap foo bar)

module_list(CURRENT_MODS)
message("CURRENT_MODS: \${CURRENT_MODS}")
message("ENV{FOO}: \$ENV{FOO}")
message("ENV{BAR}: \$ENV{BAR}")

module(load bad)

EOF

     rm -fr _stderr.* _stdout.* err.* out.* .lmod.d results.csv

     $CMAKE -P ./$(testName).cmake  > _stdout.000 2> _stderr.000

     HOME=$ORIG_HOME

     cleanUp _stdout.000 out.txt
     cleanUp _stderr.000 err.txt
     wrapperDiff --csv results.csv $(testDir)/out.txt out.txt
     wrapperDiff --csv results.csv $(testDir)/err.txt err.txt
     testFinish -r $(resultFn) -t $(runtimeFn) results.csv
   ]],


   blessScript = [[
         # perform what is needed
   ]],

   tests = {
      { id='t1'},
   },
}
