#!/usr/bin/env bash
# Build validated monthly ECMWF files sequentially as split raw pairs arrive.
# Usage: bash scripts/build_monthly_baseline.sh [START_YYYY-MM] [END_YYYY-MM]
set -u -o pipefail

project_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
start_month=${1:-1981-01}
end_month=${2:-2020-12}
poll_seconds=${MONTHLY_POLL_SECONDS:-300}
raw_dir="$project_root/data/ecmwf/raw_split"
monthly_dir="$project_root/data/ecmwf/monthly"
log_dir="$project_root/outputs/logs"
run_log="$log_dir/monthly_baseline.log"
lock_file="$log_dir/monthly_baseline.lock"
pid_file="$log_dir/monthly_baseline.pid"

mkdir -p "$log_dir" "$monthly_dir"
exec 9>"$lock_file"
if ! flock -n 9; then
  printf '%s Another monthly worker holds %s; exiting.\n' "$(date -Is)" "$lock_file" >>"$run_log"
  exit 75
fi
printf '%s\n' "$$" >"$pid_file"
trap 'rm -f "$pid_file"' EXIT

if ! [[ "$start_month" =~ ^[0-9]{4}-(0[1-9]|1[0-2])$ ]] || ! [[ "$end_month" =~ ^[0-9]{4}-(0[1-9]|1[0-2])$ ]] || ! [[ "$poll_seconds" =~ ^[1-9][0-9]*$ ]]; then
  printf 'Invalid month range or MONTHLY_POLL_SECONDS=%s\n' "$poll_seconds" >&2
  exit 2
fi
if [[ "$start_month" > "$end_month" ]]; then
  printf 'START must not be after END\n' >&2
  exit 2
fi

month_after() {
  date -d "${1}-01 +1 month" +%Y-%m
}

printf '%s START range=%s..%s poll_seconds=%s\n' "$(date -Is)" "$start_month" "$end_month" "$poll_seconds" >>"$run_log"
current_month=$start_month
while [[ "$current_month" < "$end_month" || "$current_month" == "$end_month" ]]; do
  instantaneous="$raw_dir/ecmwf_seas51_${current_month}_instantaneous.nc"
  accumulated="$raw_dir/ecmwf_seas51_${current_month}_accumulated.nc"
  output="$monthly_dir/ecmwf_seas51_${current_month}_monthly.nc"
  if [[ ! -f "$instantaneous" || ! -f "$accumulated" ]]; then
    instantaneous_ready=$([[ -f "$instantaneous" ]] && printf yes || printf no)
    accumulated_ready=$([[ -f "$accumulated" ]] && printf yes || printf no)
    printf '%s WAIT init=%s instantaneous=%s accumulated=%s; sleeping %ss\n' "$(date -Is)" "$current_month" "$instantaneous_ready" "$accumulated_ready" "$poll_seconds" >>"$run_log"
    sleep "$poll_seconds"
    continue
  fi
  printf '%s PROCESS init=%s\n' "$(date -Is)" "$current_month" >>"$run_log"
  if PYTHONDONTWRITEBYTECODE=1 PYTHONPATH="$project_root/src" python3 -m dr_gcm.monthly --init "$current_month" --instantaneous "$instantaneous" --accumulated "$accumulated" --output "$output" >>"$run_log" 2>&1; then
    printf '%s SUCCESS init=%s\n' "$(date -Is)" "$current_month" >>"$run_log"
  else
    status=$?
    printf '%s FAILED init=%s exit=%s; stopping without skipping\n' "$(date -Is)" "$current_month" "$status" >>"$run_log"
    exit "$status"
  fi
  current_month=$(month_after "$current_month")
done
printf '%s COMPLETE range=%s..%s\n' "$(date -Is)" "$start_month" "$end_month" >>"$run_log"
