#!/usr/bin/env bash
# script/changelog-update — Generate or update CHANGELOG.md from git tags.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
LIB="${SCRIPT_DIR}/_lib/changelog_update.py"

if [[ ! -f "${LIB}" ]]; then
  echo "ERROR: ${LIB} not found" >&2
  exit 1
fi

_find_python() {
  if command -v python &>/dev/null 2>&1; then
    echo "python"
    return 0
  fi
  if command -v python3 &>/dev/null 2>&1; then
    echo "python3"
    return 0
  fi
  if command -v py &>/dev/null 2>&1; then
    echo "py -3"
    return 0
  fi
  echo "ERROR: Python not found." >&2
  return 127
}

PYTHON=$(_find_python) || exit 127

exec ${PYTHON} "${LIB}" --repo-root "${REPO_ROOT}" "$@"
