#!/usr/bin/env bash
#
# bin/version - Print the pgxntool version embedded in this project
#
# The release process (see .claude/skills/release) stamps the first line of
# HISTORY.asc with the released version number, so that line is the source
# of truth for "what version of pgxntool is this?". If this copy was synced
# from an unreleased commit rather than a tagged release, the first line
# will be "STABLE" instead of a version number -- that's printed as-is, since
# it's an accurate answer too. Anything else (a corrupt or hand-edited file)
# is rejected rather than printed as if it were a real answer.
#
# Usage: bin/version [<history-file>]
#
#   history-file  Path to HISTORY.asc to read. Defaults to the copy
#                 one level up from this script (i.e. pgxntool/HISTORY.asc
#                 in a normal checkout).

set -o errexit -o errtrace -o pipefail
trap 'echo "Error on line ${LINENO}"' ERR

PGXNTOOL_DIR="$(dirname "${BASH_SOURCE[0]}")/.."
source "$PGXNTOOL_DIR/lib.sh"

history_file="${1:-$PGXNTOOL_DIR/HISTORY.asc}"

[[ -f "$history_file" ]] || die 1 "$history_file not found"

version=$(head -n1 "$history_file")

[[ -n "$version" ]] || die 1 "$history_file is empty"

# Matches the X.Y.Z format the release process validates and stamps.
[[ "$version" == "STABLE" || "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || \
    die 1 "$history_file: first line '$version' is neither STABLE nor a X.Y.Z version"

echo "$version"
