#!/bin/bash -l
# ============================================================================
#  jobs/rcms_job.sh  -  Wrapper SLURM TINH (khong sinh file vao thu muc he cu).
#
#  Duoc nop boi run_rcms.sh qua sbatch, voi tai nguyen (nodes/time/...) truyen
#  qua tuy chon dong lenh cua sbatch. Tham so cua script chinh la subcommand noi
#  bo cua run_rcms.sh, vi du:
#      sbatch [--nodes=.. --time=.. --output=..] jobs/rcms_job.sh _integrate_clwrf S E
#
#  Script nay nam trong thu muc he thong (Auto_RCMs/jobs) va chi GOI lai
#  run_rcms.sh; no khong tao/ghi bat cu file nao ngoai duong xu ly cua module.
#
#  LUU Y QUAN TRONG: khi nop qua sbatch, SLURM COPY script nay vao
#  /var/spool/slurmd/<job>/slurm_script roi moi chay, nen ${BASH_SOURCE[0]}
#  KHONG con tro toi cay thu muc goc (Auto_RCMs). Vi vay phai UU TIEN dung
#  RCMS_HOME da duoc run_rcms.sh export san (truyen qua 'sbatch --export=ALL');
#  chi tu suy ra tu BASH_SOURCE khi chay TRUC TIEP (khong qua SLURM).
# ============================================================================
set -euo pipefail
if [[ -z "${RCMS_HOME:-}" || ! -x "${RCMS_HOME:-}/run_rcms.sh" ]]; then
    RCMS_HOME="$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)"
fi
export RCMS_HOME
export RCMS_CONF="${RCMS_CONF:-$RCMS_HOME/config/rcms.conf}"
[[ -x "$RCMS_HOME/run_rcms.sh" ]] \
    || { echo "ERROR: khong tim thay run_rcms.sh trong RCMS_HOME=$RCMS_HOME" >&2; exit 1; }
exec "$RCMS_HOME/run_rcms.sh" "$@"
