#! /bin/bash
#
# FIXME: we probably want to generate some logs when releasing, do
# some sanity checking, do some stats etc...
#
# We probably want to sign the tag too.
#
USAGE="new [--help]"


if test $# -ne 0; then
	usage
fi

#
# We can use any kernel repositories or use the one tracked by kdist.
#
kdist__cd_kernel_topdir || exit

#
# The working tree must be clean
#
git__check_clean_work_tree || {
	warn "You have local changes in your kernel working tree."
	warn "You must stash/commit them before releasing."
	exit
}

#
# Test if the current branch has already been released. If not, then
# that doesn't means that no release happened. Indeed the current
# branch can have already been released but also been rebased due to
# reverts of merge or any other obscure reasons.
#
# We're releasing new release, therefore we're ignoring the autolocal
# version.
#
kdist__setup_kernel_version

n=0
if test $KERNEL_VERSION_PACKAGE; then
	n=$KERNEL_VERSION_PACKAGE
fi
tag=$(kdist__make_tag $((n+1)))

echo -n "Making release: $tag [Y/n]? "
read yesno
case "$yesno" in
[nN]|[nN][oO])	die "Aborted."
esac

#
# Ok, now the tag is well defined, modify the topdir Makefile
#
if test $KERNEL_VERSION_PACKAGE; then
	sed -i -e "/EXTRAVERSION =/s/-$((n+0))/-$((n+1))/" Makefile
else
	sed -i -e "s/EXTRAVERSION =.*/&-$((n+1))/" Makefile
fi ||
die "failed to update the top Makefile"

#
# Finally ask git to immortalizes the event
#
if ! git commit -qas -m "Mandriva Release $tag"; then
	git checkout -- Makefile
	die "git failed to commit the release."
fi
if ! git tag -a -m "$tag release" $tag; then
	git reset -q --hard HEAD~1
	die "git failed to create the tag."
fi
