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

PROJECT_ID=925200
FILE_ID=8091114
SERVER_FILE_ID=8094893
NEOFORGE_VERSION=21.1.228

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
CONTEXT_DIR="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
OUTPUT="${CONTEXT_DIR}/modpacks/atm10-7.0.md"
TMP_DIR="$(mktemp -d)"

cleanup() {
  rm -rf "${TMP_DIR}"
}
trap cleanup EXIT

require_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    printf 'missing required command: %s\n' "$1" >&2
    exit 1
  fi
}

require_cmd curl
require_cmd jq
require_cmd unzip

ARCHIVE="${TMP_DIR}/atm10-${FILE_ID}.zip"
MANIFEST="${TMP_DIR}/manifest.json"
PROJECTS="${TMP_DIR}/projects.json"

download_url=''
if [ -n "${CURSEFORGE_API_KEY:-}" ]; then
  download_url="$(curl -fsS \
    -H "x-api-key: ${CURSEFORGE_API_KEY}" \
    "https://api.curseforge.com/v1/mods/${PROJECT_ID}/files/${FILE_ID}/download-url" \
    | jq -r '.data // empty')"
fi

if [ -n "${download_url}" ]; then
  curl -fL --retry 3 -o "${ARCHIVE}" "${download_url}"
else
  curl -fL --retry 3 -o "${ARCHIVE}" \
    "https://www.curseforge.com/api/v1/mods/${PROJECT_ID}/files/${FILE_ID}/download"
fi

unzip -p "${ARCHIVE}" manifest.json > "${MANIFEST}"

if [ -n "${CURSEFORGE_API_KEY:-}" ]; then
  mod_ids="$(jq -c '[.files[].projectID] | unique' "${MANIFEST}")"
  curl -fsS \
    -H "x-api-key: ${CURSEFORGE_API_KEY}" \
    -H 'content-type: application/json' \
    -d "{\"modIds\":${mod_ids}}" \
    'https://api.curseforge.com/v1/mods' > "${PROJECTS}"
else
  printf '{"data":[]}\n' > "${PROJECTS}"
fi

{
  printf '# ATM10 7.0 Modpack\n\n'
  printf 'Generated from CurseForge client file `%s`.\n\n' "${FILE_ID}"
  printf 'Last generated: `%s`.\n\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
  printf '## Pack Metadata\n\n'
  printf -- '- Minecraft: `%s`.\n' "$(jq -r '.minecraft.version // "1.21.1"' "${MANIFEST}")"
  printf -- '- NeoForge: `%s`.\n' "${NEOFORGE_VERSION}"
  printf -- '- CurseForge project id: `%s`.\n' "${PROJECT_ID}"
  printf -- '- Client pack file id: `%s`.\n' "${FILE_ID}"
  printf -- '- Server pack file id: `%s`.\n' "${SERVER_FILE_ID}"
  printf -- '- Source: [AllTheMods/ATM-10](https://github.com/AllTheMods/ATM-10).\n\n'
  printf '## Manifest\n\n'
  jq -r '"- Name: `" + (.name // "unknown") + "`.", "- Version: `" + (.version // "unknown") + "`."' "${MANIFEST}"
  printf '\n## Mods\n\n'
  printf '| Project id | File id | Name | Reference |\n'
  printf '| --- | --- | --- | --- |\n'
  jq -r --slurpfile projects "${PROJECTS}" '
    def project($id): (($projects[0].data // [])[]? | select(.id == $id)) // {};
    def cell: tostring | gsub("\\|"; "\\\\|");
    .files[]
    | project(.projectID) as $project
    | [
        (.projectID | tostring),
        (.fileID | tostring),
        (($project.name // "unresolved") | cell),
        (($project.links.websiteUrl // ("https://api.curseforge.com/v1/mods/" + (.projectID | tostring))) | cell)
      ]
    | "| " + join(" | ") + " |"
  ' "${MANIFEST}"
} > "${OUTPUT}"

printf 'wrote %s\n' "${OUTPUT}"
