#!/bin/bash

# Compile
(cd ./sources/amip-forcing/src/fortran && make clean) && : || exit 1
(cd ./sources/amip-forcing/src/fortran && make oasis) && : || exit 1
(cd ./sources/amip-forcing/src/fortran && make) && : || exit 1
(cd ./sources/amip-primavera/src/fortran && make clean) && : || exit 1
(cd ./sources/amip-primavera/src/fortran && make) && : || exit 1

if [ $# -ne 1 ]; then
    echo 'Running tests WITHOUT the large test. Use ./run_tests.sh 1 to run all tests including the large test.'
else
    echo 'Running tests WITH the large test.'
fi

cd ./tests

echo '*****************************************************************'
echo "Running tests..."
# Clean old tests
rm -rf ./work_*
rm -rf .coverage*

tests_args=(
# Standard test
"forcing 4 1990-01-01 1991-01-01 0 L128 true"
# Standard test (for Python backwards compatibility)
"forcing 2 1991-01-01 1991-03-01 0 L128 true"
# Standard test (PRIMAVERA)
"primavera 2 1995-08-01 1996-03-01 0 L128 false"
# Standard test (PRIMAVERA) (for Python backwards compatibility)
"primavera 2 1991-08-01 1992-03-01 0 L128 false"
# Standard test L080
"forcing 2 1991-01-01 1991-03-01 0 L080 true"
# Standard test L256
"forcing 2 1991-01-01 1991-03-01 0 L256 true"
# Standard test without interpolation
"forcing 2 1990-01-01 1991-01-01 0 L128 false"
# Secuential years
"forcing 2 1990-01-01 1994-01-01 0 L128 true"
# Leap year fixed year
"forcing 2 1999-12-01 2000-03-02 2000 L128 true"
# Fixed year
"forcing 2 1995-01-01 1998-01-01 1 L128 true"
# Years before 1900 (leap-year)
"forcing 2 1892-01-01 1894-01-01 0 L128 true"
)
# LARGE TEST: All years between 1979 and 2014
if [ $1 ]; then
    tests_args+=("forcing 2 1979-01-01 2014-01-01 0 L128")
fi
for ((i = 0; i < "${#tests_args[@]}"; i++)); do
    args="${tests_args[i]}"
    ./run_example.sh fortran $args > /dev/null &
    # Test Python's backwards compatibility
    if [ $i = 1 ] || [ $i = 3 ]; then
        ./run_example.sh pythoncompat $args > /dev/null &
    else
        ./run_example.sh python $args > /dev/null &
    fi
done

# Wait for tests
for job in `jobs -p`; do
    wait $job
done

echo '*****************************************************************'
echo "Checking results..."

for args in "${tests_args[@]}"; do
    args="${args// /_}"
    python_dir="./work_python_"$args
    fortran_dir="./work_fortran_"$args
    pythoncompat_dir="./work_pythoncompat_"$args
    if [ -d $pythoncompat_dir ]; then
        python_dir=$pythoncompat_dir
    fi
    echo 'Checking '${python_dir}' vs '${fortran_dir}
    cdo diff ${python_dir}/AMIP_sic_AMIPFORC_*.nc ${fortran_dir}/AMIP_sic_AMIPFORC_*.nc && : || exit 1
    cdo diff ${python_dir}/AMIP_sst_AMIPFORC_*.nc ${fortran_dir}/AMIP_sst_AMIPFORC_*.nc && : || exit 1
    cdo diff ${python_dir}/A_Ice_frac_IFS_TOY_*.nc ${fortran_dir}/A_Ice_frac_IFS_TOY_*.nc && : || exit 1
    cdo diff ${python_dir}/A_SST_IFS_TOY_*.nc ${fortran_dir}/A_SST_IFS_TOY_*.nc && : || exit 1
    cat ${python_dir}/IFS_toy.out_* > ${python_dir}/IFS_toy__all.out
    cat ${fortran_dir}/IFS_toy.out_* > ${fortran_dir}/IFS_toy__all.out
    diff ${fortran_dir}/IFS_toy__all.out ${python_dir}/IFS_toy__all.out && : || exit 1
done

echo "Tests PASSED"

echo "Running unverifiable examples (without Fortran implementation)"
# CO2 data
./run_example.sh python co2 2 2013-05-06 2014-01-01 0 L128 true > /dev/null &
# PRIMAVERA with fixed year
./run_example.sh python primavera 2 1995-08-01 1996-03-01 1995 L128 false > /dev/null &
for job in `jobs -p`; do
    wait $job
done

echo "Done"
