#!/usr/bin/env bash
# =============================================================================
#  XDC MAINNET FAST NODE  —  one-command installer
# -----------------------------------------------------------------------------
#  Works everywhere (Linux + macOS, amd64 + arm64). Installs everything — the
#  matching geth binary, a recent chain snapshot, and starts your node. You only
#  need `curl`.
#
#      curl -fsSL https://xdc.network/install.sh | bash
#
#  Options (pass after `bash -s --`):
#      curl -fsSL https://xdc.network/install.sh | bash -s -- setup   # prepare only, don't start
#      XDC_DIR=/data/xdc curl -fsSL https://xdc.network/install.sh | bash   # custom install dir
#
#  Re-running is safe: downloads resume, an existing node is left alone.
# =============================================================================
set -euo pipefail

BASE_URL="${BASE_URL:-https://xdc.network/snapshots}"   # where one.sh + binaries + snapshot live
XDC_DIR="${XDC_DIR:-$HOME/xdc-node}"                    # install location
ACTION="${1:-${XDC_ACTION:-up}}"                        # up = setup+start (default) | setup = prepare only

if [ -t 1 ]; then B=$'\033[1m'; G=$'\033[32m'; Y=$'\033[33m'; N=$'\033[0m'; else B=""; G=""; Y=""; N=""; fi
say(){ printf '\n%s▶ %s%s\n' "$B" "$*" "$N"; }
ok(){  printf '   %s✓%s %s\n' "$G" "$N" "$*"; }
note(){ printf '   %s•%s %s\n' "$Y" "$N" "$*"; }

command -v curl >/dev/null 2>&1 || { echo "curl is required — please install curl and re-run."; exit 1; }

# zstd is needed to unpack the snapshot. Try to install it best-effort; one.sh
# also bundles a fallback. Never hard-fail here (so `| bash` stays smooth).
if ! command -v zstd >/dev/null 2>&1; then
  note "zstd not found — trying to install it…"
  if   command -v apt-get >/dev/null 2>&1; then (sudo apt-get update -y && sudo apt-get install -y zstd) >/dev/null 2>&1 || true
  elif command -v dnf     >/dev/null 2>&1; then sudo dnf install -y zstd >/dev/null 2>&1 || true
  elif command -v yum     >/dev/null 2>&1; then sudo yum install -y zstd >/dev/null 2>&1 || true
  elif command -v apk     >/dev/null 2>&1; then sudo apk add zstd        >/dev/null 2>&1 || true
  elif command -v brew    >/dev/null 2>&1; then brew install zstd        >/dev/null 2>&1 || true
  fi
  command -v zstd >/dev/null 2>&1 && ok "zstd installed" || note "could not auto-install zstd — if setup complains, run: apt install zstd / brew install zstd"
fi

say "Installing XDC mainnet fast node  →  $XDC_DIR"
mkdir -p "$XDC_DIR"; cd "$XDC_DIR"

say "Fetching one.sh (the node manager)"
curl -fsSL -A 'Mozilla/5.0' "$BASE_URL/one.sh" -o one.sh
chmod +x one.sh
ok "one.sh ready in $XDC_DIR"

case "$ACTION" in
  setup|prepare) bash one.sh setup ;;
  *)             bash one.sh setup && bash one.sh start && bash one.sh status ;;
esac

cat <<EOF

${B}✓ Done.${N} Your node lives in: ${B}$XDC_DIR${N}
   status : cd $XDC_DIR && ./one.sh status
   stop   : cd $XDC_DIR && ./one.sh stop
   start  : cd $XDC_DIR && ./one.sh start
   logs   : cd $XDC_DIR && ./one.sh logs
EOF
