#!/usr/bin/env bash
# run_main.sh: run and score the PHerc0332 spiral fit on villa main from the PUBLISHED files, with no workstation paths.
#
#   run_main.sh score --data DATA --src SRC --villa VILLA --out OUT [--seed 1|2]
#       rescore our published result (DATA/main/results/s<seed>) with the frozen scorer on CPU and compare it with the
#       published scores_trim.json. A few minutes. Exit 0 = identical apart from relocated paths.
#   run_main.sh fit --data DATA --src SRC --villa VILLA --lasagna LAS --out OUT [--prefix MSEM6|SMK22] [--seeds "1 2"] [--author NAME]
#       stage A (from scratch), stage B (resumed from A), then for each scored stage a 0-step pass with the held-back
#       patches loaded, the frozen scorer, and hard checks. MSEM6 = the real fit (10,000 + 15,000 steps, about 4.5 h per
#       seed on a 12 GB GPU); SMK22 = a 200 + 200 step smoke test of the same chain (seed 1 only).
#
# DATA    = the downloaded dataset folder (.../PHerc0332/20251211183505) holding main/, umbilicus/, anchors/, slice_lines/,
#           eval/frozen_s1_scorer/ and repro/compare_scores.py
# SRC     = the extracted frozen source (tar -xf DATA/eval/frozen_source_s1_f327248db2c1f694.tar.zst -C SRC)
# VILLA   = a clone of github.com/ScrollPrize/villa at f29334cd8 with `uv sync` done in spiral-fitting/
# LAS     = the folder with the Lasagna manifest (*.lasagna.json) and its nx / ny / grad_mag zarrs (open data)
# OUT     = a new or reusable work folder (needs about 25 GB: a working copy of the store, the cache and the run folders)
# The same Python environment (VILLA/spiral-fitting/.venv) runs the fit and the frozen scorer.
# Relocation of the frozen scorer follows repro/run_reference.sh: in the copied work/ scripts ONLY the five workstation
# path constants are replaced, and no /media/djosey or /home/djosey string may remain afterwards.
set -euo pipefail
MODE=${1:?score|fit}; shift
DATA= SRC= VILLA= LAS= OUT= SEED=1 SEEDS="1 2" P=MSEM6 AUTHOR=${USER:-replay}
while [ $# -gt 0 ]; do case $1 in
  --data) DATA=$2; shift 2;; --src) SRC=$2; shift 2;; --villa) VILLA=$2; shift 2;; --lasagna) LAS=$2; shift 2;;
  --out) OUT=$2; shift 2;; --seed) SEED=$2; shift 2;; --seeds) SEEDS=$2; shift 2;; --prefix) P=$2; shift 2;;
  --author) AUTHOR=$2; shift 2;; *) echo "unknown argument $1" >&2; exit 2;; esac; done
die() { echo "run_main: $*" >&2; exit 1; }
for v in DATA SRC VILLA OUT; do [ -n "${!v}" ] || die "--${v,,} is required"; done
DATA=$(readlink -f "$DATA"); SRC=$(readlink -f "$SRC"); VILLA=$(readlink -f "$VILLA"); mkdir -p "$OUT"; OUT=$(readlink -f "$OUT")
PY=$VILLA/spiral-fitting/.venv/bin/python; [ -x "$PY" ] || die "no environment at $PY (run uv sync in $VILLA/spiral-fitting)"
for p in "$DATA" "$SRC" "$OUT" "$PY"; do [[ "$p" =~ ^[A-Za-z0-9._/+-]*$ ]] || die "paths may contain only letters, digits and . _ / + - : $p"; done
[ -d "$SRC/work/s1" ] && [ -d "$SRC/spiral-fitting" ] || die "--src must be the extracted frozen source tar"
MAINF=$DATA/main; SCR=$MAINF/scripts; LINES=$DATA/slice_lines/PHerc0332_vcslices.volpkg.json
[ -d "$LINES" ] || die "slice lines missing: $LINES"
( cd "$MAINF" && grep -v 'm6seam22.vcobj$' SHA256SUMS | sha256sum -c --quiet ) || die "main/ files differ from SHA256SUMS"

# --- relocated frozen scorer (as run_reference.sh) ---
ND=$OUT/D; NW=$ND/work; F=$OUT/F
rm -rf "$NW" "$F"; mkdir -p "$ND/dataset" "$ND/out" "$NW" "$F" "$OUT/logs" "$OUT/overrides"
cp "$DATA/umbilicus/umbilicus.json" "$ND/dataset/umbilicus.json"
cp -a "$SRC/work/." "$NW/"
cp "$DATA/eval/frozen_s1_scorer/winding_gauge.json" "$NW/winding_gauge.json"
mkdir -p "$NW/s1_trim"; cp "$DATA/anchors/anchors_train_records.json" "$DATA/anchors/anchors_held_records.json" "$NW/s1_trim/"
ln -s "$NW" "$F/work"; ln -s "$SRC/spiral-fitting" "$F/spiral-fitting"
SUBST=(
  "s#/home/djosey/Documents/villa/spiral-fitting/.venv/bin/python#@@PY@@#g"
  "s#/home/djosey/Documents/villa/spiral-fitting#@@SPIRAL@@#g"
  "s#/media/djosey/nvme4/fibers/PHerc0332_vcslices.volpkg.json#@@LINES@@#g"
  "s#/media/djosey/nvme4/spiral_0332/work#@@WORK@@#g"
  "s#/media/djosey/nvme4/spiral_0332#@@D@@#g")
mapfile -t CODE < <(find "$NW" -type f \( -name '*.py' -o -name '*.sh' \))
for f in "${CODE[@]}"; do for e in "${SUBST[@]}"; do sed -i "$e" "$f"; done; done
left=$(grep -l -E '/(media|home)/djosey' "${CODE[@]}" || true)
[ -z "$left" ] || die "workstation paths remain after substitution in: $left"
for f in "${CODE[@]}"; do sed -i -e "s#@@PY@@#$PY#g" -e "s#@@SPIRAL@@#$SRC/spiral-fitting#g" -e "s#@@LINES@@#$LINES#g" \
  -e "s#@@WORK@@#$NW#g" -e "s#@@D@@#$ND#g" "$f"; done
score() {  # $1 run dir: the frozen trimmed scoring, on CPU
  ( cd "$F/work/s1" && SPIRAL_ROOT=$F/spiral-fitting WORK_ROOT=$F/work CUDA_VISIBLE_DEVICES= "$PY" "$F/work/s1/s1_score.py" "$1" \
      --out "$1/scores_trim.json" --train-records "$NW/s1_trim/anchors_train_records.json" \
      --held-records "$NW/s1_trim/anchors_held_records.json" ) > "$OUT/logs/score_$(basename "$1").log" 2>&1
}

case $MODE in
score)
  PUB=$MAINF/results/s$SEED; [ -d "$PUB" ] || die "no published result $PUB"
  R=$OUT/published/s$SEED; rm -rf "$R"; mkdir -p "$R"
  cp "$PUB/checkpoint_fitted.ckpt" "$PUB/satisfied_fitted.json" "$R/"
  score "$R" || die "scoring failed (see $OUT/logs)"
  "$PY" "$DATA/repro/compare_scores.py" "$PUB/scores_trim.json" "$R/scores_trim.json" | tee "$R/compare.txt";;
fit)
  [ -n "$LAS" ] || die "fit needs --lasagna"; LAS=$(readlink -f "$LAS")
  MAN=$(ls "$LAS"/*.lasagna.json 2>/dev/null | head -1); [ -n "$MAN" ] || die "no *.lasagna.json in $LAS"
  INP=$OUT/spiral-inputs.json
  "$PY" - "$MAINF/inputs/spiral-inputs.json" "$INP" "$DATA/umbilicus/umbilicus.json" "$MAN" <<'PYEOF'
import json, sys
d = json.load(open(sys.argv[1])); d['umbilicus']['path'] = sys.argv[3]; d['lasagna']['manifest'] = sys.argv[4]
json.dump(d, open(sys.argv[2], 'w'), indent=2)
PYEOF
  STORE=$OUT/m6seam22_run.vcobj
  if [ ! -f "$STORE" ]; then  # the first start writes links into the store, so work on a copy
    ( cd "$MAINF" && grep 'm6seam22.vcobj$' SHA256SUMS | sha256sum -c --quiet ) || die "m6seam22.vcobj differs from SHA256SUMS"
    cp "$MAINF/m6seam22.vcobj" "$STORE"; chmod u+w "$STORE"
  fi
  rundir() { ls -d "$ND"/out/*_"$1" 2>/dev/null | tail -1; }
  steps() { "$PY" -c "import json,sys; print(json.load(open(sys.argv[1]))['optimizer_num_training_steps'])" "$1"; }
  fit() {  # $1 tag, $2 overrides file, [$3 parent tag]
    local T=$1 O=$2 PT=${3:-} CK=""
    if [ -n "$PT" ]; then CK=$(rundir "$PT")/checkpoint_fitted.ckpt; [ -f "$CK" ] || die "no parent checkpoint for $T"; fi
    echo "$(date) === $T ${PT:+from $PT}"
    local -a E=(FIT_SPIRAL_CONFIG_OVERRIDES="$(tr -d '\n' < "$O")" FIT_SPIRAL_OUT_DIR="$ND/out" FIT_SPIRAL_RUN_TAG="$T"
                WANDB_MODE=disabled PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True)
    [ -n "$CK" ] && E+=(FIT_SPIRAL_RESUME_PATH="$CK")
    ( cd "$VILLA/spiral-fitting" && env -u FIT_SPIRAL_RESUME_PATH "${E[@]}" "$PY" fit_spiral.py --inputs "$INP" \
        --object-store "$STORE" --object-store-author "$AUTHOR" --cache "$OUT/cache" ) > "$OUT/logs/fit_$T.log" 2>&1 \
      || die "$T failed (see $OUT/logs/fit_$T.log)"
    echo "$(date) $T done; tracebacks $(grep -c Traceback "$OUT/logs/fit_$T.log" || true)"
  }
  held() {  # $1 tag: reload the finished stage with the held-back patches (0 training steps), score it, check it
    local T=$1 H=Hx_$1 N; N=$(steps "$MAINF/inputs/overrides_$T.json")
    "$PY" -c "import json,sys; c=json.load(open(sys.argv[1])); c['object_store_patch_exclude_uuids']=[]; json.dump(c,open(sys.argv[2],'w'),indent=1)" \
      "$MAINF/inputs/overrides_$T.json" "$OUT/overrides/overrides_$H.json"
    fit "$H" "$OUT/overrides/overrides_$H.json" "$T"
    local R; R=$(rundir "$H"); "$PY" "$SCR/held_ids.py" "$R"
    score "$R" || die "scoring of $H failed (see $OUT/logs)"
    echo "$(date) TSCORE $T $("$PY" "$SCR/verify_held.py" "$(rundir "$T")/checkpoint_fitted.ckpt" "$R" "$N")"
  }
  echo "$(date) run_main $P start; villa $(git -C "$VILLA" rev-parse --short HEAD); store $STORE"
  for S in $SEEDS; do
    A=Va_${P}_s${S}_10k; B=Vb_${P}_s${S}_25k
    [ -f "$MAINF/inputs/overrides_$A.json" ] || die "no settings for $A (SMK22 has seed 1 only)"
    fit "$A" "$MAINF/inputs/overrides_$A.json"; held "$A"
    fit "$B" "$MAINF/inputs/overrides_$B.json" "$A"; held "$B"
  done
  echo "$(date) run_main $P done"
  [ "$P" = MSEM6 ] && echo "published (seed 1 / seed 2): f025 0.5697 / 0.5712, f05 0.8581 / 0.8603, held_patches 0.7027 / 0.6997, held_off 0.2334 / 0.2274" || true;;
*) die "mode must be score or fit";;
esac
