#!/bin/bash

# ICON
#
# ------------------------------------------
# Copyright (C) 2004-2026, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
# Contact information: icon-model.org
# See AUTHORS.TXT for a list of authors
# See LICENSES/ for license information
# SPDX-License-Identifier: BSD-3-Clause
# ------------------------------------------

set -eu

unset CDPATH
script_dir=$(cd "$(dirname "$0")"; pwd)
icon_dir=$(cd "${script_dir}/../.."; pwd)

linux_codename=$(lsb_release -sc)

unset MAKEFLAGS
makeflags='--jobs=8 --no-print-directory --output-sync=target V=1'

set -x

# Check whether we can build with RTTOV and without DACE and EMVORADO:
#  TODO: this is a temporary quick and dirty solution. We should run
#  './run/checksuite.build/exp.check_rttov_BREEZE.run' as an experiment
#  instead.
if test -n "${BB_NAME-}"; then
  (
    mkdir -p build && cd build
    ${icon_dir}/config/mpim/${linux_codename}.nag --enable-rttov --disable-dace --disable-emvorado
    make ${makeflags}
  )
fi

${icon_dir}/config/mpim/${linux_codename}.nag --enable-yaxt --disable-openmp --enable-rte-rrtmgp --enable-serialization --enable-rttov --enable-dace --enable-emvorado

time make ${makeflags}

if test -n "${BB_NAME-}"; then
  "${icon_dir}/scripts/buildbot_scripts/build_checks/check_depgen_warnings.sh"
  "${icon_dir}/scripts/buildbot_scripts/build_checks/check_remake.sh"
  # Build the tests in parallel:
  time make ${makeflags} --keep-going TESTS= check
  # Run the tests serially:
  time make --keep-going ICON_VERBOSE_TESTS=1 ICON_COLOR_TESTS=always \
    TESTS_ENVIRONMENT='echo "$${mgn}RUNNING$${std}: $$f"; st=$$(date +%s.%N);' \
    AM_TESTS_FD_REDIRECT=';printf "$${brg}duration$${std}: %s: %0.2fs\n" "$$f" "$$(echo "$$(date +%s.%N) - $${st}" | bc -l)"' \
    check
  "${icon_dir}/scripts/buildbot_scripts/build_checks/check_git_untracked.sh"
fi
