From 9745b86ef3d27af9ff42ec43008506a659cf87be Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Tue, 6 Jul 2010 14:01:15 +0200
Subject: [PATCH] ENH: improve foamEtcFile handling (-prefix, -version options)

- This lets foamExec use foamEtcFile as well, but debian packaging
  still needs some work
---
 bin/foamEtcFile | 33 +++++++++++++----
 bin/foamExec    | 94 ++++++++++++++++++++++++-------------------------
 2 files changed, 72 insertions(+), 55 deletions(-)

diff --git a/bin/foamEtcFile b/bin/foamEtcFile
index 084c3f3769c..d661e33f6f9 100755
--- a/bin/foamEtcFile
+++ b/bin/foamEtcFile
@@ -39,8 +39,6 @@
 #     @endverbatim
 #
 #-------------------------------------------------------------------------------
-unset listOpt quietOpt
-
 usage() {
     [ "$quietOpt" = true ] && exit 1
 
@@ -53,7 +51,11 @@ Usage: ${0##*/} [OPTION] fileName
 options:
   -list             list the directories to be searched
   -mode <mode>      any combination of u(user), g(group), o(other)
+  -prefix <dir>     specify an alternative installation prefix
+                        (default: $WM_PROJECT_INST_DIR)
   -quiet            suppress all normal output
+  -version <ver>    specify an alternative OpenFOAM version
+                        (default: $WM_PROJECT_VERSION)
   -help             print the usage
 
   Locate user/group/shipped file with semantics similar to the
@@ -75,6 +77,13 @@ USAGE
 # default mode is 'ugo'
 mode=ugo
 
+# default prefix/version correspond to the active values,
+# but could also extract from the $0 value.
+prefix="$WM_PROJECT_INST_DIR"
+version="$WM_PROJECT_VERSION"
+
+
+unset listOpt quietOpt
 # parse options
 while [ "$#" -gt 0 ]
 do
@@ -100,10 +109,20 @@ do
         esac
         shift 2
         ;;
+    -p | -prefix)
+        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
+        prefix="$2"
+        shift 2
+        ;;
     -q | -quiet)
         quietOpt=true
         shift
         ;;
+    -v | -version)
+        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
+        version="$2"
+        shift 2
+        ;;
     -*)
         usage "unknown option: '$*'"
         ;;
@@ -121,21 +140,21 @@ fileName="$1"
 unset dirList
 case "$mode" in
 *u*)  # user
-    dirList="$dirList $HOME/.${WM_PROJECT:-OpenFOAM}/$WM_PROJECT_VERSION"
+    dirList="$dirList $HOME/.${WM_PROJECT:-OpenFOAM}/$version"
     dirList="$dirList $HOME/.${WM_PROJECT:-OpenFOAM}"
     ;;
 esac
 
 case "$mode" in
 *g*)  # group
-    dirList="$dirList $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION"
-    dirList="$dirList $WM_PROJECT_INST_DIR/site"
+    dirList="$dirList $prefix/site/$version"
+    dirList="$dirList $prefix/site"
     ;;
 esac
 
 case "$mode" in
-*o*)  # other
-    dirList="$dirList $WM_PROJECT_DIR/etc"
+*o*)  # other (shipped)
+    dirList="$dirList $prefix/${WM_PROJECT:-OpenFOAM}-$version/etc"
     ;;
 esac
 set -- $dirList
diff --git a/bin/foamExec b/bin/foamExec
index 84bf0435111..c5106391d02 100755
--- a/bin/foamExec
+++ b/bin/foamExec
@@ -43,8 +43,9 @@ usage() {
 Usage: ${0##*/} [OPTION] <application> ...
 
 options:
-  -v ver        specify OpenFOAM version
-  -help         this usage
+  -version <ver>    specify an alternative OpenFOAM version
+                    (default: taken from \$0 parameter)
+  -help             this usage
 
 * run a particular OpenFOAM version of <application>
 
@@ -52,17 +53,34 @@ USAGE
     exit 1
 }
 
+# This script must exist in <foamInstall>/OpenFOAM-<VERSION>/bin/
+# or <foamInstall>/openfoamVERSION/bin/
+#
 
-# This script should exist in <foamInstall>/OpenFOAM-<VERSION>/bin/
-# extract the <foamInstall> and <version> elements
-# using a function preserves the command args
-getDefaults() {
-    set -- $(echo $0 | sed -e 's@/OpenFOAM-\([^/]*\)/bin/[^/]*$@ \1@')
-    foamInstall=$1
-    version=$2
-}
+# the bindir:
+binDir="${0%/*}"
+
+# the project dir:
+projectDir="${binDir%/bin}"
+export WM_PROJECT_DIR="$projectDir"
+
+# the prefix dir (same as foamInstall):
+prefixDir="${projectDir%/*}"
+foamInstall="$prefixDir"
+
+# the name used for the project directory
+projectDirName="${projectDir##*/}"
+
+# version from OpenFOAM-VERSION (normal) or openfoamVERSION (debian)
+version=$(echo $projectDirName | sed -e 's@^openfoam-*@@i')
+
+# debugging:
+# echo "Installed locations:"
+# for i in projectDir prefixDir foamInstall projectDirName version
+# do
+#     eval echo "$i=\$$i"
+# done
 
-getDefaults
 
 # parse options
 while [ "$#" -gt 0 ]
@@ -71,10 +89,10 @@ do
     -h | -help)
         usage
         ;;
-    -v)
-        shift
-        version=$1
-        shift
+    -v | version)
+        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
+        version=$2
+        shift 2
         ;;
     --)
         shift
@@ -89,41 +107,21 @@ do
     esac
 done
 
-if [ "$#" -lt 1 ]
-then
-    usage "no application specified"
-fi
-
-unset foamDotFile
-
-# Check user-specific OpenFOAM bashrc file
-foamDotFile="$HOME/.OpenFOAM/$version/bashrc"
-if [ -f $foamDotFile ]
-then
-    . $foamDotFile
-    foamDotFile=okay
-else
-    # Use the FOAM_INST_DIR variable for locating the installed version
-    for FOAM_INST_DIR in $foamInstall $WM_PROJECT_INST_DIR
-    do
-        foamDotFile="$FOAM_INST_DIR/OpenFOAM-$version/etc/bashrc"
-        if [ -f $foamDotFile ]
-        then
-            . $foamDotFile
-            foamDotFile=okay
-            break
-        fi
-    done
-fi
-
-
-if [ "$foamDotFile" != okay ]
-then
+[ "$#" -ge 1 ] || usage "no application specified"
+
+
+# find OpenFOAM settings (bashrc)
+foamDotFile="$($binDir/foamEtcFile -version $version bashrc)"
+[ $? -eq 0 ] || {
     echo "Error : bashrc file could not be found for OpenFOAM-$version" 1>&2
     exit 1
-fi
+}
+
+# preserve arguments (can otherwise get lost when sourcing the foamDotFile)
+args="$*"
+. $foamDotFile
 
-# Pass on the rest of the arguments
-exec $*
+# execute
+exec $args
 
 #------------------------------------------------------------------------------
-- 
GitLab