#!/bin/bash
# =========================================================
# wait-for-core.sh — Wait for Core infrastructure
# =========================================================
# Used by lehrer/compliance docker-compose as init container

MAX_RETRIES=${MAX_RETRIES:-60}
RETRY_INTERVAL=${RETRY_INTERVAL:-5}
HEALTH_URL=${HEALTH_URL:-http://bp-core-health:8099/health}

echo "Waiting for Core infrastructure at $HEALTH_URL ..."

for i in $(seq 1 $MAX_RETRIES); do
    if curl -sf --max-time 3 "$HEALTH_URL" > /dev/null 2>&1; then
        echo "Core infrastructure is ready! (attempt $i)"
        exit 0
    fi
    echo "  Attempt $i/$MAX_RETRIES — Core not ready yet, retrying in ${RETRY_INTERVAL}s..."
    sleep $RETRY_INTERVAL
done

echo "ERROR: Core infrastructure did not become ready after $((MAX_RETRIES * RETRY_INTERVAL))s"
exit 1
