#!/usr/bin/env bash
# run pkgcheck on one or more packages, and open bug templates for each
# triggering pkgcheck report
# (c) 2019-2024 Michał Górny <mgorny@gentoo.org>
# SPDX-License-Identifier: GPL-2.0-or-later

scriptdir=${BASH_SOURCE%/*}
urls=()

for pkg; do
	res=$(pkgcheck scan ${PKGCHECK_ARGS} "${@}" 2>&1 | tee /dev/fd/2)
	[[ -z ${res} ]] && continue
	enc=$("${scriptdir}"/urlencode <<<"${res}")
	pkgenc=$("${scriptdir}"/urlencode <<<"${pkg}")
	ciurl="https://qa-reports.gentoo.org/output/gentoo-ci/output.html%23${pkgenc}"
	urls+=(
		"https://bugs.gentoo.org/enter_bug.cgi?product=Gentoo%20Linux&component=Current+packages&bug_file_loc=${ciurl}&short_desc=${pkgenc}:+QA+issues&comment=${enc::2048}"
	)
done

if type -P exo-open &>/dev/null; then
	exo-open "${urls[@]}"
else
	for url in "${urls[@]}"; do
		xdg-open "${url}"
	done
fi
