#!/bin/sh
# Restore landing zone files from R2.
# The extraction state DB (.state.sqlite) is restored automatically by
# the Litestream container on startup — this script handles the data files only.
#
# Requires: rclone, LITESTREAM_R2_* env vars (from /opt/padelnomics/.env)
#
# Usage:
#   source /opt/padelnomics/.env && bash infra/restore_landing.sh

set -eu

LANDING_DIR="${LANDING_DIR:-/data/padelnomics/landing}"
BUCKET_PREFIX="${LITESTREAM_R2_BUCKET:?LITESTREAM_R2_BUCKET not set}/padelnomics/landing"

echo "==> Restoring landing zone from R2 to ${LANDING_DIR}..."

rclone sync ":s3:${BUCKET_PREFIX}" "$LANDING_DIR" \
    --s3-provider Cloudflare \
    --s3-access-key-id "${LITESTREAM_R2_ACCESS_KEY_ID:?not set}" \
    --s3-secret-access-key "${LITESTREAM_R2_SECRET_ACCESS_KEY:?not set}" \
    --s3-endpoint "https://${LITESTREAM_R2_ENDPOINT:?not set}" \
    --s3-no-check-bucket \
    --exclude ".state.sqlite*" \
    --progress

echo "==> Landing zone restored to ${LANDING_DIR}"
