#!/usr/bin/env bash
# check whether packages that should be kept in sync are up-to-date
# (c) 2023-2024 Michał Górny <mgorny@gentoo.org>
# SPDX-License-Identifier: GPL-2.0-or-later

REPO=$(git rev-parse --show-toplevel)
PKGS=(
	dev-python/ensurepip-pip:0=dev-python/pip:0
	dev-python/ensurepip-setuptools:0=dev-python/setuptools:0
	dev-python/ensurepip-wheel:0=dev-python/wheel:0
	app-doc/python-docs:2.7=dev-lang/python:2.7
	app-doc/python-docs:3.8=dev-lang/python:3.8
	app-doc/python-docs:3.9=dev-lang/python:3.9
	app-doc/python-docs:3.10=dev-lang/python:3.10
	app-doc/python-docs:3.11=dev-lang/python:3.11
	app-doc/python-docs:3.12=dev-lang/python:3.12
	app-doc/python-docs:3.13=dev-lang/python:3.13
	dev-python/python-tests:3.11=dev-lang/python:3.11
	dev-python/python-tests:3.12=dev-lang/python:3.12
	dev-python/python-tests:3.13=dev-lang/python:3.13
	dev-python/python-tests:3.13t=dev-lang/python:3.13t
	dev-python/python-tests:3.14=dev-lang/python:3.14
	dev-python/python-tests:3.14t=dev-lang/python:3.14t
	dev-python/uv=dev-python/uv-build
)

declare -A VERSIONS
while read -r PKG; do
	SLOT=${PKG#*:}
	PKG=${PKG%:*}
	PKG=${PKG%-r[0-9]*}
	NAME=${PKG%-*}
	VERSION=${PKG##*-}
	VERSION=${VERSION%_p*}
	# ignore live ebuilds, we want to check newest release
	[[ ${VERSION} == *9999* ]] && continue
	VERSIONS[${NAME}:${SLOT}]=${VERSION}
done < <(
	pquery --raw --quiet --repo "${REPO}" --slot "${PKGS[@]%=*}" "${PKGS[@]#*=}"
)

RET=0
for PKG in "${PKGS[@]}"; do
	A=${PKG%=*}
	B=${PKG#*=}
	AV="${VERSIONS[${A}]}"
	BV="${VERSIONS[${B}]}"

	if [[ ${AV} != ${BV} ]]; then
		echo "Package version mismatch found!" >&2
		echo "  ${A}: ${AV}" >&2
		echo "  ${B}: ${BV}" >&2
		RET=1
	fi
done

exit "${RET}"
