#!/usr/bin/env bash
# Generate a real Bitcoin Core regtest datadir fixture (101 blocks) for BLVM Core drop-in tests.
#
# Usage:
#   ./scripts/gen-core-regtest-fixture.sh [OUTPUT_DIR]
#
# Default output: tests/fixtures/core-regtest-101/
# Requires: bitcoind and bitcoin-cli on PATH (or BITCOIND / BITCOIN_CLI env).

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUT="${1:-$ROOT/tests/fixtures/core-regtest-101}"
BITCOIND="${BITCOIND:-bitcoind}"
BITCOIN_CLI="${BITCOIN_CLI:-bitcoin-cli}"
WORKDIR="$(mktemp -d)"
DATADIR="$WORKDIR/regtest-data"
META="$OUT/fixture.json"

cleanup() {
  "$BITCOIN_CLI" -regtest -datadir="$DATADIR" stop >/dev/null 2>&1 || true
  sleep 1
  rm -rf "$WORKDIR"
}
trap cleanup EXIT

command -v "$BITCOIND" >/dev/null || { echo "bitcoind not found"; exit 1; }
command -v "$BITCOIN_CLI" >/dev/null || { echo "bitcoin-cli not