#!/usr/bin/env bash
# (c) 2026 Michał Górny <mgorny@gentoo.org>
# SPDX-License-Identifier: GPL-2.0-or-later

set -e

scriptdir=${BASH_SOURCE%/*}
system_snippet_dir=${scriptdir}/../lib/get-latest-upstream-version.d

declare -A VERSIONS_FOUND=()

# Run them in order until one of them returns something.
for snippet in "${system_snippet_dir}"/*; do
	source "${snippet}"
	if [[ ${#VERSIONS_FOUND[@]} -gt 0 ]]; then
		break
	fi
done

if [[ ${#VERSIONS_FOUND[@]} -gt 1 ]]; then
	echo "Multiple versions found: ${!VERSIONS_FOUND[@]}" >&2
	exit 1
elif [[ ${#VERSIONS_FOUND[@]} -eq 0 ]]; then
	echo "No version found" >&2
	exit 1
fi

echo "${!VERSIONS_FOUND[@]}"
