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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=lib/changelog.sh
source "$ROOT/lib/changelog.sh"

ver="${1:-}"
changelog="${2:-CHANGELOG.md}"
if [ -z "$ver" ]; then
  echo "usage: changelog-section <version> [<changelog>]" >&2
  exit 2
fi
[ -f "$changelog" ] || {
  echo "changelog-section: no such file: $changelog" >&2
  exit 1
}

notes="$(changelog_section "$changelog" "$ver")"
[ -n "$notes" ] || {
  echo "changelog-section: $changelog has no section for '$ver' — the release PR stamps the Unreleased section with version + date BEFORE the tag" >&2
  exit 1
}
printf '%s\n' "$notes"
