#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2020 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# 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/>.
#
# Script
#     Alltest
#
# Description
#     quickly tests the tutorials
#
#------------------------------------------------------------------------------
cd "${0%/*}" || exit 1 # Run from this directory

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

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

options:
  -root <dir>       specify root directory to run tests from
  -help             print the usage

* quickly tests the tutorials

USAGE
    exit 1
}

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

rootDir="./"
testDir=../tutorialsTest


# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -r | -root)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        rootDir="$2"
        shift
        ;;
    -h | -help)
        usage
        ;;
    -*)
        usage "unknown option: '$*'"
        ;;
    *)
        usage "unknown option/argument: '$*'"
        ;;
    esac
    shift
done


# Create a copy of the tutorials in which to run the test loop
echo "Copying the tutorials"
if [ -d "$testDir" ]
then
    rm -rf "$testDir" || exit 1
fi
cp -a "${rootDir}" "${testDir}" || exit 1


# Change the control dict to make the tests quick
echo "Modifying the controlDicts to run only one time step"
cd ${testDir} || exit 1
find . -name "controlDict*" | while read -r controlDict
do
    (
        foamDictionary -entry startFrom -set latestTime "$controlDict"
        foamDictionary -entry stopAt -set nextWrite "$controlDict"
        foamDictionary -entry writeControl -set timeStep "$controlDict"
        foamDictionary -entry writeInterval -set 1 "$controlDict"
        foamDictionary -entry DebugSwitches \
            -merge "{ fvSchemes 1; solution 1; }" "$controlDict"
    ) > /dev/null
done


# Copy the Allrun script into the test directory
cp -f "$FOAM_TUTORIALS"/Allrun . || exit 1


# Run everything
./Allrun && exit 0 || exit 1

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