#!/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

# Make sure we disable all features that require MPI:
${icon_dir}/config/mpim/${linux_codename}.intel --disable-mpi --disable-parallel-netcdf --disable-coupling --disable-yaxt --disable-cdi-pio --disable-comin --enable-openmp --enable-separate-mods

time make ${makeflags}

if test -n "${BB_NAME-}"; then
  "${icon_dir}/scripts/buildbot_scripts/build_checks/check_nompi.sh"
  "${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
