#!/usr/bin/env bash
# Build a Q1+Q2 2024 combined fxcache for cross-quarter walk-forward CV.
#
# Requires:
#   test_data/futures-baseline/ES.FUT/ES.FUT_2024-Q1.dbn.zst (present)
#   test_data/futures-baseline/ES.FUT/ES.FUT_2024-Q2.dbn.zst (present)
#   test_data/futures-baseline-mbp10/ES.FUT/ES.FUT_2024-Q1.dbn.zst (present)
#   test_data/futures-baseline-mbp10/ES.FUT/ES.FUT_2024-Q2.dbn.zst (download in progress)
#   test_data/futures-baseline-trades/ES.FUT/ES.FUT_2024-Q1.dbn.zst (present)
#   test_data/futures-baseline-trades/ES.FUT/ES.FUT_2024-Q2.dbn.zst (present)
#
# Produces a single fxcache + norm_stats in test_data/feature-cache/.
# Cache key is SHA256 over all matching DBN files in the input dirs.

set -euo pipefail

# Stage Q1+Q2 only — symlink them into a temporary dir so the builder
# doesn't pick up other quarters that may land later.
STAGE="${STAGE:-/tmp/fxbuild_2q_2024}"
rm -rf "$STAGE"
mkdir -p "$STAGE"/{ohlcv,mbp10,trades}/ES.FUT
for q in Q1 Q2; do
  ln -s "/home/jgrusewski/Work/foxhunt/test_data/futures-baseline/ES.FUT/ES.FUT_2024-${q}.dbn.zst"        "$STAGE/ohlcv/ES.FUT/"
  ln -s "/home/jgrusewski/Work/foxhunt/test_data/futures-baseline-mbp10/ES.FUT/ES.FUT_2024-${q}.dbn.zst" "$STAGE/mbp10/ES.FUT/"
  ln -s "/home/jgrusewski/Work/foxhunt/test_data/futures-baseline-trades/ES.FUT/ES.FUT_2024-${q}.dbn.zst" "$STAGE/trades/ES.FUT/"
done

mkdir -p test_data/feature-cache

SQLX_OFFLINE=true cargo run -p ml --release --example precompute_features -- \
  --data-dir "$STAGE/ohlcv" \
  --mbp10-data-dir "$STAGE/mbp10" \
  --trades-data-dir "$STAGE/trades" \
  --output-dir test_data/feature-cache \
  --symbol ES.FUT \
  --yes

echo
echo "Built fxcaches in test_data/feature-cache/:"
ls -lh test_data/feature-cache/ | grep "$(date +%Y-%m-%d)" || ls -lh test_data/feature-cache/ | tail
