#!/bin/bash
set -e

BASE="/opt/zlh/zpacks/minecraft"

# ------------------------
# Launch Ready Versions
# ------------------------
VERSIONS=(
  "1.12.2"
  "1.16.5"
  "1.18.2"
  "1.19.2"
  "1.20.1"
  "1.21.7"
)

# ------------------------
# Helper: download safely
# ------------------------
download() {
    local url="$1"
    local out="$2"

    echo "⬇️ Downloading:"
    echo "    URL: $url"
    echo "    OUT: $out"

    mkdir -p "$(dirname "$out")"

    if curl -L --fail -o "$out" "$url"; then
        echo "✅ Success: $out"
    else
        echo "❌ FAILED: $url"
    fi
}

paper_latest_build() {
    local version="$1"
    curl -s "https://api.papermc.io/v2/projects/paper/versions/${version}" \
        | jq -r '.builds[-1]'
}

purpur_latest_build() {
    local version="$1"
    curl -s "https://api.purpurmc.org/v2/purpur/${version}" \
        | jq -r '.builds.latest'
}

vanilla_url() {
    local version="$1"
    curl -s "https://launchermeta.mojang.com/mc/game/version_manifest.json" \
        | jq -r ".versions[] | select(.id==\"${version}\") | .url" \
        | xargs curl -s \
        | jq -r '.downloads.server.url'
}

# ------------------------
# Begin Downloads
# ------------------------
echo "📁 Creating base directory: $BASE"
mkdir -p "$BASE"

echo ""
echo "=========================="
echo "  STARTING DOWNLOADS"
echo "=========================="
echo ""

for ver in "${VERSIONS[@]}"; do

    echo ""
    echo "======================================"
    echo " Processing Version: $ver"
    echo "======================================"

    # -----------------------------------
    # Paper
    # -----------------------------------
    echo "📄 PAPER $ver"
    build=$(paper_latest_build "$ver")

    if [[ "$build" != "null" ]]; then
        download \
          "https://api.papermc.io/v2/projects/paper/versions/${ver}/builds/${build}/downloads/paper-${ver}-${build}.jar" \
          "${BASE}/paper/${ver}/server.jar"
    else
        echo "⚠️ Paper does NOT support version $ver"
    fi

    # -----------------------------------
    # Purpur
    # -----------------------------------
    echo "📄 PURPUR $ver"
    build=$(purpur_latest_build "$ver")

    if [[ "$build" != "null" ]]; then
        download \
          "https://api.purpurmc.org/v2/purpur/${ver}/latest/download" \
          "${BASE}/purpur/${ver}/server.jar"
    else
        echo "⚠️ Purpur does NOT support version $ver"
    fi

    # -----------------------------------
    # Vanilla (Mojang)
    # -----------------------------------
    echo "📄 VANILLA $ver"
    url=$(vanilla_url "$ver")

    if [[ "$url" != "null" ]]; then
        download "$url" "${BASE}/vanilla/${ver}/server.jar"
    else
        echo "⚠️ Vanilla does NOT support version $ver"
    fi

done

# -----------------------------------
# Fix permissions
# -----------------------------------
echo ""
echo "🔧 Setting permissions..."
chown -R www-data:www-data "$BASE"
chmod -R 755 "$BASE"

echo ""
echo "🎉 Minecraft Artifact Bootstrap COMPLETE!"
echo "Artifacts are now stored in:"
echo "   $BASE"
echo ""
