#%Module1.0#####################################################################
#
# Copyright 2003-2020 Intel Corporation.
# 
# This software and the related documents are Intel copyrighted materials, and
# your use of them is governed by the express license under which they were
# provided to you (License). Unless the License provides otherwise, you may
# not use, modify, copy, publish, distribute, disclose or transmit this
# software or the related documents without Intel's prior written permission.
# 
# This software and the related documents are provided as is, with no express
# or implied warranties, other than those that are expressly stated in the
# License.
#
##
## Intel(R) MPI Library modulefile
##

set min_tcl_ver 8.4
if { $tcl_version < $min_tcl_ver } {
    puts stderr " "
    puts stderr "ERROR: This modulefile requires tcl $min_tcl_ver or greater."
    puts stderr "Your system reports that tclsh version $tcl_version is installed."
    exit 1
}

# get full pathname for this script file
set scriptpath "${ModulesCurrentModulefile}"

# if modulefile script name is a symlink, resolve it
if { "[file type "$scriptpath"]" eq "link" } {
    set scriptpath "[file readlink "$scriptpath"]"
}

# if fullpath contains links, resolve them
set scriptpath "[file normalize "$scriptpath"]"

# get directory holding this modulefile script and others
set modulefileroot "[file dirname "$scriptpath"]"

# get name of modulefile script we are loading
set modulefilename "[file tail "$scriptpath"]"

# determine modulefile script version
set modulefilever "[file dirname "$modulefileroot"]"
set modulefilever "[file tail "$modulefilever"]"

# point to component top-level root folder
set componentroot "[file dirname "$modulefileroot"]"
set componentroot "[file dirname "$componentroot"]"

# get component folder name
set componentname "[file tail "$componentroot"]"

# get oneAPI top-level root folder
set oneapiroot "[file dirname "$componentroot"]"

# On load print component name and version being loaded
if { [ module-info mode load ] } {
    puts stderr "Loading $modulefilename version $modulefilever"
}

# On remove print component name and version being removed
if { [ module-info mode ] == "unload" || [ module-info mode ] == "remove" }  {
    puts stderr "Removing $modulefilename version $modulefilever"
}

# ###### Component Specific env vars setup ###################################

module-whatis "Intel(R) MPI Library"

set                 topdir                 "$componentroot/$modulefilever"

setenv              I_MPI_ROOT            "$topdir"

set i_mpi_library_kind "release"

if { [info exists ::env(I_MPI_LIBRARY_KIND) ] } {
    switch "$::env(I_MPI_LIBRARY_KIND)" {
        "release" -
        "release_mt" -
        "debug" -
        "debug_mt" {
            set i_mpi_library_kind "$::env(I_MPI_LIBRARY_KIND)"
        }
        default {
        }
    }
}

prepend-path        CLASSPATH             "$topdir/lib/mpi.jar"
prepend-path        PATH                  "$topdir/bin"
prepend-path        LD_LIBRARY_PATH       "$topdir/lib/$i_mpi_library_kind:$topdir/lib"
prepend-path        LIBRARY_PATH          "$topdir/lib/$i_mpi_library_kind:$topdir/lib"
prepend-path        CPATH                 "$topdir/include"
prepend-path        MANPATH               "$topdir/man"

if { [info exists ::env(I_MPI_OFI_LIBRARY_INTERNAL) ] } {
    set i_mpi_ofi_library_internal $::env(I_MPI_OFI_LIBRARY_INTERNAL)
} else {
    set i_mpi_ofi_library_internal "yes"
}

switch "$i_mpi_ofi_library_internal" {
    "0" -
    "no" -
    "off" -
    "disable" {
    }
    default {
        setenv              FI_PROVIDER_PATH      "$topdir/libfabric/lib/prov:/usr/lib64/libfabric"

        prepend-path        PATH                  "$topdir/libfabric/bin"
        prepend-path        LD_LIBRARY_PATH       "$topdir/libfabric/lib"
        prepend-path        LIBRARY_PATH          "$topdir/libfabric/lib"
    }
}
