#%Module
# Choose MPI version
#
# This check if any MPI lib was previously "module loaded"
#
# If an MPI library was loaded, and the family of loaded MPI lib matches
# the name of the parent dir for this .modulerc, and there is a subdir
# of that directory matching the MPI version, then we default to that
# subdir.
#
# If no MPI library was previously module loaded, or the loaded MPI family
# does not match the name of the parent directory, or no subdir matching
# the MPI version is found, then we just return w/out defaulting.
# The modulecmd will then default according to its own rules, and it is
# up to the resulting modulefile to either load the appropriate MPI library
# or abort/complain about an MPI mismatch as appropriate.
#
# NOTE: no special handling is needed for intelmpi, as we assume that if
# intelmpi is selected because the intel compiler was loaded, then we
# are to use the intelmpi that shipped with the compiler suite, and so
# there is no needed for additional versioning of the intelmpi beneath
# the intelmpi subdir.  If intelmpi is being used with a non-intel compiler,
# then it is assumed intelmpi was explicitly loaded.
#
# Usage:
# In most cases, can simply symlink .modulerc to this file
#
# In more complicated cases, .modulerc can source this file, and can
# then test the variable _did_default, which will be true if we set
# a default for modules for the next level, or false otherwise (in which
# case your .modulerc can set one)

# Source some required Tcl procedures here.  Hack for cookbook
# making use of environment variable for location.
# In production, this should ideally be in a site config file.
# At minimum, hardcode the path
set rootdir $::env(MOD_GIT_ROOTDIR)
set tcllibdir $rootdir/doc/example/compiler-etc-dependencies/tcllib
source $tcllibdir/common_utilities.tcl

# _did_default will be true if we actually default something
set _did_default false

set moduledir [file dirname $ModulesCurrentModulefile]
set parentDir [ file tail $moduledir ]

# Get the currently loaded compiler, w/out defaulting
set fullMpiTag [ GetLoadedMPI 0 ]

if { $fullMpiTag ne {} } {
   # We have an MPI module loaded
   set tmpMpiTag [ GetPackageFamilyVersion $fullMpiTag ]
   set mpiFamily [ lindex $tmpMpiTag 0 ]
   set mpiVersion [ lindex $tmpMpiTag 1 ]

   # Canonicalize intelmpi variants in parentDir and mpiFamily for comparison
   set intelList "intelmpi impi intel intelmpi-mt impi-mt intel-mt"
   set tmpMpiFamily $mpiFamily
   if { [lsearch $intelList $tmpMpiFamily] > -1 } {
	set tmpMpiFamily intelmpi
   }
   set tmpParentDir $parentDir
   if { [lsearch $intelList $tmpParentDir] > -1 } {
	set tmpParentDir intelmpi
   }

   if { $tmpParentDir eq $tmpMpiFamily } {
      # The loaded MPI lib family name matches parentDir
      # So see if have subdir matching version, and if so, default it
      if { $mpiVersion ne {} } {
         if [ ChildModuleExists $mpiVersion ] {
            module-version $mpiFamily/$mpiVersion default
            set _did_default true
         }
      }
   }
}
