#!/usr/bin/env 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
# ------------------------------------------

# this is supposed to be a replacement of create_all_builders in order to speed
# things up by creating on the builder that is requested by builderbot. Hence
# this needs rework to make it usable interactively
#
# This script is not in production, yet. But will be kept for further development

# abort on error
set -euo pipefail

set -x
# create all builders for a given list
list=$1
if [ x$list == x ]; then
  echo "Builbot list name missing"
  exit 1
fi

# lower case is used for build wrappers
bb_name=$(echo $BB_NAME | tr '[:upper:]' '[:lower:]')

# set build info according to the builder name
configureflags=""
#TODOcase $bb_name in
#TODO  *serial*)
#TODO    configureflags+=" --without-mpi"
#TODO    ;&
#TODO  *openmp*|*hybrid*)
#TODO    configureflags+=" --with-openmp"
#TODO    ;&
#TODO  *mixed*)
#TODO    configureflags+=" --enable-mixed"
#TODO    ;&
#TODO  *gpu*|*GPU*|*booster*)
#TODO    configureflags+=" --without-openmp"
#TODO    ;&
#TODOesac
# case-fall-through version without case (needed because apple puts an ancient bash-3.x on their systems)
[[ ! -z $(echo ${bb_name/serial/NoBadNotes}   | grep 'NoBadNotes') ]] && configureflags+=" --without-mpi"
[[ ! -z $(echo ${bb_name/hybrid/NoBadNotes}   | grep 'NoBadNotes') ]] && configureflags+=" --with-openmp"
[[ ! -z $(echo ${bb_name/mixed/NoBadNotes}    | grep 'NoBadNotes') ]] && configureflags+=" --enable-mixed"
[[ ! -z $(echo ${bb_name/gpu/NoBadNotes}      | grep 'NoBadNotes') ]] && configureflags+=" --without-openmp"
[[ ! -z $(echo ${bb_name/booster/NoBadNotes}  | grep 'NoBadNotes') ]] && configureflags+=" --without-openmp"

[[ -z ${configureflags} ]] && configureflags=" --with-mpi"

./addmachine $BB_SYSTEM --list $list
./addbuilder $BB_NAME \
  --machine $BB_SYSTEM \
  --build_script="./config/buildbot/${bb_name}" \
  --configureflags="${configureflags}" \
  --builderflags="Active" \
  --list $list
