From 8f4b080542538c6d47a698955f4fac14126140e7 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 9 Feb 2021 15:49:11 +0100
Subject: [PATCH] ENH: preserve ThirdParty locations in env modules

---
 bin/tools/foamCreateModuleInclude | 59 ++++++++++++++++++++++++++-----
 1 file changed, 51 insertions(+), 8 deletions(-)

diff --git a/bin/tools/foamCreateModuleInclude b/bin/tools/foamCreateModuleInclude
index 1875a2f2c2e..73214c253b0 100755
--- a/bin/tools/foamCreateModuleInclude
+++ b/bin/tools/foamCreateModuleInclude
@@ -152,7 +152,7 @@ then
 
     # Check that it appears to be an OpenFOAM installation
     # could also check [ -d "$projectDir/META-INFO" ]
-    if [ -d "$projectDir" ] && [ -f "etc/bashrc" ]
+    if [ -d "$projectDir" ] && [ -f "$projectDir/etc/bashrc" ]
     then
         echo "Appears to be an OpenFOAM installation" 1>&2
     else
@@ -276,17 +276,52 @@ fi
 echo "Using openfoam: $WM_PROJECT_DIR" 1>&2
 echo "==> $moduleOutput" 1>&2
 
-# Remove user directories as being unreliable
+# Check if directory is the OpenFOAM or ThirdParty tree
+dirInTree()
+{
+    local dir="$1"
+    [ "${dir#$WM_PROJECT_DIR}" != "$dir" ] || \
+    [ "${dir#$WM_THIRD_PARTY_DIR}" != "$dir" ]
+}
+
+# User directories are entirely unreliable
+foamOldDirs="$FOAM_USER_APPBIN $FOAM_USER_LIBBIN"
 
-foamOldDirs="$ADIOS2_ARCH_PATH $BOOST_ARCH_PATH $CGAL_ARCH_PATH $FFTW_ARCH_PATH \
-$MESA_ARCH_PATH $LLVM_ARCH_PATH \
-$MPI_ARCH_PATH $SCOTCH_ARCH_PATH \
-$FOAM_SITE_APPBIN $FOAM_SITE_LIBBIN $WM_PROJECT_SITE \
-$FOAM_USER_APPBIN $FOAM_USER_LIBBIN"
+# Site locations must be within the OpenFOAM/ThirdParty tree
+for dir in "$WM_PROJECT_SITE" "$FOAM_SITE_APPBIN" "$FOAM_SITE_LIBBIN"
+do
+    if [ -d "$dir" ] && dirInTree "$dir"
+    then
+        continue
+    fi
+    # Discard non-directories or non-OpenFOAM/ThirdParty locations
+    foamOldDirs="$foamOldDirs $dir"
+done
+
+# Changes associated with the '[A-Z].*_ARCH_PATH' variables must be
+# within the OpenFOAM/ThirdParty tree
+# - avoids adding items that may have come from another module
+for envname in $(env | sed -n -e 's/^\(.*ARCH_PATH\)=.*$/\1/p')
+do
+    eval "dir=\$$envname" || continue
+    if [ -d "$dir" ] && dirInTree "$dir"
+    then
+        continue
+    fi
+    # Discard non-directories or non-OpenFOAM/ThirdParty locations
+    foamOldDirs="$foamOldDirs $dir"
+done
 
 if [ "${optParaview:-false}" != true ]
 then
-    foamOldDirs="$foamOldDirs $ParaView_DIR"
+    for dir in "$ParaView_DIR"
+    do
+        if [ -d "$dir" ] && dirInTree "$dir"
+        then
+            continue
+        fi
+        foamOldDirs="$foamOldDirs $ParaView_DIR"
+    done
 fi
 
 foamClean="$WM_PROJECT_DIR/bin/foamCleanPath"
@@ -336,6 +371,14 @@ unset FOAM_SITE_APPBIN FOAM_SITE_LIBBIN WM_PROJECT_SITE
 # Should work without (use default)
 unset WM_DIR
 
+# Remove any sourcing cruft
+# Remove: 'BASH_.*'
+
+for envname in $(env | sed -n -e 's/^\(BASH_.*\)=.*$/\1/p')
+do
+    eval "unset $envname"
+done
+
 # Third-party cruft - only used for orig compilation
 # - as per spack installation
 
-- 
GitLab