#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-only
# Enforce that every distribution surface versions in LOCKSTEP with the engine.
#
# The root Cargo.toml [package] version is the single source of truth. Every surface
# that carries its own version string must equal it, so a release can never ship one
# surface (crate / wheel / npm / JetBrains plugin / MCP image) at a different version
# than the others. Surfaces that derive their version automatically are noted below and
# need no check:
#   - PyPI  : pyproject.toml `dynamic = ["version"]` -> maturin reads Cargo.toml
#   - crates: cargo publish reads Cargo.toml
#   - npm   : web/pkg/package.json is generated by wasm-pack from Cargo.toml at build
#             (gitignored — never committed, so it cannot drift)
#   - release.yml / mcp-publish.yml / jetbrains-plugin.yml: derive from the git tag
#   - CITATION.cff / codemeta.json / .zenodo.json: pinned to the manifest by
#     tests/citation_metadata_doc_sync.rs
set -euo pipefail

ver="$(grep -m1 '^version' Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/')"
if [ -z "$ver" ]; then
  echo "FAIL: could not read [package] version from Cargo.toml" >&2
  exit 1
fi
majmin="$(printf '%s' "$ver" | cut -d. -f1,2)"

fail=0
note() { printf '  %s\n' "$1" >&2; }

# 1. README status badge (the documented version users see first).
if ! grep -q "Status: v${ver}" README.md; then
  echo "FAIL: README status badge does not match Cargo.toml version v${ver}." >&2
  grep -n '\*\*Status:' README.md >&2 || note "(no status badge found)"
  fail=1
fi

# 2. MCP companion crate — its own version, in lockstep.
mcp_ver="$(grep -m1 '^version' mcp/kshana-mcp/Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/')"
if [ "$mcp_ver" != "$ver" ]; then
  echo "FAIL: mcp/kshana-mcp/Cargo.toml version ($mcp_ver) != engine ($ver)." >&2
  fail=1
fi

# 3. MCP crate's dependency on the published kshana crate — its major.minor requirement
#    must track the engine, or `cargo publish` of kshana-mcp cannot resolve the crate.
mcp_dep="$(grep -E '^kshana = ' mcp/kshana-mcp/Cargo.toml | sed -E 's/.*version = "([^"]+)".*/\1/')"
if [ "$mcp_dep" != "$majmin" ] && [ "$mcp_dep" != "$ver" ]; then
  echo "FAIL: mcp/kshana-mcp/Cargo.toml kshana dependency ($mcp_dep) does not track engine $majmin." >&2
  fail=1
fi

# 4. JetBrains plugin — pluginVersion, in lockstep (the publish workflow prefers the tag,
#    but the on-disk default must still match so local builds and non-tag dispatch agree).
jb_ver="$(grep -E '^pluginVersion' ide/jetbrains/gradle.properties | sed -E 's/.*= *//' | tr -d ' ')"
if [ "$jb_ver" != "$ver" ]; then
  echo "FAIL: ide/jetbrains/gradle.properties pluginVersion ($jb_ver) != engine ($ver)." >&2
  fail=1
fi

# 5. Web landing page (kshana.dev, web/index.html) — hand-authored HTML, not derived
#    from Cargo.toml, so its version strings drift silently every release unless checked
#    here. Enforce the JSON-LD softwareVersion (what crawlers read), the visible version
#    chip fallback (what a no-JS visitor sees), and the asset cache-busters in lockstep.
web="web/index.html"
for needle in \
  "\"softwareVersion\": \"${ver}\"" \
  ">v${ver}<" \
  "favicon.svg?v=${ver}" \
  "style.css?v=${ver}" \
  "app.js?v=${ver}"; do
  if ! grep -qF -- "$needle" "$web"; then
    echo "FAIL: $web is missing the v${ver} string: ${needle}" >&2
    fail=1
  fi
done

if [ "$fail" -ne 0 ]; then
  echo "Version drift detected — bring every surface to v${ver}." >&2
  exit 1
fi

echo "OK: all distribution surfaces in lockstep at v${ver} (README, MCP crate+dep, JetBrains, web landing page; npm/PyPI/crates derive automatically)."
