From 7f01a4bedafdbd929818c11f1b465d2912a9618c Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Thu, 20 Apr 2017 11:02:54 +0200
Subject: [PATCH] CONFIG: address differences between shells

- suppress error messages that appear with zsh.
  According to unset(1p), 'unset -f' unsets a function.
  If the function was not previously defined, this is a no-op.
  This is similar for zsh, but there it emits a warning if the
  function was not previously defined.

- avoid 'local' in functions sources from etc/bashrc.
  ksh does not support this.

- use 'command' shell builtin instead of 'type'.
  Seems to be more consistent between shell flavours.
---
 etc/bashrc                   |  2 +-
 etc/config.csh/ADIOS         |  2 +-
 etc/config.csh/unset         |  6 ++++--
 etc/config.sh/ADIOS          |  4 ++--
 etc/config.sh/CGAL           |  2 +-
 etc/config.sh/FFTW           |  2 +-
 etc/config.sh/aliases        |  6 +++---
 etc/config.sh/functions      | 20 +++++++++-----------
 etc/config.sh/gperftools     |  2 +-
 etc/config.sh/mpi            |  4 ++--
 etc/config.sh/paraview       |  2 +-
 etc/config.sh/unset          | 16 ++++++++--------
 etc/config.sh/vtk            |  2 +-
 etc/cshrc                    |  2 +-
 wmake/scripts/cmakeFunctions |  2 +-
 15 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/etc/bashrc b/etc/bashrc
index 9d7858586e7..ccc6a3eebcc 100644
--- a/etc/bashrc
+++ b/etc/bashrc
@@ -27,7 +27,7 @@
 # Description
 #     Set the environment for OpenFOAM when using a POSIX shell.
 #     To be sourced manually or from the ~/.profile or ~/.bashrc files.
-#     Should be usable by any POSIX-compliant shell (eg, dash, ksh)
+#     Should be usable by any POSIX-compliant shell (eg, bash,dash,zsh,...)
 #
 #------------------------------------------------------------------------------
 
diff --git a/etc/config.csh/ADIOS b/etc/config.csh/ADIOS
index 64dce79c5ad..7eb1235542c 100644
--- a/etc/config.csh/ADIOS
+++ b/etc/config.csh/ADIOS
@@ -68,6 +68,6 @@ if ( "$ending" != "adios-none" && "$ending" != "adios-system" ) then
     _foamAddPath $ADIOS_ARCH_PATH/bin
 endif
 
-unset adios_version ending cleaned
+unset adios_version ending
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/unset b/etc/config.csh/unset
index 9d5cd806fd8..6f52ec8c426 100644
--- a/etc/config.csh/unset
+++ b/etc/config.csh/unset
@@ -167,8 +167,6 @@ if ( $?foamClean ) then
 endif
 
 
-unset cleaned foamClean foamOldDirs
-
 #------------------------------------------------------------------------------
 # Cleanup aliases
 
@@ -198,5 +196,9 @@ unalias wmRefresh
 unalias foamVersion
 unalias foamPV
 
+#------------------------------------------------------------------------------
+# Intermediate variables (do as last for a clean exit code)
+
+unset cleaned foamClean foamOldDirs
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.sh/ADIOS b/etc/config.sh/ADIOS
index 15fd0169f25..58ef50a9ce5 100644
--- a/etc/config.sh/ADIOS
+++ b/etc/config.sh/ADIOS
@@ -58,7 +58,7 @@ then
     echo "Using adios ($adios_version)  ->  $ADIOS_ARCH_PATH" 1>&2
 fi
 
-if type _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
@@ -71,7 +71,7 @@ then
         _foamAddPath $ADIOS_ARCH_PATH/bin
     fi
 
-    unset adios_version ending cleaned
+    unset adios_version ending
 
 fi
 
diff --git a/etc/config.sh/CGAL b/etc/config.sh/CGAL
index 837d0e438d3..dc97d66a11a 100644
--- a/etc/config.sh/CGAL
+++ b/etc/config.sh/CGAL
@@ -64,7 +64,7 @@ then
     echo "Using CGAL ($cgal_version)  ->  $CGAL_ARCH_PATH" 1>&2
 fi
 
-if type _foamAddLib > /dev/null 2>&1    # normal sourcing
+if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
 then
 
     # If BOOST_ARCH_PATH, CGAL_ARCH_PATH do not end with '-system' or '-none',
diff --git a/etc/config.sh/FFTW b/etc/config.sh/FFTW
index 47d3d22f8af..c8b707d6a5f 100644
--- a/etc/config.sh/FFTW
+++ b/etc/config.sh/FFTW
@@ -58,7 +58,7 @@ then
     echo "Using fftw ($fftw_version)  ->  $FFTW_ARCH_PATH" 1>&2
 fi
 
-if type _foamAddLib > /dev/null 2>&1    # normal sourcing
+if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
 then
 
     # If FFTW_ARCH_PATH does not end with '-system' or '-none',
diff --git a/etc/config.sh/aliases b/etc/config.sh/aliases
index fbc0111677c..23b8b636e80 100644
--- a/etc/config.sh/aliases
+++ b/etc/config.sh/aliases
@@ -69,7 +69,7 @@ alias run='cd $FOAM_RUN'
 
 # Refresh the environment
 # ~~~~~~~~~~~~~~~~~~~~~~~
-unset -f wmRefresh
+unset -f wmRefresh 2>/dev/null
 wmRefresh()
 {
     wmProjectDir=$WM_PROJECT_DIR
@@ -81,7 +81,7 @@ wmRefresh()
 
 # Change OpenFOAM version
 # ~~~~~~~~~~~~~~~~~~~~~~~
-unset -f foamVersion
+unset -f foamVersion 2>/dev/null
 foamVersion()
 {
     if [ "$1" ]; then
@@ -98,7 +98,7 @@ foamVersion()
 
 # Change ParaView version
 # ~~~~~~~~~~~~~~~~~~~~~~~
-unset -f foamPV
+unset -f foamPV 2>/dev/null
 foamPV()
 {
     . $WM_PROJECT_DIR/etc/config.sh/paraview "${@+ParaView_VERSION=$1}"
diff --git a/etc/config.sh/functions b/etc/config.sh/functions
index 4413108cdc7..df3096d998b 100644
--- a/etc/config.sh/functions
+++ b/etc/config.sh/functions
@@ -56,15 +56,13 @@ then
     }
 
     # Source an etc file, possibly with some verbosity
-    _foamEtc()
-    {
-        local file
-        if [ $# -gt 0 ] && file=$($WM_PROJECT_DIR/bin/foamEtcFile "$@")
-        then
-            [ "$FOAM_VERBOSE" -a "$PS1" ] && echo "Using: $file" 1>&2
-            . $file
-        fi
-    }
+    # - use eval to avoid intermediate variables (ksh doesn't have 'local')
+    if [ "$FOAM_VERBOSE" -a "$PS1" ]
+    then
+        _foamEtc(){ eval "$($WM_PROJECT_DIR/bin/foamEtcFile -sh-verbose $@)"; }
+    else
+        _foamEtc(){ eval "$($WM_PROJECT_DIR/bin/foamEtcFile -sh $@)"; }
+    fi
 
     # Evaluate command-line parameters
     _foamEval()
@@ -104,9 +102,9 @@ then
 else
     # Was previously loaded/defined - now unset
 
+    unset -f _foamAddPath _foamAddLib _foamAddMan 2>/dev/null
+    unset -f _foamEtc _foamEval 2>/dev/null
     unset WM_SHELL_FUNCTIONS
-    unset -f _foamAddPath _foamAddLib _foamAddMan
-    unset -f _foamEtc _foamEval
 
 fi
 
diff --git a/etc/config.sh/gperftools b/etc/config.sh/gperftools
index da49c761670..4e13f927bb2 100644
--- a/etc/config.sh/gperftools
+++ b/etc/config.sh/gperftools
@@ -58,7 +58,7 @@ then
     echo "Using gperftools ($gperftools_version)  ->  $GPERFTOOLS_ARCH_PATH" 1>&2
 fi
 
-if type _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/mpi b/etc/config.sh/mpi
index ae3961361f5..812b354a1be 100644
--- a/etc/config.sh/mpi
+++ b/etc/config.sh/mpi
@@ -71,7 +71,7 @@ OPENMPI)
     fi
 
     # Could be sourced from ThirdParty with incomplete environment
-    if type _foamAddLib > /dev/null 2>&1
+    if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
     then
         _foamAddPath    $MPI_ARCH_PATH/bin
         _foamAddLib     $MPI_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
@@ -140,7 +140,7 @@ MPICH)
     export MPI_HOME=$MPI_ARCH_PATH
 
     # Could be sourced from ThirdParty with incomplete environment
-    if type _foamAddLib > /dev/null 2>&1
+    if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
     then
         _foamAddPath    $MPI_ARCH_PATH/bin
         _foamAddLib     $MPI_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH
diff --git a/etc/config.sh/paraview b/etc/config.sh/paraview
index 4aadf1dccca..05ae45ba449 100644
--- a/etc/config.sh/paraview
+++ b/etc/config.sh/paraview
@@ -173,7 +173,7 @@ unset -f _foamParaviewEval
 unset cleaned cmake cmake_version pvName pvMajor pvLibDir pvPython
 unset ParaView_MAJOR
 
-if type _foamAddLib > /dev/null 2>&1    # normal sourcing
+if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
 then
     unset ParaView_VERSION
 fi
diff --git a/etc/config.sh/unset b/etc/config.sh/unset
index 5c414df74c9..d9aa0639eb6 100644
--- a/etc/config.sh/unset
+++ b/etc/config.sh/unset
@@ -148,13 +148,9 @@ then
     cleaned=$($foamClean "$MANPATH" "$foamOldDirs") && MANPATH="$cleaned"
 fi
 
-
 [ -n "$LD_LIBRARY_PATH" ] || unset LD_LIBRARY_PATH
-[ -n "$MANPATH" ] || unset MANPATH
 [ -n "$LD_PRELOAD" ] || unset LD_PRELOAD
-
-unset cleaned foamClean foamOldDirs
-
+[ -n "$MANPATH" ] || unset MANPATH
 
 #------------------------------------------------------------------------------
 # Cleanup aliases and functions
@@ -181,9 +177,13 @@ unalias util        2>/dev/null
 unalias tut         2>/dev/null
 unalias run         2>/dev/null
 
-unset -f wmRefresh
-unset -f foamVersion
-unset -f foamPV
+unset -f wmRefresh  2>/dev/null
+unset -f foamVersion 2>/dev/null
+unset -f foamPV     2>/dev/null
+
+#------------------------------------------------------------------------------
+# Intermediate variables (do as last for a clean exit code)
 
+unset cleaned foamClean foamOldDirs
 
 #------------------------------------------------------------------------------
diff --git a/etc/config.sh/vtk b/etc/config.sh/vtk
index 004a8d2b826..9c2a0fe33ec 100644
--- a/etc/config.sh/vtk
+++ b/etc/config.sh/vtk
@@ -57,7 +57,7 @@ then
     echo "Using mesa ($mesa_version)  ->  $MESA_ARCH_PATH" 1>&2
 fi
 
-if type _foamAddLib > /dev/null 2>&1    # normal sourcing
+if command -v _foamAddLib > /dev/null 2>&1  # normal sourcing
 then
 
     # Set paths if binaries are present
diff --git a/etc/cshrc b/etc/cshrc
index 2e82fe08db8..4fb51cad687 100644
--- a/etc/cshrc
+++ b/etc/cshrc
@@ -145,7 +145,7 @@ endif
 # ~~~~~~~~~~~~~~~~~~~~~~
 setenv WM_PROJECT_USER_DIR $HOME/$WM_PROJECT/$LOGNAME-$WM_PROJECT_VERSION
 
-# Source etc files, possibly with some verbosity
+# Source an etc file, possibly with some verbosity
 if ($?FOAM_VERBOSE && $?prompt) then
     alias _foamEtc 'eval `$WM_PROJECT_DIR/bin/foamEtcFile -csh-verbose \!*`'
 else
diff --git a/wmake/scripts/cmakeFunctions b/wmake/scripts/cmakeFunctions
index c64bdb3525c..ca72558c9ec 100644
--- a/wmake/scripts/cmakeFunctions
+++ b/wmake/scripts/cmakeFunctions
@@ -158,7 +158,7 @@ canBuildPlugin()
         return 1
     }
 
-    type cmake > /dev/null 2>&1 || {
+    command -v cmake > /dev/null 2>&1 || {
         echo "==> cannot build ParaView plugins without cmake"
         return 1
     }
-- 
GitLab