#!/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
}

if ! diagnosis="$(changelog_section_problem "$changelog" "$ver")"; then
  echo "changelog-section: $changelog has no section for '$ver' — the release PR stamps the Unreleased section with version + date BEFORE the tag" >&2
  printf 'changelog-section: %s\n' "$diagnosis" >&2
  exit 1
fi
notes="$(changelog_section "$changelog" "$ver")"
printf '%s\n' "$notes"
