#!/bin/bash
#
#   Executable script that copies tests from TestCode dir to source, and
#   starts server or client unit tests
#
#   Requires arguments
#     $INSTALLROOT - should contain directories TestCode, ServerInstallDIR,
#                    ClientInstallDIR
#     $INSTALLTYPE - client or server, deciding which tests to run
#
#.........................................................................

if [[ -z $INSTALLROOT || -z $INSTALLTYPE ]]; then
    echo "Arguments missing. " >&2
    exit 1
fi

cd "$INSTALLROOT" || exit 1

# shellcheck source=tests/CI/CONFIG
source CONFIG
source "${DIRAC_CI_SETUP_SCRIPT}"

echo -e "*** $(date -u) **** Starting integration tests on ${INSTALLTYPE} ****\n"

if [[ "$INSTALLTYPE" == "server" ]]; then
    # shellcheck source=/dev/null
    source /home/dirac/ServerInstallDIR/bashrc
    # If not unset, assert will not trigger
    unset PYTHONOPTIMIZE
    # shellcheck disable=SC2034
    SERVER_TEST_OUTPUT=serverTestOutputs.txt
    set -o pipefail
    ERR=0
    for repo_path in "${TESTREPO[@]}"; do
        # TODO: The tests should be refactored to remove the need for this
        cp -r "${repo_path}/tests" "/home/dirac/ServerInstallDIR/$(basename "${repo_path}")"
    done
    for repo_path in "${TESTREPO[@]}"; do
        source "${repo_path}/tests/Integration/all_integration_server_tests.sh"
    done
elif [[ "$INSTALLTYPE" == "client" ]]; then
    # shellcheck source=/dev/null
    source /home/dirac/ClientInstallDIR/bashrc
    # If not unset, assert will not trigger
    unset PYTHONOPTIMIZE
    set -o pipefail
    ERR=0
    for repo_path in "${TESTREPO[@]}"; do
        source "${repo_path}/tests/Integration/all_integration_client_tests.sh"
    done

# if "$INSTALLTYPE" == "pilot" and if INSTALLATION_BRANCH is not set
elif [[ "$INSTALLTYPE" == "pilot" ]] && [[ -z "${INSTALLATION_BRANCH}" ]]; then
    # shellcheck source=/dev/null
    source /home/dirac/bashrc
    # If not unset, assert will not trigger
    unset PYTHONOPTIMIZE
    set -o pipefail
    ERR=0
    for repo_path in "${TESTREPO[@]}"; do
        source "${repo_path}/tests/Integration/all_integration_pilot_tests.sh"
    done

fi

echo -e "*** $(date -u) **** TESTS OVER ****\n"

if [[ -z "$ERR" ]]; then
    echo "WARN: Variable \$ERR not defined, check the test logs for possible failed tests"
    exit 0
elif [[ "$ERR" != "0" ]]; then
   echo "ERROR: At least one unit test in ${INSTALLTYPE} failed !!!" >&2
   exit "$ERR"
else
   echo "SUCCESS: All tests succeded"
   exit 0
fi
