#!/usr/bin/env bash
# remove the specified package, opening all relevant bugs in the browser
# and grepping profiles for stale references
# note: package.mask entry must be removed first!
# (c) 2019-2024 Michał Górny <mgorny@gentoo.org>
# SPDX-License-Identifier: GPL-2.0-or-later

scriptdir=${BASH_SOURCE%/*}
pkg=${1}; shift

if ! [[ ${pkg} ]]; then
	echo "Usage: ${0} cat/pkg [bugno]"
	exit 1
fi

set -e -x
cd "$(git rev-parse --show-toplevel)"
git rm -r "${pkg}"
urls=(
	"${@/#/https:\/\/bugs.gentoo.org\/show_bug.cgi?id=}"
	"https://bugs.gentoo.org/buglist.cgi?no_redirect=1&quicksearch=$("${scriptdir}"/urlencode <<<"${pkg}")"
)
if type -P exo-open &>/dev/null; then
	exo-open "${urls[@]}"
else
	for url in "${urls[@]}"; do
		xdg-open "${url}"
	done
fi
git --no-pager grep "${pkg}" profiles/ || :

msg="${pkg}: Remove last-rited pkg
"
if [[ -n ${@} ]]; then
	for bug; do
		msg+="
Bug: https://bugs.gentoo.org/${bug}"
	done
fi

exec git commit -a -s -S -m "${msg}"
