#!/usr/bin/env bash
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Works both:
# - in-repo:   finish_ready/scripts/sync_shared_tex.sh
# - extracted: scripts/sync_shared_tex.sh
if [[ -d "$HERE/../paper" && -d "$HERE/../engine" ]]; then
  ROOT="$(cd "$HERE/.." && pwd)"
  FR="$ROOT"
elif [[ -d "$HERE/../paper" && -d "$HERE/../../engine" ]]; then
  ROOT="$(cd "$HERE/../.." && pwd)"
  FR="$ROOT/finish_ready"
else
  echo "Could not locate sources (paper/ not found)." 1>&2
  exit 2
fi

SRC="$FR/paper"

declare -a FILES=(
  COC_definition.tex
  COC_checklist.tex
  COC_universality_schema.tex
  COC_rotation_proof.tex
  COC_embedded_loop_proof.tex
  COC_multicycle_proof.tex
  WELL_POSEDNESS_SPINE.tex
  TARGET_CORRECTNESS_AXIOMS.tex
  TARGET_DECISION_FLOW.tex
  PREDICATE_EXHAUSTION_LEMMAS.tex
  VALIDATOR_PREDICATE_SPEC.tex
  WOB_CATALOG_COMPLETENESS.tex
  TARGET_IDENTIFIABILITY_THEOREM.tex
)

declare -a DESTS=(
  "$FR/sft2"
  "$FR/sft3"
  "$ROOT/global_universality"
)

for d in "${DESTS[@]}"; do
  mkdir -p "$d"
  for f in "${FILES[@]}"; do
    if [[ -f "$SRC/$f" ]]; then
      cp "$SRC/$f" "$d/$f"
    fi
  done
done

echo "OK: synced shared TeX artifacts from finish_ready/paper into sft2, sft3, and global_universality"
