#!/bin/bash
set -e
set +x

if ! test -d debian; then
	echo "This script must be run from the root of the source package (the directory containing debian/)."
	exit 1
fi

echo "1. preparing debian/changelog ..."

sed '/--/q' debian/changelog > debian/changelog.rocm
head -n1 debian/changelog.rocm > debian/changelog.rocm.head
tail -n1 debian/changelog.rocm > debian/changelog.rocm.tail
rm debian/changelog.rocm

truncate --size 0 debian/changelog

sed -e 's/gloo/gloo-rocm/' debian/changelog.rocm.head >> debian/changelog
rm debian/changelog.rocm.head

echo "" >> debian/changelog
echo "  * Rebuild for ROCm support (non-free)." >> debian/changelog
echo "    The section of this package is changed from main to contrib" >> debian/changelog
echo "    due to non-free build-dependencies." >> debian/changelog
echo "  * This changelog was automatically generated by src:gloo via" >> debian/changelog
echo "    debian/rocmbuild.sh." >> debian/changelog
echo "  * Please merge changes from the ROCm variant back into src:gloo." >> debian/changelog
echo "" >> debian/changelog

cat debian/changelog.rocm.tail >> debian/changelog
rm debian/changelog.rocm.tail

echo "2. switching debian/control to the rocm version ..."

cp -v debian/control.rocm debian/control

echo "3. Maintainer notes are shown below"

echo --------------------------------------------------------------------------
echo "You can revert the changes using 'git checkout .'"
echo --------------------------------------------------------------------------
echo "Copy the source package of src:gloo as the source package of"
echo "src:gloo-rocm if sbuild cannot find it. Please use the identical source as src:gloo."
echo --------------------------------------------------------------------------
