#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# ==  == ====== ====   ====    |
#                   \\     ||  | Multiphase Code Repository by HZDR
# ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
# ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
# ==  == ====== ====   ==  ==  |
#------------------------------------------------------------------------------
# License
#     This file is part of the Multiphase Code Repository by HZDR.
#
#     Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
#     Website: https://hzdr.de
#
#     Multiphase Code Repository by HZDR is based on the free software for
#     computational fluid dynamics (CFD) from the OpenFOAM Foundation.
#     Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org
#
#     If you are interested in which files are original OpenFOAM Foundation
#     files, which OpenFOAM Foundation files were modified, and which files were
#     newly created, see FILES.md.
#
#     Multiphase Code Repository by HZDR 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.
#
#     Multiphase Code Repository by HZDR 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 the Multiphase Code Repository by HZDR. If not, see
#     <http://www.gnu.org/licenses/>.
#
# Description
#     Extended Alltest script for quickly testing the tutorials. Allows to just
#     configure setups to run a single time step without actually executing
#     them.
#
#------------------------------------------------------------------------------
cd "${0%/*}" || exit 1 # Run from this directory

usage()
{
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

usage: ${0##*/} [OPTION]

options:
  -from <dir>       specify directory to run tests from
  -to <dir>         specify directory to run tests in
  -configure        configure setups for test run
  -help             print the usage

* quickly tests the tutorials

USAGE
    exit 1
}

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

currentDir="$(pwd)"
fromDir="./"
toDir=../tutorialsTest
configure=""

# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -f | -from)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        fromDir="$2"
        shift
        ;;
    -t | -to)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        toDir="$2"
        shift
        ;;
    -c | -configure)
        configure="true"
        ;;
    -h | -help)
        usage
        ;;
    -*)
        usage "unknown option: '$*'"
        ;;
    *)
        usage "unknown option/argument: '$*'"
        ;;
    esac
    shift
done

if [ $(readlink -f $fromDir) != $(readlink -f $toDir) ]
then
    # Create a copy of the cases for which to run the test loop
    echo "Copying cases"
    if [ -d "$toDir" ]
    then
        rm -rf "$toDir" || exit 1
    fi
    cp -a "${fromDir}" "${toDir}" || exit 1
fi

# Change the control dict to make the tests quick
echo "Modifying the controlDicts to run only one time step"
cd ${toDir} || exit 1
find . -name "controlDict*" | while read -r controlDict
do
    (
        # Make sure to find a proper start time
        foamDictionary -entry startFrom -set latestTime "$controlDict"
        # Stop simulation at next write
        foamDictionary -entry stopAt -set nextWrite "$controlDict"
        # Set write control to time steps
        foamDictionary -entry writeControl -set timeStep "$controlDict"
        # Set one time step as next write point
        foamDictionary -entry writeInterval -set 1 "$controlDict"
        # Enable some debug switches to speed up tests
        foamDictionary -entry DebugSwitches \
            -merge "{ fvSchemes 1; solution 1; }" "$controlDict"
    ) &> /dev/null
done
find . -name "functions*" | while read -r functions
do
    (
        # Expand functions and include all #include and #includeFunc
        foamDictionary -expand -output "$functions" "$functions"
        for keyword in $(foamDictionary -keywords "$functions");
        do
            # Set startTime to zero
            foamDictionary -entry "$keyword"/startTime -set 0 "$functions"
            # Set writeControl to time steps
            foamDictionary -entry "$keyword"/writeControl \
                -set timeStep "$functions"
            # Set writeInterval to single time step
            foamDictionary -entry "$keyword"/writeInterval \
                -set 1 "$functions"
        done
    ) &> /dev/null
done

# Copy the Allrun script into the test directory
if [ ! -x "$toDir"/Allrun ]
then
    if [ -x "$currentDir"/Allrun ]
    then
        cp -f "$currentDir"/Allrun . || exit 1
    else
        cp -f "$FOAM_TUTORIALS"/Allrun . || exit 1
    fi
fi

# Run everything
if [ ! "$configure" ]
then
    ./Allrun && exit 0 || exit 1
else
    exit 0
fi

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