#!/usr/bin/env bash
# Generates data/vanity.yaml — the source of truth for go-micro.dev Go module
# vanity redirect pages. Each page is built at site-build time by
# layouts/_content.gotmpl (Hugo Content Adapter) from this file.
#
# For every major version of https://github.com/micro/go-micro we pick
# the latest patch tag, shallow-clone it, and enumerate packages. The module
# path (go-import prefix) is read from the tag's go.mod. Only modules whose
# path starts with "go-micro.dev/" are emitted — legacy github.com/* modules
# are skipped since they don't need vanity redirects.
#
# Usage: bash scripts/generate-vanity-data.sh [output-path]
set -euo pipefail

REPO="https://github.com/micro/go-micro"
OUT="${1:-internal/website/data/vanity.yaml}"
mkdir -p "$(dirname "$OUT")"

command -v go >/dev/null 2>&1 || { echo "go not found in PATH" >&2; exit 1; }
command -v git >/dev/null 2>&1 || { echo "git not found in PATH" >&2; exit 1; }

tmp="$(mktemp -d)"
cleanup() { rm -rf "$tmp"; }
trap cleanup EXIT

mirror="$tmp/mirror"
echo ":: cloning $REPO (mirror)"
git clone --mirror "$REPO" "$mirror" >/dev/null 2>&1

echo ":: discovering latest tag per major version"
mapfile -t TAGS < <(git -C "$mirror" tag --list 'v[0-9]*.[0-9]*.[0-9]*' \
  | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' \
  | sort -V \
  | awk -F. '{ latest[$1]=$0 } END { for (m in latest) print latest[m] }')

[ "${#TAGS[@]}" -gt 0 ] || { echo "no semver tags found" >&2; exit 1; }
echo ":: tags: ${TAGS[*]}"

{
  echo "# Auto-generated by scripts/generate-vanity-data.sh — do not edit manually."
  echo "# Regenerate (and commit) when upstream micro/go-micro releases new tags."
  echo "repository: $REPO"
  echo "modules:"
} > "$OUT"

for tag in "${TAGS[@]}"; do
  major="${tag%%.*}"             # v6.8.0 -> v6
  clone="$tmp/$major"
  echo ":: $tag ($major)"
  rm -rf "$clone"; mkdir -p "$clone"
  git -C "$mirror" archive "$tag" | tar -x -C "$clone"

  # Module path = go.mod's module directive; falls back to the canonical path.
  # ponytail: read straight from go.mod — no toolchain, immune to dead deps
  if [ -f "$clone/go.mod" ]; then
    module="$(awk '/^module /{print $2; exit}' "$clone/go.mod")"
  else
    module="github.com/micro/go-micro"
  fi

  # Skip modules that don't belong to the go-micro.dev vanity domain.
  if [[ "$module" != go-micro.dev/* ]]; then
    echo "   -> $module (skipped, not go-micro.dev)"
    continue
  fi

  # Enumerate packages by filesystem: any dir holding a buildable .go file.
  # Equivalent to `go list ./...` for vanity purposes, but fully offline and
  # unaffected by unresolvable/deleted dependencies in old tags.
  # (Go ignores dirs beginning with . or _, so we skip them too.)
  mapfile -t PKGS < <(
    cd "$clone"
    find . -type f -name '*.go' \
      ! -name '*_test.go' \
      ! -path '*/testdata/*' \
      -print \
    | sed 's#/[^/]*$##' \
    | sed 's#^\./##' \
    | grep -vE '(^|/)[._][^/]*' \
    | sort -u \
    | while IFS= read -r rel; do
        [ -z "$rel" ] && echo "$module" || echo "$module/$rel"
      done \
    | sort -u
  )

  {
    echo "  - module: $module"
    echo "    major: $major"
    echo "    tag: $tag"
    echo "    packages:"
    for p in "${PKGS[@]:-}"; do
      [ -n "$p" ] && echo "      - $p"
    done
  } >> "$OUT"

  echo "   -> $module (${#PKGS[@]} packages)"
done

echo ":: wrote $OUT"
