#!/usr/bin/env bash
# vaken — regenerate badges derived from Cargo.toml facts.
#
# Only the badges whose text is derived from a source-of-truth file are
# regenerated here:
#
#   - version.svg → Cargo.toml [package].version
#   - license.svg → Cargo.toml [package].license  (formatted "X / Y")
#
# Platform ("macOS") and maintenance ("passively-maintained") are
# editorial choices, not derived facts — change them by editing
# .github/badges/*.svg directly (or regenerating via generate-badge.sh).
#
# CI runs this script and `git diff --exit-code .github/badges/`. If
# the committed badges drift from the regenerated ones, CI fails with
# a hint to re-run this script and commit the result.
#
# Usage: ./scripts/update-badges.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VAKEN_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$VAKEN_DIR"

version=$(grep -m1 '^version = ' Cargo.toml | sed -E 's/version = "([^"]+)"/\1/')
# Convert SPDX "MIT OR Apache-2.0" → display "MIT / Apache-2.0" to
# match the convention used in scxml/axess license badges.
license=$(grep -m1 '^license = ' Cargo.toml | sed -E 's/license = "([^"]+)"/\1/' | sed 's| OR | / |g')

if [ -z "$version" ] || [ -z "$license" ]; then
  echo "Could not parse version/license from Cargo.toml" >&2
  exit 1
fi

./scripts/generate-badge.sh version "$version" blue .github/badges/version.svg
./scripts/generate-badge.sh license "$license" blue .github/badges/license.svg

echo "Regenerated .github/badges/version.svg ($version) and license.svg ($license)"
