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

set -e -x

scriptdir=${BASH_SOURCE%/*}
from=${1}
to=${2}

[[ ${from} && ${to} ]]

if [[ -d ${from} ]]; then
	git mv "${from}" "${to}"
fi

find profiles/updates -type f -exec sed -i -e "\@^move ${from} @d" {} +
git grep -l "${from}" :!profiles/updates |
	xargs -r sed -i -e "s@${from}@${to}@g"
update_file=profiles/updates/$(date "+%qQ-%Y")
echo "move ${from} ${to}" >> "${update_file}"
git add -N "${update_file}"

modified=( $(git diff --name-only HEAD) )
"${scriptdir}"/copybump "${modified[@]}"
if type -P gpy-impl &>/dev/null; then
	git grep -l "^PYTHON_COMPAT" "${modified[@]}" |
		xargs -r gpy-impl -@dead || :
fi
