#!/usr/bin/env bash
set -feu -o pipefail

# /hysop should be mounted as read only by run_tests_in_docker.sh
if [[ ! -d '/hysop' ]]; then
    echo "This script should not be called from host, but from within a docker image."
    echo " => /hysop has not been mounted (see hysop/ci/utils/run_ci.sh)."
    exit 1
fi

CC=gcc
CXX=g++
FC=gfortran

HYSOP_DIR='/tmp/hysop'
HYSOP_BUILD_DIR="${HYSOP_DIR}/build"
HYSOP_INSTALL_DIR='/opt/hysop'

cp -r /hysop "${HYSOP_DIR}"
rm -rf "${HYSOP_BUILD_DIR}"

SCRIPT_DIR="${HYSOP_DIR}/ci/scripts"
cd "${HYSOP_DIR}"
${SCRIPT_DIR}/version.sh
${SCRIPT_DIR}/config.sh "${HYSOP_BUILD_DIR}" "${HYSOP_INSTALL_DIR}" "${CC}" "${CXX}" "${FC}"
${SCRIPT_DIR}/build.sh "${HYSOP_BUILD_DIR}" "${CC}" "${CXX}" "${FC}"
${SCRIPT_DIR}/install.sh "${HYSOP_BUILD_DIR}" "${HYSOP_INSTALL_DIR}"
time ${SCRIPT_DIR}/test.sh "${HYSOP_INSTALL_DIR}" "${HYSOP_DIR}/hysop"

# clean everything because image may be commited to retain hysop cache
cd
rm -rf "${HYSOP_DIR}" "${HYSOP_INSTALL_DIR}"
