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

# Works both:
# - in-repo:   finish_ready/scripts/run_all.sh
# - extracted: scripts/run_all.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/ or engine/ missing)." 1>&2
  exit 2
fi

STATUS="$FR/BUILD_STATUS.md"

write_status() {
  rm -f "$STATUS"
  {
    echo "# Build status"
    echo ""
    echo "Version: $(cat "$FR/VERSION.txt" 2>/dev/null || echo "UNKNOWN")"
    echo "Timestamp (UTC): $(date -u +"%Y-%m-%d %H:%M:%S")"
    echo ""
    echo "Validations: OK"
    echo ""
    echo "Built files:"
    for f in sft_form_theory.pdf sft2_syncre_stability_modulus.pdf sft3_target_inference_and_certificates.pdf witness_atlas.pdf global_universality.pdf; do
      if [[ -f "$FR/$f" ]]; then
        echo "- $f"
      fi
    done
    echo ""
    echo "Integrity index: OK"
    echo "Bundle: OK"
  } >> "$STATUS"
}

clean_transient_build_dirs() {
  rm -rf "$FR/scripts/build"          "$FR/scripts/build_sft2"          "$FR/scripts/build_sft3"          "$FR/scripts/build_atlas"          "$FR/scripts/build_global" || true
}

build_manifest_and_bundle() {
  python3 "$FR/hash_manifest_regen.py"
  (
    cd "$FR"
    rm -f submission_bundle.zip
    zip -qr submission_bundle.zip . -x "submission_bundle.zip" \
      -x "scripts/build*" \
      -x "scripts/build_*" \
      -x "scripts/build_atlas/*" \
      -x "scripts/build_global/*" \
      -x "scripts/build_sft2/*" \
      -x "scripts/build_sft3/*" \
      -x "*.log" -x "*.aux" -x "*.out" -x "*.toc" -x "*.synctex.gz"
  )
}

bash "$FR/scripts/sync_shared_tex.sh"
bash "$FR/scripts/validate_all.sh"

bash "$FR/scripts/compile_pdf.sh"
[[ -f "$FR/scripts/build/sft_form_theory.pdf" ]] && cp "$FR/scripts/build/sft_form_theory.pdf" "$FR/sft_form_theory.pdf" || true

bash "$FR/scripts/compile_sft2.sh"
[[ -f "$FR/scripts/build_sft2/sft2_syncre_stability_modulus.pdf" ]] && cp "$FR/scripts/build_sft2/sft2_syncre_stability_modulus.pdf" "$FR/sft2_syncre_stability_modulus.pdf" || true

bash "$FR/scripts/compile_sft3.sh"
[[ -f "$FR/scripts/build_sft3/sft3_target_inference_and_certificates.pdf" ]] && cp "$FR/scripts/build_sft3/sft3_target_inference_and_certificates.pdf" "$FR/sft3_target_inference_and_certificates.pdf" || true

bash "$FR/scripts/compile_atlas.sh"
[[ -f "$FR/scripts/build_atlas/witness_atlas.pdf" ]] && cp "$FR/scripts/build_atlas/witness_atlas.pdf" "$FR/witness_atlas.pdf" || true

bash "$FR/scripts/compile_global.sh"
[[ -f "$FR/scripts/build_global/GLOBAL_SYNCRE_UNIVERSALITY_THEOREM.pdf" ]] && cp "$FR/scripts/build_global/GLOBAL_SYNCRE_UNIVERSALITY_THEOREM.pdf" "$FR/global_universality.pdf" || true

# Remove transient build directories before hashing and bundling to keep the release stable.
clean_transient_build_dirs

build_manifest_and_bundle
python3 "$FR/scripts/verify_hash_manifest.py"

# Rebuild once more so BUILD_STATUS.md is included unchanged.
build_manifest_and_bundle
python3 "$FR/scripts/verify_hash_manifest.py"

write_status

echo "OK: run_all completed"
