From 2821769445acc51361a2c67b73b1f3f304480fb9 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 30 Nov 2018 08:45:13 +0100
Subject: [PATCH] ENH: reimplement startup scripts with functions

- this enables later direct command-line usage

- disable option bundling for selector and selector-menu so that the
  long options work with a single dash.
---
 openfoam-selector/openfoam-selector-menu  |  2 +-
 openfoam-selector/openfoam-selector.in    |  2 +-
 openfoam-selector/openfoam-selector.sh.in | 59 +++++++++++++++--------
 3 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/openfoam-selector/openfoam-selector-menu b/openfoam-selector/openfoam-selector-menu
index 3840172..fa5119c 100755
--- a/openfoam-selector/openfoam-selector-menu
+++ b/openfoam-selector/openfoam-selector-menu
@@ -142,7 +142,7 @@ $| = 1;
 
 # Module options
 $Text::Wrap::columns = 76;
-&Getopt::Long::Configure("bundling");
+# No option bundling
 
 my $help = 0;
 my $version = 0;
diff --git a/openfoam-selector/openfoam-selector.in b/openfoam-selector/openfoam-selector.in
index 70b174d..496beb0 100644
--- a/openfoam-selector/openfoam-selector.in
+++ b/openfoam-selector/openfoam-selector.in
@@ -590,7 +590,7 @@ $| = 1;
 
 # Module options
 $Text::Wrap::columns = 76;
-&Getopt::Long::Configure("bundling");
+# No option bundling
 
 my $help = 0;
 my $register;
diff --git a/openfoam-selector/openfoam-selector.sh.in b/openfoam-selector/openfoam-selector.sh.in
index efe6fe8..86ace04 100644
--- a/openfoam-selector/openfoam-selector.sh.in
+++ b/openfoam-selector/openfoam-selector.sh.in
@@ -6,29 +6,50 @@
 # installation to use.  Not using "alternatives" because we want to be
 # able to set per-user level defaults, not just system-wide defaults.
 
+#
+# Define select mechanism as a function for later reuse
+#
+unset -f openfoam_select 2>/dev/null
+openfoam_select()
+{
 openfoam_selector_dir="@OPENFOAM_SELECTOR_DATADIR@"
 openfoam_selector_homefile="$HOME/@OPENFOAM_SELECTOR_HOME_FILE@"
 openfoam_selector_sysfile="@OPENFOAM_SELECTOR_SYSCONFDIR@/@OPENFOAM_SELECTOR_SYSCONFIG_FILE@"
 
-# The selection name
-unset openfoam_selection
-if test -f "$openfoam_selector_homefile"
-then
-    openfoam_selection=$(cat $openfoam_selector_homefile)
-elif test -f "$openfoam_selector_sysfile"
-then
-    openfoam_selection=$(cat $openfoam_selector_sysfile)
-fi
-
-# 1st: get selection name -> directory name correspondence
-if test "$openfoam_selection" != "" -a -f "$openfoam_selector_dir/$openfoam_selection"
-then
-    openfoam_selection=$(cat "$openfoam_selector_dir/$openfoam_selection")
-
-    # 2nd: test for and source OpenFOAM etc/bashrc
-    if test "$openfoam_selection" != "" -a -f "$openfoam_selection/etc/bashrc"
+    # Get the selection name
+    unset openfoam_selection
+
+    if [ -f "$openfoam_selector_homefile" ]
+    then
+        # Home file
+        openfoam_selection=$(cat "$openfoam_selector_homefile")
+    elif [ -f "$openfoam_selector_sysfile" ]
     then
-    . "$openfoam_selection/etc/bashrc"
+        # System file
+        openfoam_selection=$(cat "$openfoam_selector_sysfile")
     fi
-fi
+
+
+    if [ -n "$openfoam_selection" ] && \
+       [ -f "$openfoam_selector_dir/$openfoam_selection" ]
+    then
+        # Resolve to OpenFOAM directory
+
+        openfoam_selection=$(cat "$openfoam_selector_dir/$openfoam_selection")
+
+        if [ -n "$openfoam_selection" ] && \
+           [ -f "$openfoam_selection/etc/bashrc" ]
+        then
+            . "$openfoam_selection/etc/bashrc" ''
+        fi
+    fi
+
+    # Cleanup
+    unset openfoam_selection openfoam_selector_dir
+    unset openfoam_selector_homefile openfoam_selector_sysfile
+}
+
+# Execute the above
+openfoam_select
+
 # -----------------------------------------------------------------------------
-- 
GitLab