#!/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 -euo pipefail
set -x

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)
ICON_DIR=$(cd "${SCRIPT_DIR}/../.."; pwd)

#DONT TOUCH
#  this logfile name is used in by buildbot
LOGFILE="${ICON_DIR}/LOG.buildModel.o"
MAKE_PROCS=22

[[ -f ${LOGFILE} ]] && rm ${LOGFILE}

# slurm specific stuff is set here to keep the jobscripts as general as possible
# time limits as of now:
# - buildbot timeout after 30mins of buildbing without generating any input (server-side)
# - 1.5 hours time limit after job submission with slurm
# - together with roughly 30min build time, we allow 60min queuing time
DEADLINE="--deadline=now+90minutes"
RUNTIME_LIMIT="--time=01:00:00"
MINIMAL_TIME="--time-min=00:30:00"
TIME_LIMITS="${RUNTIME_LIMIT} ${MINIMAL_TIME} ${DEADLINE}"

#unusable buildername: it has to be set properly by the buildbot server
BB_NAME=${BB_NAME-unknown}

case ${BB_NAME} in
  levante_aurora)
    cd ${ICON_DIR} && ${ICON_DIR}/config/buildbot/buildModel.sbatch > ${LOGFILE} 2>&1
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  levante*)
    sbatch ${TIME_LIMITS} --wait -A mh0156 --qos=buildbot -p shared,compute -n ${MAKE_PROCS} -o ${LOGFILE} -D $(pwd) ${ICON_DIR}/config/buildbot/buildModel.sbatch
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  balfrin*|tasna*|alps_mch*) # alps_mch
    cd ${ICON_DIR} && ${ICON_DIR}/config/buildbot/buildModel.sbatch > ${LOGFILE} 2>&1
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  breeze*|mpimac*)
    # MPIMET workstation (and macs) do not have any scheduler - local execution
    cd ${ICON_DIR} && ${ICON_DIR}/config/buildbot/buildModel.sbatch > ${LOGFILE} 2>&1
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  dwd*)
    # DWD might avoid job submission for building the model
    cd ${ICON_DIR} && ${ICON_DIR}/config/buildbot/buildModel.sbatch > ${LOGFILE} 2>&1
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  cluster_*|booster_*)
    # JSC cpu and gpu setups - no queue known, yet
    cd ${ICON_DIR} && ${ICON_DIR}/config/buildbot/buildModel.sbatch > ${LOGFILE} 2>&1
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  horeka_*)
    # KIT horeka: use quarter of a shared node
    sbatch ${TIME_LIMITS} --wait -A hk-project-glori-init -p dev_cpuonly --qos=cx -t 60 -N 1 -n 19 -o ${LOGFILE} -D ${PWD} ${ICON_DIR}/config/buildbot/buildModel.sbatch
    #cd ${ICON_DIR} && ${ICON_DIR}/config/buildbot/buildModel.sbatch > ${LOGFILE} 2>&1
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  lumi_cpu*)
    # CSC super computer; CPU partition
    sbatch ${TIME_LIMITS} --wait -A project_462000911 -p debug --time=00:30:00 -n ${MAKE_PROCS} -o ${LOGFILE} -D $(pwd) ${ICON_DIR}/config/buildbot/buildModel.sbatch
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  lumi_gpu*)
    # CSC super computer; GPU partition
    sbatch ${TIME_LIMITS} --wait -A project_465000454 -p dev-g -n ${MAKE_PROCS} -o ${LOGFILE} -D $(pwd) ${ICON_DIR}/config/buildbot/buildModel.sbatch
    if test 0 -ne "$?" ; then
      exit 1
    fi
    ;;
  *)
    echo "unknown builder ${BB_NAME}!"
    exit 1;
    ;;
esac
