#!/usr/bin/env bash
set -euo pipefail

# Usage:
#   bash bootstrap-compose.sh
#   BASE_URL="https://git.nesterovic.cc/nessi/NexaPG/raw/branch/main" bash bootstrap-compose.sh

BASE_URL="${BASE_URL:-https://git.nesterovic.cc/nessi/NexaPG/raw/branch/main}"

echo "[bootstrap] Using base URL: ${BASE_URL}"

fetch_file() {
  local path="$1"
  local out="$2"

  if command -v wget >/dev/null 2>&1; then
    wget -q -O "${out}" "${BASE_URL}/${path}"
  elif command -v curl >/dev/null 2>&1; then
    curl -fsSL "${BASE_URL}/${path}" -o "${out}"
  else
    echo "[bootstrap] ERROR: wget or curl is required"
    exit 1
  fi
}

fetch_file "docker-compose.yml" "docker-compose.yml"
fetch_file ".env.example" ".env.example"
fetch_file "Makefile" "Makefile"

if [[ ! -f ".env" ]]; then
  cp .env.example .env
  echo "[bootstrap] Created .env from .env.example"
else
  echo "[bootstrap] .env already exists, keeping it"
fi

echo
echo "[bootstrap] Next steps:"
echo "  1) Edit .env (set JWT_SECRET_KEY and ENCRYPTION_KEY at minimum)"
echo "  2) Run: make up"
echo
