#!/usr/bin/env bash
#
# Render the Scoop manifest for rdbms-playground to stdout.
#
# Pure function of its inputs — NO network, NO jq/ruby — so it runs unchanged in
# the CI job container (node:22-bookworm-slim: bash + coreutils only). Given a
# version and the two Windows asset SHA-256 hashes it prints a complete,
# schema-valid Scoop manifest. The publish.yaml `scoop-bucket` job fetches the
# hashes from the release's .sha256 sidecars and commits the result into the
# lazyeval/scoop-bucket repository as rdbms-playground.json.
#
# Manifest updates are CI-driven (this script, per release), so the manifest
# carries `checkver` (so `scoop status` / the community excavator can see when
# the bucket lags upstream) but deliberately NO `autoupdate` — our pipeline is
# the updater, not Scoop's maintainer tooling.
#
# Usage: render-scoop-manifest.sh <version> <hash-x64> <hash-arm64>
#   <version>     version, with or without a leading 'v' (e.g. 0.2.0 or v0.2.0)
#   <hash-x64>    sha256 of the x86_64-pc-windows-gnu.exe asset
#   <hash-arm64>  sha256 of the aarch64-pc-windows-gnullvm.exe asset
set -euo pipefail

if [ "$#" -ne 3 ]; then
  echo "usage: $0 <version> <hash-x64> <hash-arm64>" >&2
  exit 2
fi

# Accept either 0.2.0 or v0.2.0; the manifest 'version' field is bare.
version=${1#v}
hash_x64=$2
hash_arm64=$3

repo="https://git.lazyeval.net/oli/rdbms-playground"
base="$repo/releases/download/v$version"

# The `#/rdbms-playground.exe` fragment tells Scoop to save the versioned asset
# under a stable filename, so the `bin` shim resolves regardless of version.
url_x64="$base/rdbms-playground-v$version-x86_64-pc-windows-gnu.exe#/rdbms-playground.exe"
url_arm64="$base/rdbms-playground-v$version-aarch64-pc-windows-gnullvm.exe#/rdbms-playground.exe"

# Note: \$.tag_name emits a literal $ (Scoop's JSONPath); the regex uses [0-9.]
# rather than \d so the manifest contains no backslashes to escape.
cat <<EOF
{
    "version": "$version",
    "description": "A cross-platform TUI playground for learning relational databases.",
    "homepage": "https://relplay.org",
    "license": "MIT OR Apache-2.0",
    "architecture": {
        "64bit": {
            "url": "$url_x64",
            "hash": "$hash_x64"
        },
        "arm64": {
            "url": "$url_arm64",
            "hash": "$hash_arm64"
        }
    },
    "bin": "rdbms-playground.exe",
    "checkver": {
        "url": "https://git.lazyeval.net/api/v1/repos/oli/rdbms-playground/releases/latest",
        "jsonpath": "\$.tag_name",
        "regex": "v([0-9.]+)"
    }
}
EOF
