#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2011-2016 OpenFOAM Foundation
#     Copyright (C) 2018-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     foamRunTutorials
#
# Description
#     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.
#     When this is detected, the case will be skipped.
#
#------------------------------------------------------------------------------
. ${WM_PROJECT_DIR:?}/bin/tools/RunFunctions    # Tutorial run functions

# Normally use standard "make"
make="make"

thisScript="$0"
if [ "/${thisScript#/}" != "$thisScript" ]
then
    thisScript="$PWD/$thisScript"
fi

printHelp() {
    cat <<USAGE

Usage: ${0##*/} [OPTION]
options:
  -case <dir>       specify starting directory, default is cwd
  -self             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.
The -skipFirst option is the same as -self.

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
    -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/Alltest script
    -self* | -skipFirst)
        skipSelf=true
        ;;
    --)
        shift
        break
        ;;
    *)
        break
        ;;
    esac
    shift
done


unset exitCode
if [ -z "$skipSelf" ]
then
    # 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
    exit "$exitCode"
elif [ -d system ]
then
    # Run normal case with blockMesh and the application
    runApplication blockMesh
    runApplication $(getApplication)
else
    # Loop over sub-directories and compile any applications
    for caseName in *
    do
        if [ -d "$caseName/Make" ]
        then
            ( compileApplication "$caseName" )
        fi
    done
    FOAM_TARGETS=$(for d in *; do [ -d "$d" ] && echo "$d"; done | xargs)

    # Run all cases which have not already been run
    $make -k -f $WM_PROJECT_DIR/bin/tools/MakefileDirs \
          FOAM_TARGETS="$FOAM_TARGETS" \
          FOAM_APP="$thisScript" FOAM_ARGS="$passArgs $*"
fi

#------------------------------------------------------------------------------
