From 6849c008a1c14b6c0cd6163a2f93382d372db629 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 25 Mar 2020 09:58:52 +0100
Subject: [PATCH] ENH: support -case option for foamCleanTutorials,
 foamRunTutorials

- makes it easier to run/clean individual cases
---
 bin/foamCleanTutorials | 118 +++++++++++++++++++++++++++--------------
 bin/foamRunTutorials   | 100 ++++++++++++++++++++++++++--------
 2 files changed, 157 insertions(+), 61 deletions(-)

diff --git a/bin/foamCleanTutorials b/bin/foamCleanTutorials
index 597788843f0..d2d1eeb153f 100755
--- a/bin/foamCleanTutorials
+++ b/bin/foamCleanTutorials
@@ -7,23 +7,10 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011 OpenFOAM Foundation
-#     Copyright (C) 2019 OpenCFD Ltd.
+#     Copyright (C) 2019-2020 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
-#     This file is part of OpenFOAM.
-#
-#     OpenFOAM is free software: you can redistribute it and/or modify it
-#     under the terms of the GNU General Public License as published by
-#     the Free Software Foundation, either version 3 of the License, or
-#     (at your option) any later version.
-#
-#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
-#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-#     for more details.
-#
-#     You should have received a copy of the GNU General Public License
-#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # Script
 #     foamCleanTutorials
@@ -32,10 +19,6 @@
 #     Run either Allwclean, Allclean or default cleanCase in current directory
 #     and all its subdirectories.
 #
-#     When an argument is provided, it is treated as a directory name.
-#     If an option (eg, -self) is provided, it suppresses calling
-#     Allwclean or Allclean (ie, to avoid recursion)
-#
 #------------------------------------------------------------------------------
 . $WM_PROJECT_DIR/bin/tools/CleanFunctions  # Tutorial clean functions
 
@@ -45,29 +28,68 @@ then
     thisScript="$PWD/$thisScript"
 fi
 
-# Simple option/argument parsing.
-# By default use Allclean, Allwclean when present
-#
-# If an argument is supplied, treat as a change directory
-#
-# If an option (eg, -self) is provided,
-# do not execute ./Allwclean or ./Allclean (to avoid recursion)
+printHelp() {
+    cat <<USAGE
+
+Usage: ${0##*/} [OPTION]
+       ${0##*/} [OPTION] directory
+options:
+  -0                use cleanCase0 instead of cleanCase
+  -case <dir>       specify starting directory, default is cwd
+  -self             avoid Allclean script (prevent infinite recursion)
+  -help             print the usage
+
+Recursively clean an OpenFOAM case directory.
+By default uses Allclean, Allwclean when present.
+
+USAGE
+   exit 0  # 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 '${0##*/} -help' for usage"
+    echo
+    exit 1
+}
 
-withAllclean=true
+#------------------------------------------------------------------------------
 
+# Parse options
+unset skipSelf clean0
 if [ "$#" -gt 0 ]
 then
     case "$1" in
     -h | -help*)
-        echo "${0##*/}: recursively clean an OpenFOAM case directory" 1>&2
-        exit 0
+        printHelp
+        ;;
+    -0)
+        clean0=true
+        ;;
+    -case)
+        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
+        shift
+        cd "$1" 2>/dev/null || {
+            echo "${0##*}: No such directory $1" 1>&2
+            exit 2
+        }
+        ;;
+    -self*)
+        skipSelf=true
         ;;
-    -self | -*)
-        unset withAllclean
+    --)
+        shift
+        break
         ;;
     *)
         cd "$1" 2>/dev/null || {
-            echo "${0##*}: No such directory" 1>&2
+            echo "${0##*}: No such directory $1" 1>&2
             exit 2
         }
         ;;
@@ -75,18 +97,34 @@ then
 fi
 
 
-if [ -n "$withAllclean" ] && [ -f Allwclean ]
+unset exitCode
+if [ -z "$skipSelf" ]
 then
-    # Specialized script
-    ./Allwclean
-elif [ -n "$withAllclean" ] && [ -f Allclean ]
+    # Use specialized script(s)
+    if [ -f Allwclean ]
+    then
+        ./Allwclean
+        exitCode="$?"
+    elif [ -f Allclean ]
+    then
+        ./Allclean
+        exitCode="$?"
+    fi
+fi
+
+
+if [ -n "$exitCode" ]
 then
-    # Specialized script
-    ./Allclean
+    exit "$exitCode"
 elif [ -d system ]
 then
     # Normal case
-    cleanCase
+    if [ "$clean0" = true ]
+    then
+        cleanCase0
+    else
+        cleanCase
+    fi
 elif [ -d Make ]
 then
     # Normal application
@@ -95,7 +133,7 @@ else
     # Recurse into subdirectories
     for caseName in *
     do
-        ( cd $caseName 2>/dev/null && $thisScript )
+        ( cd $caseName 2>/dev/null && "$thisScript" )
     done
 fi
 
diff --git a/bin/foamRunTutorials b/bin/foamRunTutorials
index fd063b5036f..27553b651d2 100755
--- a/bin/foamRunTutorials
+++ b/bin/foamRunTutorials
@@ -7,18 +7,17 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011-2016 OpenFOAM Foundation
-#     Copyright (C) 2018-2019 OpenCFD Ltd.
+#     Copyright (C) 2018-2020 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
-#     This file is part of OpenFOAM, licensed under GNU General Public License
-#     <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # Script
 #     foamRunTutorials
 #
 # Description
-#     Run either Allrun or blockMesh/application in current directory
-#     and all its subdirectories.
+#     Recursively run Allrun/Alltest or blockMesh+application,
+#     starting with the current directory or the specified -case directory.
 #
 #     For tutorials that are known to run poorly, an Allrun-optional
 #     placeholder can be used instead of the usual Allrun script.
@@ -36,20 +35,67 @@ then
     thisScript="$PWD/$thisScript"
 fi
 
-unset passArgs runTests skipFirst
+printHelp() {
+    cat <<USAGE
+
+Usage: ${0##*/} [OPTION]
+options:
+  -case <dir>       specify starting directory, default is cwd
+  -self | -skipFirst avoid Allrun, Alltest script (prevent infinite recursion)
+  -test             prefer Alltest script, pass -test argument to scripts
+  -help             print the usage
+
+Recursively run Allrun/Alltest or blockMesh+application,
+starting with the current directory or the specified -case directory.
+
+USAGE
+   exit 0  # 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 '${0##*/} -help' for usage"
+    echo
+    exit 1
+}
+
+#------------------------------------------------------------------------------
 
 # Parse options
+unset passArgs runTests skipSelf
+
 while [ "$#" -gt 0 ]
 do
     case "$1" in
-    -t | -test)
+    -h | -help*)
+        printHelp
+        ;;
+    -case)
+        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
+        shift
+        cd "$1" 2>/dev/null || {
+            echo "${0##*}: No such directory $1" 1>&2
+            exit 2
+        }
+        ;;
+    -test)
         passArgs="-test"
         runTests=true
         ;;
 
-    # Avoid infinite recursion when invoked from an Allrun or Alltest script
-    -s | -skipFirst)
-        skipFirst=true
+    # Avoid infinite recursion when invoked from an Allrun/Alltest script
+    -self* | -skipFirst)
+        skipSelf=true
+        ;;
+    --)
+        shift
+        break
         ;;
     *)
         break
@@ -58,18 +104,30 @@ do
     shift
 done
 
-if [ -z "$skipFirst" ] && [ -n "$runTests" ] && test -f Alltest
-then
-    # Run specialized Alltest script
-    ./Alltest $passArgs $*
-elif [ -z "$skipFirst" ] && test -f Allrun
+
+unset exitCode
+if [ -z "$skipSelf" ]
 then
-    # Run specialized Allrun script
-    ./Allrun $passArgs $*
-elif [ -z "$skipFirst" ] && test -f Allrun-optional
+    # Use specialized script(s)
+    if [ "$runTests" = true ] && [ -f Alltest ]
+    then
+        ./Alltest $passArgs $*
+        exitCode="$?"
+    elif [ -f Allrun ]
+    then
+        ./Allrun $passArgs $*
+        exitCode="$?"
+    elif [ -f Allrun-optional ]
+    then
+        echo "Skipped optional case $PWD"
+        exitCode=0
+    fi
+fi
+
+
+if [ -n "$exitCode" ]
 then
-    # Has Allrun-optional script - skip this tutorial.
-    echo "Skipped optional case $PWD"
+    exit "$exitCode"
 elif [ -d system ]
 then
     # Run normal case with blockMesh and the application
@@ -79,7 +137,7 @@ else
     # Loop over sub-directories and compile any applications
     for caseName in *
     do
-        if [ -d "$caseName" ] && [ -d "$caseName/Make" ]
+        if [ -d "$caseName/Make" ]
         then
             ( compileApplication "$caseName" )
         fi
-- 
GitLab