From 110b00f048cb64cebd917895bffc359d261c1e43 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 11 Jan 2018 01:30:23 +0100
Subject: [PATCH] ENH: improved handling of gmp/mpfr library settings (issue
 #674)

- export library path for gmp/mpfr from CGAL config files.
  This is required when non-system gmp/mpfr libraries are being
  used, but not using a ThirdParty compiler installation.

- automatically handle lib/ vs lib64/ (eg, for central installations)
  for packages such as boost, CGAL, etc. While the ThirdParty
  compilation of these will normally land in lib64/, this may not be
  the case when they are supplied by another means.

- reworked the handling of foamEtcFile and foamCleanPath for less
  clutter in the configuration files.
  Added the bin/tools/lib-dir script to handle logic that is
  too complex to easily manage in csh.
---
 bin/foamCleanPath                | 108 +++++++++++++++++----
 bin/foamEtcFile                  | 132 +++++++++++++++++--------
 bin/tools/lib-dir                | 162 +++++++++++++++++++++++++++++++
 etc/bashrc                       |  66 ++++++-------
 etc/config.csh/ADIOS             |   2 +-
 etc/config.csh/ADIOS2            |   2 +-
 etc/config.csh/CGAL              |  30 +++---
 etc/config.csh/FFTW              |  15 +--
 etc/config.csh/aliases           |   4 +-
 etc/config.csh/compiler          |   2 +-
 etc/config.csh/ensight           |   2 +-
 etc/config.csh/example/compiler  |   4 +-
 etc/config.csh/example/openmpi   |   3 +-
 etc/config.csh/example/paraview  |   3 +-
 etc/config.csh/example/prefs.csh |  11 +--
 etc/config.csh/functions         |  62 ++++++++++++
 etc/config.csh/mpi               |  48 ++++++---
 etc/config.csh/paraview          |  13 +--
 etc/config.csh/settings          |  34 +++----
 etc/config.csh/tcsh_completion   |   1 +
 etc/config.csh/unset             |  11 +--
 etc/config.csh/vtk               |   2 +-
 etc/config.sh/ADIOS              |   6 +-
 etc/config.sh/ADIOS2             |   4 +-
 etc/config.sh/CGAL               |  35 +++----
 etc/config.sh/FFTW               |  20 ++--
 etc/config.sh/aliases            |   6 +-
 etc/config.sh/bash_completion    |   1 +
 etc/config.sh/ccmio              |   2 +-
 etc/config.sh/compiler           |   2 +-
 etc/config.sh/ensight            |   2 +-
 etc/config.sh/example/compiler   |   4 +-
 etc/config.sh/example/openmpi    |   3 +-
 etc/config.sh/example/paraview   |   2 +-
 etc/config.sh/example/prefs.sh   |  13 +--
 etc/config.sh/functions          | 113 ++++++++++++++++++---
 etc/config.sh/gperftools         |   6 +-
 etc/config.sh/kahip              |   2 +-
 etc/config.sh/metis              |   2 +-
 etc/config.sh/mgridgen           |   2 +-
 etc/config.sh/mpi                |  54 +++++++----
 etc/config.sh/paraview           |  17 ++--
 etc/config.sh/scotch             |   2 +-
 etc/config.sh/settings           |  31 +++---
 etc/config.sh/unset              |  12 +--
 etc/config.sh/vtk                |   2 +-
 etc/cshrc                        |  78 ++++++---------
 47 files changed, 759 insertions(+), 379 deletions(-)
 create mode 100755 bin/tools/lib-dir
 create mode 100644 etc/config.csh/functions

diff --git a/bin/foamCleanPath b/bin/foamCleanPath
index 6049fa8bfea..1fb86152d52 100755
--- a/bin/foamCleanPath
+++ b/bin/foamCleanPath
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -15,6 +15,7 @@
 #
 # Description
 #     Usage: foamCleanPath [OPTION] path [wildcard] .. [wildcard]
+#            foamCleanPath [OPTION] -env=name [wildcard1] .. [wildcardN]
 #
 #     Prints its argument (which should be a ':' separated path)
 #     without the following:
@@ -23,24 +24,48 @@
 #         - inaccessible directories (with the -strip option)
 #
 # Note
-#     - this routine will fail when directories have embedded spaces
+#     - this routine fails when directories have embedded spaces
 #     - false matches possible if a wildcard contains '.' (sed regex)
 #     - the wildcards themselves can be written together and separated
 #       by colons or whitespace
+#
+# Examples for cleaning the path:
+#
+#     - Using explicit arguments
+#       cleaned=$(foamCleanPath "$PATH" dir1:dir2) && PATH=$cleaned
+#
+#     - Variable to clean passed as an option
+#       cleaned=$(foamCleanPath -env=PATH dir1:dir2) && PATH=$cleaned
+#
+#     - Using shell evaluation for the output
+#       eval $(foamCleanPath -sh=PATH "$PATH" dir1:dir2)
+#       eval $(foamCleanPath -sh=PATH -env=PATH dir1:dir2)
+#       eval $(foamCleanPath -sh-env=PATH dir1:dir2)
+#
+#     - Similarly for c-shell
+#       eval `foamCleanPath -csh-env=PATH dir1:dir2`
+#
 #------------------------------------------------------------------------------
 usage() {
     cat <<USAGE 1>&2
-Usage: ${0##*/} [OPTION] path [wildcard1] .. [wildcardN]
+Usage: foamCleanPath [OPTION] path [wildcard1] .. [wildcardN]]
+       foamCleanPath [OPTION] -env=name [wildcard1] .. [wildcardN]
+
 options:
+  -csh=NAME         Produce 'setenv NAME ...' output for csh eval
+  -sh=NAME          Produce 'NAME=...' output for sh eval
+  -csh-env=NAME     As per -csh, with -env for initial content
+  -sh-env=NAME      As per -sh,  with -env for initial content
+  -env=NAME         Evaluate NAME to obtain initial content
   -debug            print debug information to stderr
   -strip            remove inaccessible directories
   -verbose          report some progress (input, output, ...)
   -help             print the usage
 
 Prints its argument (which should be a ':' separated list) cleansed from
-  - duplicate elements
-  - elements whose start matches one of the wildcard(s)
-  - inaccessible directories (with the -strip option)
+  * duplicate elements
+  * elements whose start matches one of the wildcard(s)
+  * inaccessible directories (the -strip option)
 
 Exit status
     0  on success
@@ -51,15 +76,58 @@ USAGE
     exit 1
 }
 
+# Report error and exit
+die()
+{
+    exec 1>&2
+    echo
+    echo "Error encountered:"
+    while [ "$#" -ge 1 ]; do echo "    $1"; shift; done
+    echo
+    echo "See 'foamCleanPath -help' for usage"
+    echo
+    exit 1
+}
+
+#-------------------------------------------------------------------------------
+
+# Input and outputs
+unset dirList shellOutput
 
 # Parse options
-unset optDebug optStrip optVerbose
+unset optDebug optEnvName optStrip optVerbose
 while [ "$#" -gt 0 ]
 do
     case "$1" in
     -h | -help*)
         usage
         ;;
+    -csh=* | -sh=* | -csh-env=* | -sh-env=*)
+        name="${1#*=}"
+        [ -n "$name" ] || die "Option '$1' missing an ENVNAME"
+
+        # Output prefix
+        case "$1" in
+        -csh*)
+            shellOutput="setenv $name "     # eg, "setenv PATH xyz"
+            ;;
+        *)
+            shellOutput="$name="            # eg, "PATH=xyz"
+            ;;
+        esac
+
+        # For (-csh-env | -sh-env) also use name for input evaluation
+        case "$1" in
+        *-env=*)
+            optEnvName="$name"
+            ;;
+        esac
+        ;;
+    -env=*)
+        name="${1#*=}"
+        [ -n "$name" ] || die "Option '$1' missing an ENVNAME"
+        optEnvName="$name"
+        ;;
     -debug)
         optDebug=true
         ;;
@@ -76,11 +144,18 @@ do
     shift
 done
 
-# Basic checks, setup
-[ "$#" -ge 1 ] || usage
 
-dirList="$1"
-shift
+# Basic checks
+if [ -n "$optEnvName" ]
+then
+    eval "dirList=\$$optEnvName"
+elif [ "$#" -ge 1 ]
+then
+    dirList="$1"
+    shift
+else
+    die "Requires at least one argument, or use the -env option"
+fi
 
 [ -n "$dirList" ] || exit 2     # Quick exit on empty 'dirList'
 
@@ -105,8 +180,8 @@ fi
 # The "wildcard1 ... wildcardN" may have been passed as a single parameter
 # or may contain ':' separators
 
-oldIFS="$IFS"   # Preserve initial IFS
-IFS=': '        # Split on colon, whitespace
+oldIFS="$IFS"       # Preserve initial IFS
+IFS=': '            # Split on colon, whitespace
 set -- $*
 
 if [ -n "$optVerbose" ]
@@ -128,10 +203,10 @@ do
 done
 printDebug "intermediate>$dirList<"
 
-IFS=': '        # Split on colon, whitespace (to avoid surprises)
+IFS=': '            # Split on colon, whitespace (to avoid surprises)
 set -- $dirList
 
-IFS="$oldIFS"   # Restore initial IFS
+IFS="$oldIFS"       # Restore initial IFS
 
 # Rebuild the list
 unset dirList
@@ -153,12 +228,11 @@ do
 done
 
 printDebug "output>$dirList<"
-
 if [ -n "$optVerbose" ]
 then
     echo "output: $dirList" 1>&2
 fi
 
-echo "$dirList"
+echo "$shellOutput$dirList"
 
 #------------------------------------------------------------------------------
diff --git a/bin/foamEtcFile b/bin/foamEtcFile
index e61279b5913..ea1d1c3fbf2 100755
--- a/bin/foamEtcFile
+++ b/bin/foamEtcFile
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -56,10 +56,13 @@ options:
   -mode=MODE        Any combination of u(user), g(group), o(other)
   -prefix=DIR       Specify an alternative installation prefix
   -version=VER      Specify alternative OpenFOAM version (eg, 3.0, 1612, ...)
-  -csh              Produce output suitable for a csh or sh 'eval'
-  -csh-verbose      As per -csh with additional verbosity
-  -sh               Produce output suitable for a csh or sh 'eval'
-  -sh-verbose       As per -sh  with additional verbosity
+  -csh              Produce 'source FILE' output for a csh eval
+  -sh               Produce '. FILE' output for a sh eval
+  -csh-verbose      As per -csh, with additional verbosity
+  -sh-verbose       As per -sh,  with additional verbosity
+  -config           Add config directory prefix for shell type:
+                        with -csh* for a config.csh/ prefix
+                        with -sh*  for a config.sh/ prefix
   -quiet (-q)       Suppress all normal output
   -silent (-s)      Suppress stderr, except -csh-verbose, -sh-verbose output
   -help             Print the usage
@@ -81,7 +84,6 @@ USAGE
     exit 0  # A clean exit
 }
 
-
 unset optQuiet optSilent
 # Report error and exit
 die()
@@ -183,7 +185,8 @@ setVersion()
 
 
 optMode=ugo         # Default mode is always 'ugo'
-unset optAll optList optShell optVersion
+unset shellOutput verboseOutput
+unset optAll optConfig optList optVersion
 
 # Parse options
 while [ "$#" -gt 0 ]
@@ -194,19 +197,24 @@ do
         ;;
     -a | -all)
         optAll=true
-        unset optShell
+        unset shellOutput verboseOutput
         ;;
     -l | -list)
         optList=true
-        unset optShell
         ;;
     -list-test)
         optList='test'
-        unset optShell
         ;;
-    -csh | -sh | -csh-verbose | -sh-verbose)
-        optShell="${1#-}"
-        unset optAll
+    -csh | -sh)
+        shellOutput="${1#-}"
+        unset verboseOutput
+        ;;
+    -csh-verbose | -sh-verbose)
+        shellOutput="${1#-}"
+        verboseOutput="source "         # Report: "source FILE"
+        ;;
+    -config)
+        optConfig=true
         ;;
     -mode=[ugo]*)
         optMode="${1#*=}"
@@ -260,7 +268,6 @@ do
     shift
 done
 
-
 #-------------------------------------------------------------------------------
 
 if [ -n "$optVersion" ]
@@ -309,27 +316,61 @@ case "$optMode" in (*o*) # (O)ther == shipped
 esac
 set -- $dirList
 
+[ "$#" -ge 1 ] || die "No directories to scan. Programming error?"
+exitCode=2  # Fallback is a FileNotFound error
+
+
+#
+# Preliminaries
+#
+
+# Special handling of config.sh/ , config.csh/ directories
+if [ -n "$optConfig" -a -n "$shellOutput" -a -n "$fileName" ]
+then
+    case "$shellOutput" in
+    csh*)
+        optConfig="config.csh/"
+        ;;
+    sh*)
+        optConfig="config.sh/"
+        ;;
+    *)
+        unset optConfig
+        ;;
+    esac
+
+    if [ -n "$optConfig" ]
+    then
+        case "$fileName" in
+        /* | config.csh* | config.sh*)
+            # Does not need or cannot add a prefix
+            unset optConfig
+            ;;
+        *)
+            fileName="$optConfig$fileName"
+            ;;
+        esac
+    fi
+fi
+
 
 #
 # The main routine
 #
 
-exitCode=0
 if [ -n "$optList" ]
 then
 
     # List directories, or potential file locations
     [ "$nArgs" -le 1 ] || \
-    die "-list expects 0 or 1 filename, but $nArgs provided"
+        die "-list options expect 0 or 1 filename, but $nArgs provided"
 
-    # A silly combination, but -quiet does have precedence
+    # A silly combination, but -quiet has absolute precedence
     [ -n "$optQuiet" ] && exit 0
 
     # Test for directory or file too?
     if [ "$optList" = "test" ]
     then
-        exitCode=2  # Fallback to a general error (file not found)
-
         if [ "$nArgs" -eq 1 ]
         then
             for dir
@@ -352,6 +393,7 @@ then
             done
         fi
     else
+        exitCode=0  # OK, already verified that $# != 0
         for dir
         do
             echo "$dir${fileName:+/}$fileName"
@@ -362,35 +404,39 @@ else
 
     [ "$nArgs" -eq 1 ] || die "One filename expected - $nArgs provided"
 
-    exitCode=2  # Fallback to a general error (file not found)
+    # Output for sourcing files ("source" for csh, "." for POSIX shell)
+    # Only allow sourcing a single file (disallow combination with -all)
+    case "$shellOutput" in
+    csh*)
+        shellOutput="source "   # eg, "source FILE"
+        ;;
+    sh*)
+        shellOutput=". "        # eg, ". FILE"
+        ;;
+    esac
+
+    # Anti-pattern: -all disables shell commands
+    if [ -n "$optAll" ]
+    then
+        unset shellOutput verboseOutput
+    fi
 
     for dir
     do
-        if [ -f "$dir/$fileName" ]
+        resolved="$dir/$fileName"
+        if [ -f "$resolved" ]
         then
-            exitCode=0
-            [ -n "$optQuiet" ] && break
-
-            case "$optShell" in
-            (*verbose)
-                echo "Using: $dir/$fileName" 1>&2
-                ;;
-            esac
-
-            case "$optShell" in
-            csh*)
-                echo "source $dir/$fileName"
+            exitCode=0  # OK
+            if [ -n "$optQuiet" ]
+            then
                 break
-                ;;
-            sh*)
-                echo ". $dir/$fileName"
-                break
-                ;;
-            *)
-                echo "$dir/$fileName"
-                [ -n "$optAll" ] || break
-                ;;
-            esac
+            elif [ -n "$verboseOutput" ]
+            then
+                echo "$verboseOutput$resolved" 1>&2
+            fi
+
+            echo "$shellOutput$resolved"
+            [ -n "$optAll" ] || break
         fi
     done
 
diff --git a/bin/tools/lib-dir b/bin/tools/lib-dir
new file mode 100755
index 00000000000..a61d5748266
--- /dev/null
+++ b/bin/tools/lib-dir
@@ -0,0 +1,162 @@
+#!/bin/sh
+#------------------------------------------------------------------------------
+# =========                 |
+# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+#  \\    /   O peration     |
+#   \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+#    \\/     M anipulation  |
+#-------------------------------------------------------------------------------
+# License
+#     This file is part of OpenFOAM, licensed under GNU General Public License
+#     <http://www.gnu.org/licenses/>.
+#
+# Script
+#     tools/lib-dir [OPTION] DIR [LIBEXT]
+#
+# Description
+#     Since csh/tcsh doesn't have functions, this script can be used to manage
+#     slightly more complex logic.
+#
+#     Resolves for the existence of DIR/lib64 and DIR/lib, or uses the fallback
+#     LIBEXT if these failed. A DIR ending in "-none" or "-system" is skipped.
+#
+#     output -csh:     setenv LD_LIBRARY_PATH dir/lib:$LD_LIBRARY_PATH
+#     output -make:    -Ldir/lib
+#     output -sh:      LD_LIBRARY_PATH=dir/lib:$LD_LIBRARY_PATH
+#
+#------------------------------------------------------------------------------
+printHelp() {
+    cat<<USAGE
+
+Usage: ${0##*/} [OPTION] DIR [LIBEXT]
+
+options:
+   -sh               Emit POSIX shell syntax (default)
+   -csh              Emit C-shell shell syntax
+   -make             Emit content for a makefile
+   -help             Print the usage
+
+Resolves for the existence of DIR/lib64 and DIR/lib, or uses the fallback
+LIBEXT if these failed. A DIR ending in "-none" or "-system" is skipped.
+
+With -sh             LD_LIBRARY_PATH=dir/lib:$LD_LIBRARY_PATH
+With -csh            setenv LD_LIBRARY_PATH dir/lib:$LD_LIBRARY_PATH
+With -make           -Ldir/lib
+
+Exit status is zero (success) or non-zero (failure)
+USAGE
+    exit 0  # A clean exit
+}
+
+# Report error and exit
+die()
+{
+    exec 1>&2
+    echo
+    echo "Error encountered:"
+    while [ "$#" -ge 1 ]; do echo "    $1"; shift; done
+    echo
+    echo "See '${Script##*/} -help' for usage"
+    echo
+    exit 1
+}
+
+
+#------------------------------------------------------------------------------
+
+optSyntax=sh
+
+# Parse options
+while [ "$#" -gt 0 ]
+do
+    case "$1" in
+    -h | -help*)
+        printHelp
+        ;;
+    -csh | -sh | -make)
+        optSyntax="${1#-}"
+        ;;
+    --)
+        shift
+        break
+        ;;
+    -*)
+        die "unknown option: '$1'"
+        ;;
+    *)
+        break
+        ;;
+    esac
+    shift
+done
+
+#------------------------------------------------------------------------------
+
+dir="$1"   # $1 = base directory for 'lib' or 'lib64'
+alt="$2"   # $2 = fallback libname ('lib' or 'lib64')
+
+unset resolved
+
+# 0)
+# Skip entirely if directory ends in "-none" or "-system".
+# These special cases (disabled, system directories) should not require
+# adjustment of LD_LIBRARY_PATH
+
+case "$dir" in
+none |  system  | *-none | *-system)
+    unset dir
+    ;;
+esac
+
+if [ -z "$dir" ]
+then
+    exit 1
+elif [ -d "$dir" ]
+then
+    # 1) Check for dir/lib64 and dir/lib
+    for end in lib$WM_COMPILER_LIB_ARCH lib
+    do
+        if [ -d "$dir/$end" ]
+        then
+            resolved=$dir/$end
+            break
+        fi
+    done
+fi
+
+# 2) Use fallback if the previous failed
+if [ -z "$resolved" -a -n "$alt" ]
+then
+    # Fallback
+    case "$alt" in
+    /*)
+        resolved=$alt
+        ;;
+    (*)
+        resolved=$dir/$alt
+        ;;
+    esac
+    return 0
+fi
+
+
+if [ -n "$resolved" ]
+then
+    case "$optSyntax" in
+    csh)
+        echo "setenv LD_LIBRARY_PATH $resolved:$LD_LIBRARY_PATH"
+        ;;
+    make)
+        printf "%s\n" "-L$resolved"
+        ;;
+    *)
+        echo "LD_LIBRARY_PATH=$resolved:$LD_LIBRARY_PATH"
+        ;;
+    esac
+    exit 0      # Good
+else
+    exit 1    # Error
+fi
+
+
+#------------------------------------------------------------------------------
diff --git a/etc/bashrc b/etc/bashrc
index b1a39aaed39..c7f8b7eb771 100644
--- a/etc/bashrc
+++ b/etc/bashrc
@@ -139,65 +139,55 @@ _foamEtc -mode=ug prefs.sh
 export FOAM_SETTINGS="$@"
 _foamEval $@
 
-# Clean standard environment variables (PATH, LD_LIBRARY_PATH, MANPATH)
+# Clean standard environment variables (PATH, MANPATH, LD_LIBRARY_PATH)
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-foamClean=$WM_PROJECT_DIR/bin/foamCleanPath
-
-# Clean PATH
-cleaned=$($foamClean "$PATH" "$foamOldDirs") && PATH="$cleaned"
-
-# Clean LD_LIBRARY_PATH
-cleaned=$($foamClean "$LD_LIBRARY_PATH" "$foamOldDirs") \
-    && LD_LIBRARY_PATH="$cleaned"
-
-# Clean MANPATH
-cleaned=$($foamClean "$MANPATH" "$foamOldDirs") && MANPATH="$cleaned"
-
-export PATH LD_LIBRARY_PATH MANPATH
-
+export PATH MANPATH LD_LIBRARY_PATH
+_foamClean PATH "$foamOldDirs"
+_foamClean MANPATH "$foamOldDirs"
+_foamClean LD_LIBRARY_PATH "$foamOldDirs"
 
 # Setup for OpenFOAM compilation etc
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-_foamEtc config.sh/settings
+_foamEtc -config  settings
 
 # Setup for third-party packages
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-_foamEtc config.sh/mpi
-_foamEtc config.sh/paraview
-_foamEtc config.sh/vtk
-_foamEtc config.sh/ensight
-_foamEtc config.sh/gperftools
-## _foamEtc config.csh/ADIOS
-_foamEtc config.sh/CGAL
-_foamEtc config.sh/scotch
-_foamEtc config.sh/FFTW
+_foamEtc -config  mpi
+_foamEtc -config  paraview
+_foamEtc -config  vtk
+_foamEtc -config  ensight
+_foamEtc -config  gperftools
+## _foamEtc -config  ADIOS
+_foamEtc -config  CGAL
+_foamEtc -config  scotch
+_foamEtc -config  FFTW
 
 # Interactive shell
 if /usr/bin/tty -s 2>/dev/null
 then
-    _foamEtc config.sh/aliases
-    [ "${BASH_VERSINFO:-0}" -ge 4 ] && _foamEtc config.sh/bash_completion
+    _foamEtc -config  aliases
+    [ "${BASH_VERSINFO:-0}" -ge 4 ] && _foamEtc -config  bash_completion
 fi
 
 
 # Clean environment paths again. Only remove duplicates
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-# Clean PATH
-cleaned=$($foamClean "$PATH") && PATH="$cleaned"
-
-# Clean LD_LIBRARY_PATH
-cleaned=$($foamClean "$LD_LIBRARY_PATH") && LD_LIBRARY_PATH="$cleaned"
+export PATH MANPATH LD_LIBRARY_PATH
 
-# Clean MANPATH (trailing ':' to find system pages)
-cleaned=$($foamClean "$MANPATH") && MANPATH="${cleaned}:"
+_foamClean PATH
+_foamClean MANPATH
+_foamClean LD_LIBRARY_PATH
 
-export PATH LD_LIBRARY_PATH MANPATH
+# Add trailing ':' for system manpages
+if [ -n "$MANPATH" ]
+then
+    MANPATH="${MANPATH}:"
+fi
 
-# Clean LD_PRELOAD
 if [ -n "$LD_PRELOAD" ]
 then
-    cleaned=$($foamClean "$LD_PRELOAD") && LD_PRELOAD="$cleaned"
     export LD_PRELOAD
+    _foamClean LD_PRELOAD
 fi
 
 
@@ -208,6 +198,6 @@ fi
 . $WM_PROJECT_DIR/etc/config.sh/functions
 
 # Variables (done as the last statement for a clean exit code)
-unset cleaned foamClean foamOldDirs
+unset cleaned foamOldDirs
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/ADIOS b/etc/config.csh/ADIOS
index 7ef241af600..bc7a7759e7d 100644
--- a/etc/config.csh/ADIOS
+++ b/etc/config.csh/ADIOS
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/ADIOS
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
 #     Setup for ADIOS include/libraries (usually ThirdParty installation).
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc
 #
 #     To disable its use:               adios_version=adios-none
 #     For system-wide installations:    adios_version=adios-system
diff --git a/etc/config.csh/ADIOS2 b/etc/config.csh/ADIOS2
index a78d06a7528..b375cd8ea41 100644
--- a/etc/config.csh/ADIOS2
+++ b/etc/config.csh/ADIOS2
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/ADIOS2
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
 #     Setup for ADIOS2 include/libraries (usually ThirdParty installation).
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc
 #
 #------------------------------------------------------------------------------
 # USER EDITABLE PART: Changes made here may be lost with the next upgrade
diff --git a/etc/config.csh/CGAL b/etc/config.csh/CGAL
index d4190c3a974..52b50704573 100644
--- a/etc/config.csh/CGAL
+++ b/etc/config.csh/CGAL
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/CGAL
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
-#     Setup file for CGAL (& boost) include/libraries.
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc
+#     Setup CGAL (& boost) include/libraries (usually ThirdParty installation).
 #
 #     To disable its use:
 #         boost_version=boost-none
@@ -29,6 +29,9 @@
 #       2. and provide full paths for BOOST_ARCH_PATH / CGAL_ARCH_PATH
 #
 # Note
+#     Define GMP_ARCH_PATH and MPFR_ARCH_PATH here, if required and when not
+#     using a ThirdParty gcc.
+#
 #     Changes made here MUST be made in the equivalent config.sh version too,
 #     since that is the one used in the build process.
 #
@@ -50,20 +53,19 @@ if ($?FOAM_VERBOSE && $?prompt) then
     echo "Using CGAL ($cgal_version)  ->  $CGAL_ARCH_PATH"
 endif
 
-# If BOOST_ARCH_PATH, CGAL_ARCH_PATH do not end with '-system' or '-none',
-# they are either located within ThirdParty, or a central installation
-# outside of ThirdParty and must be added to the lib-path.
+_foamAddLibAuto $BOOST_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
+_foamAddLibAuto $CGAL_ARCH_PATH   lib$WM_COMPILER_LIB_ARCH
 
-set ending="${BOOST_ARCH_PATH:t}"
-if ( "$ending" != "boost-none" && "$ending" != "boost-system" ) then
-    _foamAddLib $BOOST_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
-endif
+# GMP/MPFR may have already been added with ThirdParty compiler, but cannot
+# be certain so add here. If they are duplicates, they will be removed later.
 
-set ending="${CGAL_ARCH_PATH:t}"
-if ( "$ending" != "cgal-none" && "$ending" != "cgal-system" ) then
-    _foamAddLib $CGAL_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
+if ( $?GMP_ARCH_PATH ) then
+    _foamAddLibAuto $GMP_ARCH_PATH   # No fallback libdir
+endif
+if ( $?MPFR_ARCH_PATH ) then
+    _foamAddLibAuto $MPFR_ARCH_PATH  # No fallback libdir
 endif
 
-unset boost_version cgal_version ending
+unset boost_version cgal_version
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/FFTW b/etc/config.csh/FFTW
index 7cb78bb9430..2ac3bbc736d 100644
--- a/etc/config.csh/FFTW
+++ b/etc/config.csh/FFTW
@@ -2,7 +2,7 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
+#   \\  /    A nd           | Copyright (C) 2016-2018 OpenCFD Ltd.
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 # License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/FFTW
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup for FFTW include/libraries (usually ThirdParty installation).
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #     To disable its use:               fftw_version=fftw-none
 #     For system-wide installations:    fftw_version=fftw-system
@@ -41,15 +41,8 @@ if ($?FOAM_VERBOSE && $?prompt) then
     echo "Using fftw ($fftw_version)  ->  $FFTW_ARCH_PATH"
 endif
 
-# If FFTW_ARCH_PATH does not end with '-system' or '-none',
-# it is either located within ThirdParty, or a central installation
-# outside of ThirdParty and must be added to the lib-path.
+_foamAddLibAuto $FFTW_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
 
-set ending="${FFTW_ARCH_PATH:t}"
-if ( "$ending" != "fftw-none" && "$ending" != "fftw-system" ) then
-    _foamAddLib $FFTW_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
-endif
-
-unset fftw_version ending
+unset fftw_version
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/aliases b/etc/config.csh/aliases
index 31afd9850eb..1f63a50900e 100644
--- a/etc/config.csh/aliases
+++ b/etc/config.csh/aliases
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/aliases
+#     - sourced by OpenFOAM-*/etc/cshrc  (or from the user's ~/.cshrc)
 #
 # Description
 #     Aliases for working with OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc and/or ~/.cshrc
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.csh/compiler b/etc/config.csh/compiler
index 6b0a286b12b..3543d732c53 100644
--- a/etc/config.csh/compiler
+++ b/etc/config.csh/compiler
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/compiler
+#     - sourced by OpenFOAM-*/etc/config.csh/settings
 #
 # Description
 #     Setup for custom compiler versions for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/config.csh/settings
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.csh/ensight b/etc/config.csh/ensight
index b294eeade3a..a840cbda12e 100644
--- a/etc/config.csh/ensight
+++ b/etc/config.csh/ensight
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/ensight
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
 #     Setup for ENSIGHT
-#     Sourced from OpenFOAM-*/etc/cshrc
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.csh/example/compiler b/etc/config.csh/example/compiler
index ebc90c35b15..607f780fff1 100644
--- a/etc/config.csh/example/compiler
+++ b/etc/config.csh/example/compiler
@@ -11,15 +11,15 @@
 #
 # File
 #     config.csh/example/compiler
+#     - sourced by OpenFOAM-*/etc/config.csh/settings
 #
 # Description
 #     Example of fine tuning ThirdParty compiler settings for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/config.csh/settings
 #
 #------------------------------------------------------------------------------
 
 # Load the standard versions
-eval `$WM_PROJECT_DIR/bin/foamEtcFile -csh -mode=o config.csh/compiler`
+eval `$WM_PROJECT_DIR/bin/foamEtcFile -mode=o -csh -config compiler`
 
 # Modify/override compiler settings
 switch ("$WM_COMPILER")
diff --git a/etc/config.csh/example/openmpi b/etc/config.csh/example/openmpi
index a23e951db9b..ae995100f4a 100644
--- a/etc/config.csh/example/openmpi
+++ b/etc/config.csh/example/openmpi
@@ -11,14 +11,13 @@
 #
 # File
 #     config.csh/example/openmpi
+#     - sourced by OpenFOAM-*/etc/config.csh/mpi
 #
 # Description
 #     Example of fine tuning openmpi settings for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/config.csh/settings
 #
 #------------------------------------------------------------------------------
 
-# Modified openmpi settings
 setenv FOAM_MPI openmpi-3.0.0
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/example/paraview b/etc/config.csh/example/paraview
index a2211ec2ec3..ce07cea7c25 100644
--- a/etc/config.csh/example/paraview
+++ b/etc/config.csh/example/paraview
@@ -22,8 +22,7 @@
 #
 #------------------------------------------------------------------------------
 
-# Use shipped paraview config file (-mode=o) with a different ParaView_VERSION
-
+# Use standard paraview config file (-mode=o) with a different ParaView_VERSION
 set foamFile=`$WM_PROJECT_DIR/bin/foamEtcFile -mode=o config.csh/paraview`
 if ( $status == 0 ) source $foamFile ParaView_VERSION=5.4.0
 
diff --git a/etc/config.csh/example/prefs.csh b/etc/config.csh/example/prefs.csh
index 2d688b587b3..daa27f80de8 100644
--- a/etc/config.csh/example/prefs.csh
+++ b/etc/config.csh/example/prefs.csh
@@ -11,12 +11,10 @@
 #
 # File
 #     config.csh/example/prefs.csh
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
-#     Preset variables for the OpenFOAM configuration - C-Shell shell syntax.
-#
-#     The prefs.csh file will be sourced by the OpenFOAM etc/cshrc when it is
-#     found by foamEtcFile.
+#     Example of preset variables for the OpenFOAM configuration (C-Shell shell)
 #
 # See also
 #     'foamEtcFile -help' or 'foamEtcFile -list' for information about the
@@ -24,13 +22,8 @@
 #
 #------------------------------------------------------------------------------
 
-#- Compiler location:
 setenv WM_COMPILER_TYPE ThirdParty
-
-#- Compiler:
 setenv WM_COMPILER Clang
-
-#- MPI implementation:
 setenv WM_MPLIB SYSTEMOPENMPI
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/functions b/etc/config.csh/functions
new file mode 100644
index 00000000000..cd038150587
--- /dev/null
+++ b/etc/config.csh/functions
@@ -0,0 +1,62 @@
+#----------------------------------*-sh-*--------------------------------------
+# =========                 |
+# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+#  \\    /   O peration     |
+#   \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+#    \\/     M anipulation  |
+#------------------------------------------------------------------------------
+# License
+#     This file is part of OpenFOAM, licensed under GNU General Public License
+#     <http://www.gnu.org/licenses/>.
+#
+# File
+#     etc/config.csh/functions
+#     - sourced by OpenFOAM-*/etc/cshrc
+#
+# Description
+#     C-shell aliases and variables used when sourcing the OpenFOAM environment
+#
+#     Some functionality implemented via bin/tools/lib-dir
+#
+#------------------------------------------------------------------------------
+
+# Cleaning environment variables
+alias _foamClean 'eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-env=\!*`'
+
+# Prepend PATH, MANPATH, LD_LIBRARY_PATH
+alias _foamAddPath 'setenv PATH \!*\:${PATH}'
+alias _foamAddMan  'setenv MANPATH \!*\:${MANPATH}'
+alias _foamAddLib  'setenv LD_LIBRARY_PATH \!*\:${LD_LIBRARY_PATH}'
+
+# Prefix to LD_LIBRARY_PATH with additional checking
+# $1 = base directory for 'lib' or 'lib64'
+# $2 = fallback libname ('lib' or 'lib64')
+alias _foamAddLibAuto 'eval `$WM_PROJECT_DIR/bin/tools/lib-dir -csh \!*`'
+
+# Source an etc file, possibly with some verbosity
+if ($?FOAM_VERBOSE && $?prompt) then
+    alias _foamEtc 'eval `$WM_PROJECT_DIR/bin/foamEtcFile -csh-verbose \!*`'
+else
+    alias _foamEtc 'eval `$WM_PROJECT_DIR/bin/foamEtcFile -csh \!*`'
+endif
+
+
+#------------------------------------------------------------------------------
+# Avoid any ThirdParty settings that may have 'leaked' into the environment
+
+unsetenv MPI_ARCH_PATH
+unsetenv ADIOS_ARCH_PATH
+unsetenv ADIOS1_ARCH_PATH
+unsetenv ADIOS2_ARCH_PATH
+unsetenv BOOST_ARCH_PATH
+unsetenv CCMIO_ARCH_PATH
+unsetenv CGAL_ARCH_PATH
+unsetenv FFTW_ARCH_PATH
+unsetenv GPERFTOOLS_ARCH_PATH
+unsetenv GMP_ARCH_PATH
+unsetenv MPFR_ARCH_PATH
+unsetenv MESA_ARCH_PATH
+unsetenv METIS_ARCH_PATH
+unsetenv SCOTCH_ARCH_PATH
+
+#------------------------------------------------------------------------------
diff --git a/etc/config.csh/mpi b/etc/config.csh/mpi
index 581bf0274bf..795b62f4c81 100644
--- a/etc/config.csh/mpi
+++ b/etc/config.csh/mpi
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,16 @@
 #
 # File
 #     etc/config.csh/mpi
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
 #     Setup for MPI communications library for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc
+#
+#     User adjustments are possible in these files:
+#       - config.csh/openmpi-system
+#       - config.csh/openmpi
+#       - config.csh/mpi-user
+#       - config.csh/mpi-system
 #
 #     For USERMPI, the user is responsible for supplying an appropriate
 #     wmake/rules/General/mplibUSERMPI file and managing all settings
@@ -25,25 +31,35 @@ setenv FOAM_MPI dummy  # Fallback value
 
 switch ("$WM_MPLIB")
 case SYSTEMOPENMPI:
-    # Use the system installed openmpi, get library directory via mpicc
+    # The system installed openmpi, locations discovery via mpicc.
     setenv FOAM_MPI openmpi-system
+    _foamEtc -config openmpi-system             # <- Adjustments (optional)
 
-    # Bit of a hack: strip off 'lib' and assume it is the prefix for openmpi
-    # include files and libraries.
-    set libDir=`mpicc --showme:link | sed -e 's/.*-L\([^ ]*\).*/\1/'`
+    # Respect MPI_ARCH_PATH if set to a valid directory (ie, from user adjustments)
+    if (! $?MPI_ARCH_PATH ) setenv MPI_ARCH_PATH
+    if ( -d "$MPI_ARCH_PATH" ) then
+        _foamAddLibAuto $MPI_ARCH_PATH
+    else
+        # Slight hack: strip off 'lib' to get presumed prefix for include and libs
+        set libDir=`mpicc --showme:link | sed -e 's/.*-L\([^ ]*\).*/\1/'`
 
-    setenv MPI_ARCH_PATH "${libDir:h}"
-    _foamAddLib     $libDir
-    unset libDir
+        setenv MPI_ARCH_PATH "${libDir:h}"
+        _foamAddLib $libDir
+        unset libDir
+    endif
     breaksw
 
 case OPENMPI:
     setenv FOAM_MPI openmpi-1.10.4
-    _foamEtc config.csh/openmpi                 # <- Adjustments (optional)
+    _foamEtc -config openmpi                    # <- Adjustments (optional)
 
-    setenv MPI_ARCH_PATH $WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/$FOAM_MPI
+    # Respect MPI_ARCH_PATH if set to a valid directory (ie, from user adjustments)
+    if (! $?MPI_ARCH_PATH ) setenv MPI_ARCH_PATH
+    if (! -d "$MPI_ARCH_PATH" ) then
+        setenv mpiDir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/$FOAM_MPI
+    endif
 
-    # Tell OpenMPI where to find its install directory
+    # Inform openmpi where to find its install directory
     setenv OPAL_PREFIX $MPI_ARCH_PATH
 
     if ($?FOAM_VERBOSE && $?prompt) then
@@ -53,19 +69,19 @@ case OPENMPI:
     endif
 
     _foamAddPath    $MPI_ARCH_PATH/bin
-    _foamAddLib     $MPI_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
     _foamAddMan     $MPI_ARCH_PATH/share/man
+    _foamAddLibAuto $MPI_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
     breaksw
 
 case USERMPI:
     # Use an arbitrary, user-specified mpi implementation
     setenv FOAM_MPI mpi-user
-    _foamEtc config.csh/mpi-user                # <- Adjustments
+    _foamEtc -config mpi-user                   # <- Adjustments (optional)
     breaksw
 
 case SYSTEMMPI:
     setenv FOAM_MPI mpi-system
-    _foamEtc config.csh/mpi-system              # <- Adjustments (optional)
+    _foamEtc -config mpi-system                 # <- Adjustments (optional)
 
     if ( ! $?MPI_ROOT ) then
         echo
@@ -114,8 +130,8 @@ case MPICH:
     setenv MPI_HOME $MPI_ARCH_PATH
 
     _foamAddPath    $MPI_ARCH_PATH/bin
-    _foamAddLib     $MPI_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
     _foamAddMan     $MPI_ARCH_PATH/share/man
+    _foamAddLibAuto $MPI_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
     breaksw
 
 case MPICH-GM:
diff --git a/etc/config.csh/paraview b/etc/config.csh/paraview
index 42a8f9a5884..4282eb21a76 100644
--- a/etc/config.csh/paraview
+++ b/etc/config.csh/paraview
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     config.csh/paraview
+#     - sourced by OpenFOAM-*/etc/cshrc or via foamPV alias
 #
 # Description
 #     Setup for PARAVIEW (partially cmake, qt too)
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc or from foamPV alias
 #
 #     For system-wide cmake:            cmake_version=cmake-system
 #     For system-wide qt:               ParaView_QT=qt-system
@@ -54,13 +54,8 @@ if (! $?WM_COMPILER_LIB_ARCH ) setenv WM_COMPILER_LIB_ARCH
 set archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"
 
 # Clean PATH and LD_LIBRARY_PATH
-set cleaned=`$WM_PROJECT_DIR/bin/foamCleanPath "$PATH" "$ParaView_DIR $archDir/cmake- $archDir/qt- $archDir/ParaView-"`
-if ( $status == 0 ) setenv PATH $cleaned
-
-if ( $?LD_LIBRARY_PATH ) then
-    set cleaned=`$WM_PROJECT_DIR/bin/foamCleanPath "$LD_LIBRARY_PATH" "$ParaView_DIR $archDir/qt- $archDir/ParaView-"`
-    if ( $status == 0 ) setenv LD_LIBRARY_PATH $cleaned
-endif
+eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-env=PATH "$ParaView_DIR $archDir/ParaView- $archDir/qt- $archDir/cmake-"`
+eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-env=LD_LIBRARY_PATH "$ParaView_DIR $archDir/ParaView- $archDir/qt-"`
 
 # ThirdParty cmake
 set cmake=$archDir/$cmake_version
diff --git a/etc/config.csh/settings b/etc/config.csh/settings
index 8b7d27e2a38..73cba45fc1e 100644
--- a/etc/config.csh/settings
+++ b/etc/config.csh/settings
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,19 +11,11 @@
 #
 # File
 #     etc/config.csh/settings
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
-#     Settings for OpenFOAM, sourced from OpenFOAM-<VERSION>/etc/cshrc
+#     Settings for OpenFOAM
 #
-#------------------------------------------------------------------------------
-
-# Prefix to PATH
-alias _foamAddPath 'setenv PATH \!*\:${PATH}'
-# Prefix to LD_LIBRARY_PATH
-alias _foamAddLib 'setenv LD_LIBRARY_PATH \!*\:${LD_LIBRARY_PATH}'
-# Prefix to MANPATH
-alias _foamAddMan 'setenv MANPATH \!*\:${MANPATH}'
-
 #------------------------------------------------------------------------------
 setenv WM_ARCH `uname -s`                   # System name
 if (! $?WM_OSTYPE ) setenv WM_OSTYPE POSIX  # System type (POSIX is default)
@@ -228,7 +220,7 @@ unsetenv GMP_ARCH_PATH MPFR_ARCH_PATH
 
 # Load pre-defined compiler versions
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-_foamEtc config.csh/compiler
+_foamEtc -config compiler
 
 # ThirdParty base for compilers
 set archDir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER_ARCH
@@ -263,19 +255,19 @@ GCC_NOT_FOUND
     # Add ThirdParty compiler libraries to run-time environment
     _foamAddLib     $gccDir/lib$WM_COMPILER_LIB_ARCH
 
-    # Add ThirdParty gmp/mpfr/mpc libraries to run-time environment
+    # Add gmp/mpfr/mpc libraries to run-time environment.
+    # Require that they exist, automatically find lib64/ or lib/.
     if ( "${gmpDir:t}" != "gmp-system" ) then
-        _foamAddLib $gmpDir/lib$WM_COMPILER_LIB_ARCH
-        setenv GMP_ARCH_PATH $gmpDir        # For ThirdParty CGAL
+        _foamAddLibAuto $gmpDir
+        setenv GMP_ARCH_PATH $gmpDir    # For non-system CGAL
     endif
     if ( "${mpfrDir:t}" != "mpfr-system" ) then
-        _foamAddLib $mpfrDir/lib$WM_COMPILER_LIB_ARCH
-        setenv MPFR_ARCH_PATH $mpfrDir      # For ThirdParty CGAL
-    endif
-    if ( "${mpcDir:t}" != "mpc-system" ) then
-        _foamAddLib $mpcDir/lib$WM_COMPILER_LIB_ARCH
+        _foamAddLibAuto $mpfrDir
+        setenv MPFR_ARCH_PATH $mpfrDir  # For non-system CGAL
     endif
 
+    _foamAddLibAuto $mpcDir
+
     if ($?FOAM_VERBOSE && $?prompt) then
         echo "Using ThirdParty compiler"
         echo "    ${gccDir:t} (${gmpDir:t} ${mpfrDir:t} ${mpcDir:t})"
@@ -332,6 +324,6 @@ unset archDir
 unset gcc_version gccDir
 unset gmp_version gmpDir  mpfr_version mpfrDir  mpc_version mpcDir
 unset clang_version clangDir
-# Retain: _foamAddPath _foamAddLib _foamAddMan
+# Retain: _foamAddPath _foamAddLib _foamAddMan _foamAddLibAuto
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/tcsh_completion b/etc/config.csh/tcsh_completion
index f183e83387b..569353f1081 100644
--- a/etc/config.csh/tcsh_completion
+++ b/etc/config.csh/tcsh_completion
@@ -11,6 +11,7 @@
 #
 # File
 #     etc/config.csh/tcsh_completion
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
 #     Tcsh completions for OpenFOAM applications
diff --git a/etc/config.csh/unset b/etc/config.csh/unset
index 9d62c3ac387..818a405753d 100644
--- a/etc/config.csh/unset
+++ b/etc/config.csh/unset
@@ -114,7 +114,7 @@ unsetenv PV_PLUGIN_PATH
 unsetenv VTK_DIR
 
 #------------------------------------------------------------------------------
-# unset other ThirdParty environment variables
+# Unset other ThirdParty environment variables
 
 unsetenv ADIOS_ARCH_PATH
 unsetenv ADIOS1_ARCH_PATH
@@ -136,18 +136,15 @@ unsetenv SCOTCH_ARCH_PATH
 
 if ( $?foamClean ) then
 
-    set cleaned=`$foamClean "$PATH" "$foamOldDirs"`
-    if ( $status == 0 ) setenv PATH $cleaned
+    eval `$foamClean -csh-env=PATH "$foamOldDirs"`
 
     if ($?LD_LIBRARY_PATH) then
-        set cleaned=`$foamClean "$LD_LIBRARY_PATH" "$foamOldDirs"`
-        if ( $status == 0 ) setenv LD_LIBRARY_PATH $cleaned
+        eval `$foamClean -csh-env=LD_LIBRARY_PATH "$foamOldDirs"`
         if ( ${%LD_LIBRARY_PATH} == 0 ) unsetenv LD_LIBRARY_PATH
     endif
 
     if ($?MANPATH) then
-        set cleaned=`$foamClean "$MANPATH" "$foamOldDirs"`
-        if ( $status == 0 ) setenv MANPATH $cleaned
+        eval `$foamClean -csh-env=MANPATH "$foamOldDirs"`
         if ( ${%MANPATH} == 0 ) unsetenv MANPATH
     endif
 
diff --git a/etc/config.csh/vtk b/etc/config.csh/vtk
index 1c660cfbd66..0e027e6d5cd 100644
--- a/etc/config.csh/vtk
+++ b/etc/config.csh/vtk
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.csh/vtk
+#     - sourced by OpenFOAM-*/etc/cshrc
 #
 # Description
 #     Setup file for VTK (and MESA)
-#     Sourced from OpenFOAM-<VERSION>/etc/cshrc
 #
 #     The library path is only adjusted when the paths specified here
 #     actually exist at the time of sourcing.
diff --git a/etc/config.sh/ADIOS b/etc/config.sh/ADIOS
index 92d58a02e6d..54534209ddf 100644
--- a/etc/config.sh/ADIOS
+++ b/etc/config.sh/ADIOS
@@ -2,7 +2,7 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
+#   \\  /    A nd           | Copyright (C) 2016-2018 OpenCFD Ltd.
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 # License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/ADIOS
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup for ADIOS include/libraries (usually ThirdParty installation).
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #     To disable its use:               adios_version=adios-none
 #     For system-wide installations:    adios_version=adios-system
@@ -42,7 +42,7 @@ then
     echo "Using adios ($adios_version)  ->  $ADIOS_ARCH_PATH" 1>&2
 fi
 
-if command -v _foamAddPath >/dev/null 2>&1  # normal sourcing
+if command -v _foamAddPath >/dev/null 2>&1      # Normal sourcing
 then
     # If ADIOS_ARCH_PATH does not end with '-system' or '-none',
     # it is located within ThirdParty, or a central installation
diff --git a/etc/config.sh/ADIOS2 b/etc/config.sh/ADIOS2
index 30677cf644f..8fe60b0a117 100644
--- a/etc/config.sh/ADIOS2
+++ b/etc/config.sh/ADIOS2
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/ADIOS2
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup for ADIOS2 include/libraries (usually ThirdParty installation).
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #------------------------------------------------------------------------------
 # USER EDITABLE PART: Changes made here may be lost with the next upgrade
@@ -29,7 +29,7 @@ then
     echo "Using adios ($adios2_version)  ->  $ADIOS2_ARCH_PATH" 1>&2
 fi
 
-if command -v _foamAddPath >/dev/null 2>&1  # normal sourcing
+if command -v _foamAddPath >/dev/null 2>&1      # Normal sourcing
 then
     # If *_ARCH_PATH does not end with '-system' or '-none',
     # it is located within ThirdParty, or a central installation
diff --git a/etc/config.sh/CGAL b/etc/config.sh/CGAL
index abec7c04b3b..aaa5470b0be 100644
--- a/etc/config.sh/CGAL
+++ b/etc/config.sh/CGAL
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/CGAL
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
-#     Setup file for CGAL (& boost) include/libraries.
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
+#     Setup CGAL (& boost) include/libraries (usually ThirdParty installation).
 #
 #     To disable its use:
 #         boost_version=boost-none
@@ -29,7 +29,10 @@
 #       2. and provide full paths for BOOST_ARCH_PATH / CGAL_ARCH_PATH
 #
 # Note
-#     When _foamAddLib is unset (eg, called from makeCGAL):
+#     Define GMP_ARCH_PATH and MPFR_ARCH_PATH here, if required and when not
+#     using a ThirdParty gcc.
+#
+#     When _foamAddLibAuto is unset (eg, called from makeCGAL):
 #       - boost_version / cgal_version variables are retained.
 #       - the LD_LIBRARY_PATH is not adjusted.
 #
@@ -52,26 +55,18 @@ then
     echo "Using CGAL ($cgal_version)  ->  $CGAL_ARCH_PATH" 1>&2
 fi
 
-if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
+if command -v _foamAddLibAuto > /dev/null 2>&1  # Normal sourcing (not makeCGAL)
 then
+    _foamAddLibAuto $BOOST_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
+    _foamAddLibAuto $CGAL_ARCH_PATH   lib$WM_COMPILER_LIB_ARCH
 
-    # If BOOST_ARCH_PATH, CGAL_ARCH_PATH do not end with '-system' or '-none',
-    # they are either located within ThirdParty, or a central installation
-    # outside of ThirdParty and must be added to the lib-path.
-
-    ending="${BOOST_ARCH_PATH##*-}"
-    if [ "$ending" != none -a "$ending" != system ]
-    then
-        _foamAddLib $BOOST_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
-    fi
+    # GMP/MPFR may have already been added with ThirdParty compiler, but cannot
+    # be certain so add here. Any duplicates will be removed later.
 
-    ending="${CGAL_ARCH_PATH##*-}"
-    if [ "$ending" != none -a "$ending" != system ]
-    then
-        _foamAddLib $CGAL_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
-    fi
+    _foamAddLibAuto $GMP_ARCH_PATH   # No fallback libdir
+    _foamAddLibAuto $MPFR_ARCH_PATH  # No fallback libdir
 
-    unset boost_version cgal_version ending
+    unset boost_version cgal_version
 
 fi
 
diff --git a/etc/config.sh/FFTW b/etc/config.sh/FFTW
index ce961f544af..854bc819171 100644
--- a/etc/config.sh/FFTW
+++ b/etc/config.sh/FFTW
@@ -2,7 +2,7 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
+#   \\  /    A nd           | Copyright (C) 2016-2018 OpenCFD Ltd.
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 # License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/FFTW
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup for FFTW include/libraries (usually ThirdParty installation).
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #     To disable its use:               fftw_version=fftw-none
 #     For system-wide installations:    fftw_version=fftw-system
@@ -24,7 +24,7 @@
 #       2. and provide full path for FFTW_ARCH_PATH
 #
 # Note
-#     When _foamAddLib is unset (eg, called from makeFFTW):
+#     When _foamAddLibAuto is unset (eg, called from makeFFTW):
 #       - fftw_version variable is retained.
 #       - LD_LIBRARY_PATH is not adjusted.
 #
@@ -43,20 +43,12 @@ then
     echo "Using fftw ($fftw_version)  ->  $FFTW_ARCH_PATH" 1>&2
 fi
 
-if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
+if command -v _foamAddLibAuto > /dev/null 2>&1  # Normal sourcing (not makeFFTW)
 then
 
-    # If FFTW_ARCH_PATH does not end with '-system' or '-none',
-    # it is either located within ThirdParty, or a central installation
-    # outside of ThirdParty and must be added to the lib-path.
+    _foamAddLibAuto $FFTW_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
 
-    ending="${FFTW_ARCH_PATH##*-}"
-    if [ "$ending" != none -a "$ending" != system ]
-    then
-        _foamAddLib $FFTW_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
-    fi
-
-    unset fftw_version ending
+    unset fftw_version
 
 fi
 
diff --git a/etc/config.sh/aliases b/etc/config.sh/aliases
index 81b9196b884..088d653ce0b 100644
--- a/etc/config.sh/aliases
+++ b/etc/config.sh/aliases
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/aliases
+#     - sourced by OpenFOAM-*/etc/bashrc (or from the user's ~/.bashrc)
 #
 # Description
-#     Aliases for working with OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc and/or ~/.bashrc
+#     Aliases for working with OpenFOAM.
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.sh/bash_completion b/etc/config.sh/bash_completion
index e0117ea48be..acd7f9fcd18 100644
--- a/etc/config.sh/bash_completion
+++ b/etc/config.sh/bash_completion
@@ -11,6 +11,7 @@
 #
 # File
 #     etc/config.sh/bash_completion
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Bash completion handler for OpenFOAM applications and automatic
diff --git a/etc/config.sh/ccmio b/etc/config.sh/ccmio
index e58250dd0cf..dbf4b5c7498 100644
--- a/etc/config.sh/ccmio
+++ b/etc/config.sh/ccmio
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/ccmio
+#     - sourced during wmake process only.
 #
 # Description
 #     Setup for LIBCCMIO include/libraries.
-#     Sourced during wmake process only.
 #
 #     Static libraries (recommended) are found under CCMIO_ARCH_PATH/lib.
 #     Dynamic libraries are found under FOAM_EXT_LIBBIN path.
diff --git a/etc/config.sh/compiler b/etc/config.sh/compiler
index bd16912d13d..0a5e8f54e35 100644
--- a/etc/config.sh/compiler
+++ b/etc/config.sh/compiler
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/compiler
+#     - sourced by OpenFOAM-*/etc/config.sh/settings
 #
 # Description
 #     Setup for custom compiler versions for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/config.sh/settings
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.sh/ensight b/etc/config.sh/ensight
index 6c06f4fa306..933aa6c9df2 100644
--- a/etc/config.sh/ensight
+++ b/etc/config.sh/ensight
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/ensight
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup for ENSIGHT
-#     Sourced from OpenFOAM-*/etc/bashrc
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.sh/example/compiler b/etc/config.sh/example/compiler
index 8b3f412db08..3d3d748b4d5 100644
--- a/etc/config.sh/example/compiler
+++ b/etc/config.sh/example/compiler
@@ -11,15 +11,15 @@
 #
 # File
 #     config.sh/example/compiler
+#     - sourced by OpenFOAM-*/etc/config.sh/settings
 #
 # Description
 #     Example of fine tuning compiler versions and settings for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/config.sh/settings
 #
 #------------------------------------------------------------------------------
 
 # Load the standard versions
-eval $($WM_PROJECT_DIR/bin/foamEtcFile -sh -mode=o config.sh/compiler)
+eval $($WM_PROJECT_DIR/bin/foamEtcFile -mode=o -sh -config compiler)
 
 # Modify/override compiler settings
 case "$WM_COMPILER" in
diff --git a/etc/config.sh/example/openmpi b/etc/config.sh/example/openmpi
index 4a6b2ddfd55..34f31cdbb21 100644
--- a/etc/config.sh/example/openmpi
+++ b/etc/config.sh/example/openmpi
@@ -11,14 +11,13 @@
 #
 # File
 #     config.sh/example/openmpi
+#     - sourced by OpenFOAM-*/etc/config.sh/mpi
 #
 # Description
 #     Example of fine tuning openmpi settings for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/config.sh/settings
 #
 #------------------------------------------------------------------------------
 
-# Modified openmpi settings
 export FOAM_MPI=openmpi-3.0.0
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.sh/example/paraview b/etc/config.sh/example/paraview
index 9f045e684c2..88ae895f8f1 100644
--- a/etc/config.sh/example/paraview
+++ b/etc/config.sh/example/paraview
@@ -22,7 +22,7 @@
 #
 #------------------------------------------------------------------------------
 
-# Use shipped paraview config file (-mode=o) with a different ParaView_VERSION
+# Use standard paraview config file (-mode=o) with a different ParaView_VERSION
 
 foamFile=$($WM_PROJECT_DIR/bin/foamEtcFile -mode=o config.sh/paraview 2>/dev/null)
 [ $? -eq 0 ] && . $foamFile ParaView_VERSION=5.4.0
diff --git a/etc/config.sh/example/prefs.sh b/etc/config.sh/example/prefs.sh
index fc7abeebf5d..62cead90d76 100644
--- a/etc/config.sh/example/prefs.sh
+++ b/etc/config.sh/example/prefs.sh
@@ -11,12 +11,10 @@
 #
 # File
 #     config.sh/example/prefs.sh
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
-#     Preset variables for the OpenFOAM configuration - POSIX shell syntax.
-#
-#     The prefs.sh file will be sourced by the OpenFOAM etc/bashrc when it is
-#     found by foamEtcFile.
+#     Example of preset variables for the OpenFOAM configuration (POSIX shell)
 #
 # See also
 #     'foamEtcFile -help' or 'foamEtcFile -list' for information about the
@@ -24,13 +22,8 @@
 #
 #------------------------------------------------------------------------------
 
-#- Compiler location:
-WM_COMPILER_TYPE=ThirdParty
-
-#- Compiler:
+export WM_COMPILER_TYPE=ThirdParty
 export WM_COMPILER=Clang
-
-#- MPI implementation:
 export WM_MPLIB=SYSTEMOPENMPI
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.sh/functions b/etc/config.sh/functions
index 5ebf2222dab..99a5ea5b1dd 100644
--- a/etc/config.sh/functions
+++ b/etc/config.sh/functions
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,12 @@
 #
 # File
 #     etc/config.sh/functions
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
-#     Initialization script functions for the bashrc environment
-#     Sourced from OpenFOAM-<VERSION>/etc/config.sh/bashrc
+#     Shell functions and variables used when sourcing the OpenFOAM environment
+#
+#     Some functionality is shadowed in bin/tools/lib-dir
 #
 #------------------------------------------------------------------------------
 
@@ -25,24 +27,91 @@ then
     # Temporary environment variable to track loading/unloading of functions
     WM_SHELL_FUNCTIONS=loaded
 
-    # Prefix to PATH
+    # Cleaning environment variables
+    foamClean=$WM_PROJECT_DIR/bin/foamCleanPath
+
+    # Cleaning environment variables
+    _foamClean()
+    {
+         local var=$1
+         shift
+         eval $($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=$var "$@")
+    }
+
+    # Prepend PATH
     _foamAddPath()
     {
-        [ $# -gt 0 ] && export PATH=$1:$PATH
+        [ -n "$1" ] && export PATH=$1:$PATH
     }
 
-    # Prefix to LD_LIBRARY_PATH
+    # Prepend MANPATH
+    _foamAddMan()
+    {
+        [ -n "$1" ] && export MANPATH=$1:$MANPATH
+    }
+
+    # Prepend LD_LIBRARY_PATH
     _foamAddLib()
     {
-        [ $# -gt 0 ] && export LD_LIBRARY_PATH=$1:$LD_LIBRARY_PATH
+        [ -n "$1" ] && export LD_LIBRARY_PATH=$1:$LD_LIBRARY_PATH
     }
 
-    # Prefix to MANPATH
-    _foamAddMan()
+    # Prefix to LD_LIBRARY_PATH with additional checking
+    # $1 = base directory for 'lib' or 'lib64'
+    # $2 = fallback libname ('lib' or 'lib64')
+    #
+    # 0) Skip entirely if directory ends in "-none" or "-system".
+    #    These special cases (disabled, system directories) should not require
+    #    adjustment of LD_LIBRARY_PATH
+    # 1) Check for dir/lib64 and dir/lib
+    # 2) Use fallback if the previous failed
+    #
+    # Return 0 on success
+    _foamAddLibAuto()
     {
-        [ $# -gt 0 ] && export MANPATH=$1:$MANPATH
+        # Note ksh doesn't have 'local' thus these ugly variable names
+        foamVar_prefix="$1"
+        foamVar_end="${1##*-}"
+
+        # Do not add (none) or a system directory
+        if [ -z "$foamVar_prefix" -o "$foamVar_end" = none -o "$foamVar_end" = system ]
+        then
+            unset foamVar_prefix foamVar_end
+            return 1
+        elif [ -d "$foamVar_prefix" ]
+        then
+            for foamVar_end in lib$WM_COMPILER_LIB_ARCH lib
+            do
+                if [ -d "$foamVar_prefix/$foamVar_end" ]
+                then
+                    export LD_LIBRARY_PATH=$foamVar_prefix/$foamVar_end:$LD_LIBRARY_PATH
+                    unset foamVar_prefix foamVar_end
+                    return 0
+                fi
+            done
+        fi
+
+        # Use fallback. Add without checking existence of the directory
+        foamVar_end=$2
+        if [ -n "$foamVar_end" ]
+        then
+            case "$foamVar_end" in
+            /*)     # An absolute path
+                export LD_LIBRARY_PATH=$foamVar_end:$LD_LIBRARY_PATH
+                ;;
+            (*)     # Relative to prefix
+                export LD_LIBRARY_PATH=$foamVar_prefix/$foamVar_end:$LD_LIBRARY_PATH
+                ;;
+            esac
+            unset foamVar_prefix foamVar_end
+            return 0
+        fi
+
+        unset foamVar_prefix foamVar_end
+        return 1    # Nothing set
     }
 
+
     # Source an etc file, possibly with some verbosity
     # - use eval to avoid intermediate variables (ksh doesn't have 'local')
     if [ "$FOAM_VERBOSE" -a "$PS1" ]
@@ -87,11 +156,31 @@ then
         done
     }
 
+
+    #--------------------------------------------------------------------------
+    # Avoid any ThirdParty settings that may have 'leaked' into the environment
+
+    unset MPI_ARCH_PATH
+    unset ADIOS_ARCH_PATH
+    unset ADIOS1_ARCH_PATH
+    unset ADIOS2_ARCH_PATH
+    unset BOOST_ARCH_PATH
+    unset CCMIO_ARCH_PATH
+    unset CGAL_ARCH_PATH
+    unset FFTW_ARCH_PATH
+    unset GPERFTOOLS_ARCH_PATH
+    unset GMP_ARCH_PATH
+    unset MPFR_ARCH_PATH
+    unset MESA_ARCH_PATH
+    unset METIS_ARCH_PATH
+    unset SCOTCH_ARCH_PATH
+
 else
     # Was previously loaded/defined - now unset
 
-    unset -f _foamAddPath _foamAddLib _foamAddMan 2>/dev/null
-    unset -f _foamEtc _foamEval 2>/dev/null
+    unset -f _foamAddPath _foamAddMan _foamAddLib _foamAddLibAuto 2>/dev/null
+    unset -f _foamClean _foamEtc _foamEval 2>/dev/null
+    unset foamClean
     unset WM_SHELL_FUNCTIONS
 
 fi
diff --git a/etc/config.sh/gperftools b/etc/config.sh/gperftools
index a4b13ec44bc..f23ee42bbb0 100644
--- a/etc/config.sh/gperftools
+++ b/etc/config.sh/gperftools
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2012-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/gperftools
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup file for GPERFTOOLS binaries/libraries.
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #     To disable its use:               gperftools_version=gperftools-none
 #     For system-wide installations:    gperftools_version=gperftools-system
@@ -45,7 +45,7 @@ then
     echo "Using gperftools ($gperftools_version)  ->  $GPERFTOOLS_ARCH_PATH" 1>&2
 fi
 
-if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
+if command -v _foamAddLib > /dev/null 2>&1      # Normal sourcing
 then
 
     # If GPERFTOOLS_ARCH_PATH does not end with '-system' or '-none',
diff --git a/etc/config.sh/kahip b/etc/config.sh/kahip
index 0c764bdc29c..294db422ee2 100644
--- a/etc/config.sh/kahip
+++ b/etc/config.sh/kahip
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/kahip
+#     - sourced during wmake process only.
 #
 # Description
 #     Setup for KAHIP include/libraries (usually ThirdParty installation).
-#     Sourced during wmake process only.
 #
 #     To disable its use:               KAHIP_VERSION=kahip-none
 #     For system-wide installations:    KAHIP_VERSION=kahip-system
diff --git a/etc/config.sh/metis b/etc/config.sh/metis
index cedca16b2aa..bde5498c446 100644
--- a/etc/config.sh/metis
+++ b/etc/config.sh/metis
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/metis
+#     - sourced during wmake process only.
 #
 # Description
 #     Setup for METIS include/libraries (usually ThirdParty installation).
-#     Sourced during wmake process only.
 #
 #     To disable its use:               METIS_VERSION=metis-none
 #     For system-wide installations:    METIS_VERSION=metis-system
diff --git a/etc/config.sh/mgridgen b/etc/config.sh/mgridgen
index dfa06d06663..1cd825f4765 100644
--- a/etc/config.sh/mgridgen
+++ b/etc/config.sh/mgridgen
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/mgridgen
+#     - sourced during wmake process only.
 #
 # Description
 #     Setup for MGRIDGEN include/libraries (usually ThirdParty installation).
-#     Sourced during wmake process only.
 #
 #     Normally used to specify the MGridGen version and location for a
 #     ThirdParty installation.
diff --git a/etc/config.sh/mpi b/etc/config.sh/mpi
index a0f48693e47..55e7cda57b9 100644
--- a/etc/config.sh/mpi
+++ b/etc/config.sh/mpi
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,16 @@
 #
 # File
 #     etc/config.sh/mpi
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup for MPI communications library for OpenFOAM
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
+#
+#     User adjustments are possible in these files:
+#       - config.sh/openmpi-system
+#       - config.sh/openmpi
+#       - config.sh/mpi-user
+#       - config.sh/mpi-system
 #
 #     For USERMPI, the user is responsible for supplying an appropriate
 #     wmake/rules/General/mplibUSERMPI file and managing all settings
@@ -25,31 +31,41 @@ export FOAM_MPI=dummy  # Fallback value
 
 case "$WM_MPLIB" in
 SYSTEMOPENMPI)
-    # Use the system installed openmpi, get library directory via mpicc
+    # The system installed openmpi, locations discovery via mpicc.
     export FOAM_MPI=openmpi-system
 
     # Undefine OPAL_PREFIX if set to one of the paths on foamOldDirs
-    if [ -z "$($foamClean "$OPAL_PREFIX" "$foamOldDirs")" ]
+    if [ -z "$($foamClean -env=OPAL_PREFIX "$foamOldDirs")" ]
     then
         unset OPAL_PREFIX
     fi
+    _foamEtc -config  openmpi-system            # <- Adjustments (optional)
 
-    # Bit of a hack: strip off 'lib' and assume it is the prefix for openmpi
-    # include files and libraries.
-    libDir=$(mpicc --showme:link | sed -e 's/.*-L\([^ ]*\).*/\1/')
+    # Respect MPI_ARCH_PATH if set to a valid directory (ie, from user adjustments)
+    if [ -d "$MPI_ARCH_PATH" ]
+    then
+        _foamAddLibAuto $MPI_ARCH_PATH
+    else
+        # Slight hack: strip off 'lib' to get presumed prefix for include and libs
+        libDir=$(mpicc --showme:link | sed -e 's/.*-L\([^ ]*\).*/\1/')
 
-    export MPI_ARCH_PATH="${libDir%/*}"
-    _foamAddLib     $libDir
-    unset libDir
+        export MPI_ARCH_PATH="${libDir%/*}"
+        _foamAddLib  $libDir
+        unset libDir
+    fi
     ;;
 
 OPENMPI)
     export FOAM_MPI=openmpi-1.10.4
-    _foamEtc config.sh/openmpi                  # <- Adjustments (optional)
+    _foamEtc -config  openmpi                   # <- Adjustments (optional)
 
-    export MPI_ARCH_PATH=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/$FOAM_MPI
+    # Respect MPI_ARCH_PATH if set to a valid directory (ie, from user adjustments)
+    if [ ! -d "$MPI_ARCH_PATH" ]
+    then
+        export MPI_ARCH_PATH=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/$FOAM_MPI
+    fi
 
-    # Tell OpenMPI where to find its install directory
+    # Inform openmpi where to find its install directory
     export OPAL_PREFIX=$MPI_ARCH_PATH
 
     if [ "$FOAM_VERBOSE" -a "$PS1" ]
@@ -60,23 +76,23 @@ OPENMPI)
     fi
 
     # Could be sourced from ThirdParty with incomplete environment
-    if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
+    if command -v _foamAddLibAuto > /dev/null 2>&1  # Normal sourcing
     then
         _foamAddPath    $MPI_ARCH_PATH/bin
-        _foamAddLib     $MPI_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
         _foamAddMan     $MPI_ARCH_PATH/share/man
+        _foamAddLibAuto $MPI_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
     fi
     ;;
 
 USERMPI)
     # Use an arbitrary, user-specified mpi implementation
     export FOAM_MPI=mpi-user
-    _foamEtc config.sh/mpi-user                 # <- Adjustments
+    _foamEtc -config  mpi-user                  # <- Adjustments (optional)
     ;;
 
 SYSTEMMPI)
     export FOAM_MPI=mpi-system
-    _foamEtc config.sh/mpi-system               # <- Adjustments (optional)
+    _foamEtc -config  mpi-system                # <- Adjustments (optional)
 
     if [ -z "$MPI_ROOT" ]
     then
@@ -129,11 +145,11 @@ MPICH)
     export MPI_HOME=$MPI_ARCH_PATH
 
     # Could be sourced from ThirdParty with incomplete environment
-    if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
+    if command -v _foamAddLibAuto > /dev/null 2>&1  # Normal sourcing
     then
         _foamAddPath    $MPI_ARCH_PATH/bin
-        _foamAddLib     $MPI_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
         _foamAddMan     $MPI_ARCH_PATH/share/man
+        _foamAddLibAuto $MPI_ARCH_PATH  lib$WM_COMPILER_LIB_ARCH
     fi
     ;;
 
diff --git a/etc/config.sh/paraview b/etc/config.sh/paraview
index 1ccd7e1e132..20f60412d33 100644
--- a/etc/config.sh/paraview
+++ b/etc/config.sh/paraview
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/paraview
+#     - sourced by OpenFOAM-*/etc/bashrc or via foamPV alias
 #
 # Description
 #     Setup for PARAVIEW (partially cmake, qt too)
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc or from foamPV alias
 #
 #     For system-wide cmake:            cmake_version=cmake-system
 #     For system-wide qt:               ParaView_QT=qt-system
@@ -54,16 +54,11 @@ pv_api=auto             # Normally auto or pair of digits (eg, '5.4' etc)
 archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"
 
 # Clean PATH and LD_LIBRARY_PATH
-cleaned=$($WM_PROJECT_DIR/bin/foamCleanPath "$PATH" \
-    "$ParaView_DIR $archDir/cmake- $archDir/qt- $archDir/ParaView-" \
-) && PATH="$cleaned"
+eval $($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=PATH \
+    "$ParaView_DIR $archDir/ParaView- $archDir/qt- $archDir/cmake-")
 
-if [ -n "$LD_LIBRARY_PATH" ]
-then
-    cleaned=$($WM_PROJECT_DIR/bin/foamCleanPath "$LD_LIBRARY_PATH" \
-        "$ParaView_DIR $archDir/qt- $archDir/ParaView-" \
-    ) && LD_LIBRARY_PATH="$cleaned"
-fi
+eval $($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=LD_LIBRARY_PATH \
+    "$ParaView_DIR $archDir/ParaView- $archDir/qt-")
 
 # ThirdParty cmake
 cmake=$archDir/$cmake_version
diff --git a/etc/config.sh/scotch b/etc/config.sh/scotch
index 00302dfd354..d2cb9f059dd 100644
--- a/etc/config.sh/scotch
+++ b/etc/config.sh/scotch
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/scotch
+#     - sourced during wmake process only.
 #
 # Description
 #     Setup for SCOTCH include/libraries (usually ThirdParty installation).
-#     Sourced during wmake process only.
 #
 #     To disable its use:               SCOTCH_VERSION=scotch-none
 #     For system-wide installations:    SCOTCH_VERSION=scotch-system
diff --git a/etc/config.sh/settings b/etc/config.sh/settings
index 330f197af1f..73de9fa6811 100644
--- a/etc/config.sh/settings
+++ b/etc/config.sh/settings
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -11,9 +11,10 @@
 #
 # File
 #     etc/config.sh/settings
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
-#     Settings for OpenFOAM, sourced from OpenFOAM-<VERSION>/etc/bashrc
+#     Settings for OpenFOAM.
 #
 #------------------------------------------------------------------------------
 export WM_ARCH=$(uname -s)                  # System name
@@ -216,7 +217,7 @@ unset GMP_ARCH_PATH MPFR_ARCH_PATH
 
 # Load pre-defined compiler versions
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-_foamEtc config.sh/compiler
+_foamEtc -config compiler
 
 # ThirdParty base for compilers
 archDir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER_ARCH
@@ -246,21 +247,15 @@ GCC_NOT_FOUND
     # Add ThirdParty compiler libraries to run-time environment
     _foamAddLib     $gccDir/lib$WM_COMPILER_LIB_ARCH
 
-    # Add ThirdParty gmp/mpfr/mpc libraries to run-time environment
-    if [ "${gmpDir##*-}" != system ]
-    then
-        _foamAddLib $gmpDir/lib$WM_COMPILER_LIB_ARCH
-        export GMP_ARCH_PATH=$gmpDir        # For ThirdParty CGAL
-    fi
-    if [ "${mpfrDir##*-}" != system ]
-    then
-        _foamAddLib $mpfrDir/lib$WM_COMPILER_LIB_ARCH
-        export MPFR_ARCH_PATH=$mpfrDir      # For ThirdParty CGAL
-    fi
-    if [ "${mpcDir##*-}" != system ]
-    then
-        _foamAddLib $mpcDir/lib$WM_COMPILER_LIB_ARCH
-    fi
+    # Add gmp/mpfr/mpc libraries to run-time environment.
+    # Require that they exist, automatically find lib64/ or lib/.
+    _foamAddLibAuto $gmpDir   && \
+        export GMP_ARCH_PATH=$gmpDir    # For non-system CGAL
+
+    _foamAddLibAuto $mpfrDir && \
+        export MPFR_ARCH_PATH=$mpfrDir  # For non-system CGAL
+
+    _foamAddLibAuto $mpcDir
 
     if [ "$FOAM_VERBOSE" -a "$PS1" ]
     then
diff --git a/etc/config.sh/unset b/etc/config.sh/unset
index d5d4a8530e3..d1df193478f 100644
--- a/etc/config.sh/unset
+++ b/etc/config.sh/unset
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -93,7 +93,7 @@ unset MPI_ARCH_PATH
 unset MPI_BUFFER_SIZE
 
 # Undefine OPAL_PREFIX if set to one of the paths on foamOldDirs
-if [ -z "$($foamClean "$OPAL_PREFIX" "$foamOldDirs")" ]
+if [ -z "$($foamClean -env=OPAL_PREFIX "$foamOldDirs")" ]
 then
     unset OPAL_PREFIX
 fi
@@ -110,7 +110,7 @@ unset PV_PLUGIN_PATH
 unset VTK_DIR
 
 #------------------------------------------------------------------------------
-# unset other ThirdParty environment variables
+# Unset other ThirdParty environment variables
 
 unset ADIOS_ARCH_PATH
 unset ADIOS1_ARCH_PATH
@@ -133,9 +133,9 @@ unset SCOTCH_ARCH_PATH
 
 if [ -n "$foamClean" ]
 then
-    cleaned=$($foamClean "$PATH" "$foamOldDirs") && PATH="$cleaned"
-    cleaned=$($foamClean "$LD_LIBRARY_PATH" "$foamOldDirs") && LD_LIBRARY_PATH="$cleaned"
-    cleaned=$($foamClean "$MANPATH" "$foamOldDirs") && MANPATH="$cleaned"
+    eval $($foamClean -sh-env=PATH "$foamOldDirs")
+    eval $($foamClean -sh-env=LD_LIBRARY_PATH "$foamOldDirs")
+    eval $($foamClean -sh-env=MANPATH "$foamOldDirs")
 fi
 
 [ -n "$LD_LIBRARY_PATH" ] || unset LD_LIBRARY_PATH
diff --git a/etc/config.sh/vtk b/etc/config.sh/vtk
index 3e73ce564db..add026831a0 100644
--- a/etc/config.sh/vtk
+++ b/etc/config.sh/vtk
@@ -11,10 +11,10 @@
 #
 # File
 #     etc/config.sh/vtk
+#     - sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup file for VTK (and MESA)
-#     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #     The library path is only adjusted when the paths specified here
 #     actually exist at the time of sourcing.
diff --git a/etc/cshrc b/etc/cshrc
index 4dc34be99e9..a678eda3c11 100644
--- a/etc/cshrc
+++ b/etc/cshrc
@@ -3,7 +3,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+#    \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
@@ -136,12 +136,8 @@ endif
 # [WM_PROJECT_USER_DIR] - Location of user files
 setenv WM_PROJECT_USER_DIR $HOME/$WM_PROJECT/$LOGNAME-$WM_PROJECT_VERSION
 
-# Source an etc file, possibly with some verbosity
-if ($?FOAM_VERBOSE && $?prompt) then
-    alias _foamEtc 'eval `$WM_PROJECT_DIR/bin/foamEtcFile -csh-verbose \!*`'
-else
-    alias _foamEtc 'eval `$WM_PROJECT_DIR/bin/foamEtcFile -csh \!*`'
-endif
+# Load shell "functions" (actually aliases)
+source $WM_PROJECT_DIR/etc/config.csh/functions
 
 # Override definitions via prefs, with 'other' first so the sys-admin
 # can provide base values independent of WM_PROJECT_SITE
@@ -181,68 +177,53 @@ while ( $#argv > 0 )
 end
 
 
-# Clean standard environment variables (PATH, LD_LIBRARY_PATH, MANPATH)
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-set foamClean=$WM_PROJECT_DIR/bin/foamCleanPath
-
+# Clean standard environment variables (PATH, MANPATH, LD_LIBRARY_PATH)
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 # Prevent local variables from shadowing setenv variables
 unset PATH MANPATH LD_LIBRARY_PATH LD_PRELOAD
-
 if (! $?LD_LIBRARY_PATH ) setenv LD_LIBRARY_PATH
 if (! $?MANPATH ) setenv MANPATH
 
-# Clean PATH (path)
-set cleaned=`$foamClean "$PATH" "$foamOldDirs"`
-if ( $status == 0 ) setenv PATH $cleaned
-
-# Clean LD_LIBRARY_PATH
-set cleaned=`$foamClean "$LD_LIBRARY_PATH" "$foamOldDirs"`
-if ( $status == 0 ) setenv LD_LIBRARY_PATH $cleaned
-
-# Clean MANPATH
-set cleaned=`$foamClean "$MANPATH" "$foamOldDirs"`
-if ( $status == 0 ) setenv MANPATH $cleaned
+_foamClean PATH "$foamOldDirs"
+_foamClean MANPATH "$foamOldDirs"
+_foamClean LD_LIBRARY_PATH "$foamOldDirs"
 
 
 # Setup for OpenFOAM compilation etc
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-_foamEtc config.csh/settings
+_foamEtc -config  settings
 
 # Setup for third-party packages
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-_foamEtc config.csh/mpi
-_foamEtc config.csh/paraview
-_foamEtc config.csh/vtk
-_foamEtc config.csh/ensight
-## _foamEtc config.csh/ADIOS
-_foamEtc config.csh/CGAL
-_foamEtc config.csh/FFTW
+_foamEtc -config  mpi
+_foamEtc -config  paraview
+_foamEtc -config  vtk
+_foamEtc -config  ensight
+## _foamEtc -config  ADIOS
+_foamEtc -config  CGAL
+_foamEtc -config  FFTW
 
 # Interactive shell
 if ($?prompt) then
-    _foamEtc config.csh/aliases
-    _foamEtc config.csh/tcsh_completion
+    _foamEtc -config  aliases
+    _foamEtc -config  tcsh_completion
 endif
 
 
 # Clean environment paths again. Only remove duplicates
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-# Clean PATH (path)
-set cleaned=`$foamClean "$PATH"`
-if ( $status == 0 ) setenv PATH $cleaned
 
-# Clean LD_LIBRARY_PATH
-set cleaned=`$foamClean "$LD_LIBRARY_PATH"`
-if ( $status == 0 ) setenv LD_LIBRARY_PATH $cleaned
+_foamClean PATH
+_foamClean MANPATH
+_foamClean LD_LIBRARY_PATH
 
-# Clean MANPATH (trailing ':' to find system pages)
-set cleaned=`$foamClean "$MANPATH"`
-if ( $status == 0 ) setenv MANPATH "${cleaned}:"
+# Add trailing ':' for system manpages
+if ( $?MANPATH ) then
+    setenv MANPATH "${MANPATH}:"
+endif
 
-# Clean LD_PRELOAD
 if ( $?LD_PRELOAD ) then
-    set cleaned=`$foamClean "$LD_PRELOAD"`
-    if ( $status == 0 ) setenv LD_PRELOAD $cleaned
+    _foamClean LD_PRELOAD
 endif
 
 
@@ -250,11 +231,14 @@ endif
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 # Unload shell "functions"
+unalias _foamClean
 unalias _foamEtc
 unalias _foamAddPath
-unalias _foamAddLib
 unalias _foamAddMan
+unalias _foamAddLib
+unalias _foamAddLibAuto
 
-unset cleaned foamClean foamOldDirs
+# Variables (done as the last statement for a clean exit code)
+unset cleaned foamOldDirs
 
 #------------------------------------------------------------------------------
-- 
GitLab