#!/usr/bin/env bash
# XDC mainnet bootstrap bundle — generated by XDC SkyNet (#735)
# Downloads the official mainnet snapshot and extracts it into the node
# data directory. Override the target with XDC_DATADIR=/path/to/datadir.
set -euo pipefail

NETWORK="mainnet"
SNAPSHOT_URL="https://download.xinfin.network/xdcchain.tar"
DATADIR="${XDC_DATADIR:-$HOME/.XDC/$NETWORK}"

command -v curl >/dev/null 2>&1 || { echo "curl is required" >&2; exit 1; }
command -v tar  >/dev/null 2>&1 || { echo "tar is required" >&2; exit 1; }

echo "==> Stop the XDC node first (docker compose stop / systemctl stop xdc)"
mkdir -p "$DATADIR"
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT

echo "==> Downloading $SNAPSHOT_URL"
curl -fL --retry 3 -o "$WORKDIR/snapshot.tar" "$SNAPSHOT_URL"

echo "==> Extracting into $DATADIR"
tar -xf "$WORKDIR/snapshot.tar" -C "$DATADIR"

echo "==> Done. Start the node; it will sync from the snapshot block onward."
