From 9cbc0efca3d8c25b25214e17bb0468b2ae694565 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Fri, 11 Mar 2011 18:32:18 +0100
Subject: [PATCH] ENH: add support for WM_PROJECT_SITE env variable

- alternative to WM_PROJECT_INST_DIR/site for systems with locked down
  installation paths (eg, when packaged as RPM or deb)

  When "$WM_PROJECT_SITE" is defined, it is used in favour of
  "$WM_PROJECT_INST_DIR/site"
---
 bin/foamEtcFile                   | 10 ++---
 bin/foamLog                       | 10 +++--
 bin/foamNewCase                   |  2 +-
 etc/aliases.csh                   |  7 ++-
 etc/aliases.sh                    |  8 +++-
 etc/settings.csh                  |  3 ++
 etc/settings.sh                   |  3 ++
 src/OSspecific/POSIX/POSIX.C      | 73 ++++++++++++++++++++++---------
 src/OpenFOAM/include/OSspecific.H | 22 +++++-----
 9 files changed, 94 insertions(+), 44 deletions(-)

diff --git a/bin/foamEtcFile b/bin/foamEtcFile
index 20b5be0fedf..a6859b6ae5a 100755
--- a/bin/foamEtcFile
+++ b/bin/foamEtcFile
@@ -204,15 +204,15 @@ fileName="${1#~OpenFOAM/}"
 unset dirList
 case "$mode" in
 *u*)  # user
-    dirList="$dirList $HOME/.${WM_PROJECT:-OpenFOAM}/$version"
-    dirList="$dirList $HOME/.${WM_PROJECT:-OpenFOAM}"
+    userDir="$HOME/.${WM_PROJECT:-OpenFOAM}"
+    dirList="$dirList $userDir/$version $userDir"
     ;;
 esac
 
 case "$mode" in
-*g*)  # group
-    dirList="$dirList $prefixDir/site/$version"
-    dirList="$dirList $prefixDir/site"
+*g*)  # group (site)
+    siteDir="${WM_PROJECT_SITE:-$prefixDir/site}"
+    dirList="$dirList $siteDir/$version $siteDir"
     ;;
 esac
 
diff --git a/bin/foamLog b/bin/foamLog
index b5ca4900b0c..c86dce622da 100755
--- a/bin/foamLog
+++ b/bin/foamLog
@@ -33,6 +33,8 @@
 #------------------------------------------------------------------------------
 Script=${0##*/}
 toolsDir=${0%/*}/tools
+siteDir=${WM_PROJECT_SITE:-${WM_PROJECT_INST_DIR:-<unknown>}/site}
+userDir=$HOME/.OpenFOAM
 
 usage() {
     exec 1>&2
@@ -84,10 +86,10 @@ The value taken will be the first (non-space)word after this column.
 The database ($Script.db) will taken from these locations:
 
     .
-    $HOME/.OpenFOAM/$WM_PROJECT_VERSION
-    $HOME/.OpenFOAM
-    $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION
-    $WM_PROJECT_INST_DIR/site
+    $userDir/$WM_PROJECT_VERSION
+    $userDir
+    $siteDir/$WM_PROJECT_VERSION
+    $siteDir
     $WM_PROJECT_DIR/etc
     $toolsDir
 
diff --git a/bin/foamNewCase b/bin/foamNewCase
index fda86b4bdda..3abbd76e1ef 100755
--- a/bin/foamNewCase
+++ b/bin/foamNewCase
@@ -30,7 +30,7 @@
 #     - requires rsync
 #
 #------------------------------------------------------------------------------
-siteDir=${WM_PROJECT_INST_DIR:-unknown}/site
+siteDir=${WM_PROJECT_SITE:-${WM_PROJECT_INST_DIR:-<unknown>}/site}
 userDir=$HOME/.OpenFOAM
 version=${WM_PROJECT_VERSION:-unknown}
 templateDir="appTemplates"
diff --git a/etc/aliases.csh b/etc/aliases.csh
index 76baa715855..314810bef65 100644
--- a/etc/aliases.csh
+++ b/etc/aliases.csh
@@ -67,6 +67,11 @@ alias foamSol 'cd $FOAM_SOLVERS'
 alias foamTuts 'cd $FOAM_TUTORIALS'
 alias foamUtils 'cd $FOAM_UTILITIES'
 alias foam3rdParty 'cd $WM_THIRD_PARTY_DIR'
-alias foamSite 'cd $WM_PROJECT_INST_DIR/site'
+
+if ( $?WM_PROJECT_SITE ) then
+    alias foamSite 'cd $WM_PROJECT_SITE'
+else
+    alias foamSite 'cd $WM_PROJECT_INST_DIR/site'
+endif
 
 # -----------------------------------------------------------------------------
diff --git a/etc/aliases.sh b/etc/aliases.sh
index c3d05d4a72a..2f3898a90ec 100644
--- a/etc/aliases.sh
+++ b/etc/aliases.sh
@@ -72,6 +72,12 @@ alias foamSol='cd $FOAM_SOLVERS'
 alias foamTuts='cd $FOAM_TUTORIALS'
 alias foamUtils='cd $FOAM_UTILITIES'
 alias foam3rdParty='cd $WM_THIRD_PARTY_DIR'
-alias foamSite='cd $WM_PROJECT_INST_DIR/site'
+
+if [ -n "$WM_PROJECT_SITE" ]
+then
+    alias foamSite='cd $WM_PROJECT_SITE'
+else
+    alias foamSite='cd $WM_PROJECT_INST_DIR/site'
+fi
 
 # -----------------------------------------------------------------------------
diff --git a/etc/settings.csh b/etc/settings.csh
index 5a6b6c273a7..ae4da934d9e 100644
--- a/etc/settings.csh
+++ b/etc/settings.csh
@@ -148,6 +148,9 @@ setenv FOAM_LIBBIN $WM_PROJECT_DIR/platforms/$WM_OPTIONS/lib
 # external (ThirdParty) libraries
 setenv FOAM_EXT_LIBBIN $WM_THIRD_PARTY_DIR/platforms/$WM_OPTIONS/lib
 
+# default location of site-specific templates etc
+# setenv WM_PROJECT_SITE $WM_PROJECT_INST_DIR/site
+
 # shared site executables/libraries
 # similar naming convention as ~OpenFOAM expansion
 setenv FOAM_SITE_APPBIN $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION/platforms/$WM_OPTIONS/bin
diff --git a/etc/settings.sh b/etc/settings.sh
index e86de7de25e..17c8c091ee5 100644
--- a/etc/settings.sh
+++ b/etc/settings.sh
@@ -169,6 +169,9 @@ export FOAM_LIBBIN=$WM_PROJECT_DIR/platforms/$WM_OPTIONS/lib
 # external (ThirdParty) libraries
 export FOAM_EXT_LIBBIN=$WM_THIRD_PARTY_DIR/platforms/$WM_OPTIONS/lib
 
+# default location of site-specific templates etc
+# export WM_PROJECT_SITE=$WM_PROJECT_INST_DIR/site
+
 # shared site executables/libraries
 # similar naming convention as ~OpenFOAM expansion
 export FOAM_SITE_APPBIN=$WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION/platforms/$WM_OPTIONS/bin
diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C
index e6e54c32c05..ff4c2e84ee1 100644
--- a/src/OSspecific/POSIX/POSIX.C
+++ b/src/OSspecific/POSIX/POSIX.C
@@ -253,19 +253,20 @@ bool Foam::chDir(const fileName& dir)
 
 Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory)
 {
-    // Search user files:
-    // ~~~~~~~~~~~~~~~~~~
+    //
+    // search for user files in
+    // * ~/.OpenFOAM/VERSION
+    // * ~/.OpenFOAM
+    //
     fileName searchDir = home()/".OpenFOAM";
     if (isDir(searchDir))
     {
-        // Check for user file in ~/.OpenFOAM/VERSION
         fileName fullName = searchDir/FOAMversion/name;
         if (isFile(fullName))
         {
             return fullName;
         }
 
-        // Check for version-independent user file in ~/.OpenFOAM
         fullName = searchDir/name;
         if (isFile(fullName))
         {
@@ -274,32 +275,61 @@ Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory)
     }
 
 
-    // Search site files:
-    // ~~~~~~~~~~~~~~~~~~
-    searchDir = getEnv("WM_PROJECT_INST_DIR");
-    if (isDir(searchDir))
+    //
+    // search for group (site) files in
+    // * $WM_PROJECT_SITE/VERSION
+    // * $WM_PROJECT_SITE
+    //
+    searchDir = getEnv("WM_PROJECT_SITE");
+    if (searchDir.size())
     {
-        // Check for site file in $WM_PROJECT_INST_DIR/site/VERSION
-        fileName fullName = searchDir/"site"/FOAMversion/name;
-        if (isFile(fullName))
+        if (isDir(searchDir))
         {
-            return fullName;
-        }
+            fileName fullName = searchDir/FOAMversion/name;
+            if (isFile(fullName))
+            {
+                return fullName;
+            }
 
-        // Check for version-independent site file in $WM_PROJECT_INST_DIR/site
-        fullName = searchDir/"site"/name;
-        if (isFile(fullName))
+            fullName = searchDir/name;
+            if (isFile(fullName))
+            {
+                return fullName;
+            }
+        }
+    }
+    else
+    {
+        //
+        // OR search for group (site) files in
+        // * $WM_PROJECT_INST_DIR/site/VERSION
+        // * $WM_PROJECT_INST_DIR/site
+        //
+        searchDir = getEnv("WM_PROJECT_INST_DIR");
+        if (isDir(searchDir))
         {
-            return fullName;
+            fileName fullName = searchDir/"site"/FOAMversion/name;
+            if (isFile(fullName))
+            {
+                return fullName;
+            }
+
+            fullName = searchDir/"site"/name;
+            if (isFile(fullName))
+            {
+                return fullName;
+            }
         }
     }
 
-    // Search installation files:
-    // ~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+    //
+    // search for other (shipped) files in
+    // * $WM_PROJECT_DIR/etc
+    //
     searchDir = getEnv("WM_PROJECT_DIR");
     if (isDir(searchDir))
     {
-        // Check for shipped OpenFOAM file in $WM_PROJECT_DIR/etc
         fileName fullName = searchDir/"etc"/name;
         if (isFile(fullName))
         {
@@ -311,7 +341,8 @@ Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory)
     // abort if the file is mandatory, otherwise return null
     if (mandatory)
     {
-        std::cerr<< "--> FOAM FATAL ERROR in Foam::findEtcFile() :"
+        std::cerr
+            << "--> FOAM FATAL ERROR in Foam::findEtcFile() :"
                " could not find mandatory file\n    '"
             << name.c_str() << "'\n\n" << std::endl;
         ::exit(1);
diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H
index d68ae1661db..ec1d5bdcc30 100644
--- a/src/OpenFOAM/include/OSspecific.H
+++ b/src/OpenFOAM/include/OSspecific.H
@@ -93,22 +93,22 @@ fileName cwd();
 //  else return false
 bool chDir(const fileName& dir);
 
-//- Search for \em name
-//  in the following hierarchy:
-//  -# personal settings:
-//    - ~/.OpenFOAM/\<VERSION\>/
-//      <em>for version-specific files</em>
+//- Search for a file from user/group/shipped directories.
+//  The search scheme allows for version-specific and
+//  version-independent files using the following hierarchy:
+//  - \b user settings:
+//    - ~/.OpenFOAM/\<VERSION\>
 //    - ~/.OpenFOAM/
-//      <em>for version-independent files</em>
-//  -# site-wide settings:
+//  - \b group (site) settings (when $WM_PROJECT_SITE is set):
+//    - $WM_PROJECT_SITE/\<VERSION\>
+//    - $WM_PROJECT_SITE
+//  - \b group (site) settings (when $WM_PROJECT_SITE is not set):
 //    - $WM_PROJECT_INST_DIR/site/\<VERSION\>
-//      <em>for version-specific files</em>
 //    - $WM_PROJECT_INST_DIR/site/
-//      <em>for version-independent files</em>
-//  -# shipped settings:
+//  - \b other (shipped) settings:
 //    - $WM_PROJECT_DIR/etc/
 //
-//  \return the full path name or fileName() if the name cannot be found
+//  \return The full path name or fileName() if the name cannot be found
 //  Optionally abort if the file cannot be found
 fileName findEtcFile(const fileName&, bool mandatory=false);
 
-- 
GitLab