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

set -x -e

scriptdir=${BASH_SOURCE%/*}
distdir=$(portageq distdir)
cd "$(git rev-parse --show-toplevel)"

while [[ ${#} -gt 0 ]]; do
	old=${1}
	new=${2}
	[[ ${old} && ${new} ]]

	for pkg in gentoo-kernel-bin; do
		for arch in amd64 arm64 ppc64le x86; do
			match=(
				${BINPKG:-~/binpkg}/${arch}/kernel/sys-kernel/${pkg%-bin}/${pkg%-bin}-${new}*.gpkg.tar
			)
			[[ ${#match[@]} -eq 1 ]]

			fn=${match##*/}
			cp "${match}" "${distdir}/${fn/.gpkg.tar/.${arch}.gpkg.tar}"
		done

		(
			cd sys-kernel/${pkg}
			match=( *${pkg}-${old}.ebuild )
			newk=${pkg}-${new}.ebuild
			cp "${match[-1]}" "${newk}"
			ekeyword ~all "${newk}"
			"${scriptdir}"/copybump "${newk}"
			sed -i -e "s:^\(SHA256SUM_DATE=\).*:\1$(date +%Y%m%d):" "${newk}"
			${EDITOR:-vim} "${newk}"
			GENTOO_MIRRORS= ebuild "${newk}" manifest

			git add Manifest "${newk}"
			"${scriptdir}"/pkgcommit -sS . -m "Bump to ${new}"
		)
	done

	shift 2
done
