#!/bin/bash
# Release a packaged crate to Debian.
#
# Usage: [REALVER=<EXACTVER>] ./release.sh <CRATE> [<SEMVER>]
#
# Envvars:
# See also ./vars.sh.frag for its envvars, which we pass through.
# See also ./build.sh for its envvars, which we pass through.
# RERELEASE=1
#     Bump the changelog for a source-only reupload, required for migration to
#     Debian Testing, and automatically dput the tarball. You need this after a
#     NEW upload. This is a dumb consequence of two independently-thought-out
#     policies but nobody on either team has expressed interest in fixing it,
#     claiming "not my department".
# NOUPDATE=1
#     Tell debcargo not to attempt to update to the latest version, i.e.
#     autodetect REALVER. Set this if you get unexpected diffs when releasing.
#     We probably want to switch this on by default, please complain in our IRC
#     channel if you agree.
# REUSE_EXISTING_ORIG_TARBALL=1
#     Re-use the existing .orig tarball. This is needed if it was previously
#     generated with an old version of debcargo, otherwise you'll get
#     auto-REJECT from Debian FTP. TODO: we probably want to set this
#     automatically on if the Debian version ends with -2 or above.

. ./vars.sh.frag

RED=`echo -e "\033[1;31m"`
NC=`echo -e "\033[0m"`

if test ! -d $PKGDIR_REL; then
    abort 1 "Cannot find $PKGDIR_REL. Did you run ./new-package.sh before?"
fi

if test ! -f "$PKGDIR_REL/debian/changelog"; then
	abort 1 "Cannot find $PKGDIR_REL/debian/changelog. Did you run ./new-package.sh before?"
fi

if git grep --quiet FIXME -- "$PKGDIR_REL" :^"$PKGDIR_REL/debian/*.debcargo.hint" :^"$PKGDIR_REL/debian/changelog"; then
	abort 1 "FIXMEs remain in $PKGDIR_REL, fix and commit those first."
fi

git diff --quiet --cached || \
abort 1 "You have other pending changes to git, please complete it or stash it away and re-run this script."

git diff --quiet -- "$PKGDIR_REL" || \
abort 1 "Please git-add your changes to $PKGDIR_REL before running"

type dch >/dev/null || \
abort 1 "Install devscripts, we need to run dch."

RELBRANCH="pending-$PKGNAME"
git fetch origin --prune

git merge-base --is-ancestor origin/master HEAD || \
abort 1 "You are not synced with origin/master, please do so before running this script."

if [ "$RERELEASE" = 1 -o "$NOUPDATE" = 1 ]; then
	REALVER="$(get_existing_version "$PKGDIR")"
fi

if head -n1 "$PKGDIR/debian/changelog" | grep -qv UNRELEASED-FIXME-AUTOGENERATED-DEBCARGO; then
	if [ "$RERELEASE" = 1 ]; then
		run_debcargo
		( cd "$PKGDIR" && dch -a "No-op source-only re-upload for Debian Testing Migration." )
		export REUSE_EXISTING_ORIG_TARBALL=1
	else
		abort 0 \
		  "Package already released. If you want to do a source-only re-upload e.g. to" \
		  "hoop-jump through the Debian Testing migration requirements, set RERELEASE=1."
	fi
fi

if [ -e "$PKGDIR/debian/BLOCK" ]; then
	abort 1 "TODO items remain in $PKGDIR/debian/BLOCK, please deal with those"
fi

PREVBRANCH="$(git rev-parse --abbrev-ref HEAD)"
case "$PREVBRANCH" in
pending-$PKGNAME)	true;;
pending-*)	abort 1 "You are on a pending-release branch for a package other than $PKGNAME, $0 can only be run on another branch, like master";;
*)	if git rev-parse -q --verify "refs/heads/$RELBRANCH" >/dev/null || \
	   git rev-parse -q --verify "refs/remotes/origin/$RELBRANCH" >/dev/null; then
		git checkout "$RELBRANCH"
	else
		git checkout -b "$RELBRANCH"
	fi;;
esac

if head -n1 "$PKGDIR/debian/changelog" | grep -qv UNRELEASED-FIXME-AUTOGENERATED-DEBCARGO; then
	git checkout "$PREVBRANCH"
	abort 0 "Package already released on branch $RELBRANCH. If that was a mistake then run:" \
	  "    git branch -D $RELBRANCH" \
	  "And re-run this script ($0 $*). You might have to delete the remote branch too:" \
	  "    git push --delete origin $RELBRANCH"
fi

( cd "$PKGDIR"
sed -i -e s/UNRELEASED-FIXME-AUTOGENERATED-DEBCARGO/UNRELEASED/ debian/changelog
if test -z "$DISTRO"; then
    # To upload to other distro like experimental
    DISTRO=unstable
fi
dch -m -r -D $DISTRO ""
git add debian/changelog
git rm --ignore-unmatch debian/RFS
)

revert_git_changes() {
	git reset --merge
	git checkout -- "$PKGDIR/debian/changelog"
	git checkout -q -- "$PKGDIR/debian/RFS" || true
	git checkout "$PREVBRANCH"
	git branch -d "$RELBRANCH"
}

if ! run_debcargo --changelog-ready; then
	revert_git_changes
	abort 1 "Release attempt failed to run debcargo, probably the package needs updating (./update.sh $*)"
fi

# sometimes the copyright years need to be updated, try to do this automatically
if git diff -- "$PKGDIR_REL/debian/copyright.debcargo.hint" | patch -r - --no-backup-if-mismatch "$PKGDIR_REL/debian/copyright"; then
	git add "$PKGDIR_REL/debian/copyright.debcargo.hint" "$PKGDIR_REL/debian/copyright"
else
	git diff -- "$PKGDIR_REL/debian/copyright.debcargo.hint"
	revert_git_changes
	abort 1 \
	  "copyright file needs updating; apply the above diff to $PKGDIR_REL/debian/copyright" \
	  "then commit your changes, and run me again."
fi

if ! git diff --exit-code -- "$PKGDIR_REL"; then
	revert_git_changes
	abort 1 \
	  "Release attempt resulted in git diffs to $PKGDIR_REL, probably you need to update the package (./update.sh $*)." \
	  "Alternatively, set NOUPDATE=1 to override this requirement, but please have a good reason."
fi

if ! ( cd build && SOURCEONLY=1 ./build.sh "$CRATE" $VER ); then
	revert_git_changes
	abort 1 "Release attempt failed (see messages above), possible reasons are: " \
		"- build-dependencies not in Debian => release those first." \
		"- packaged version is out-of-date => run \`./update.sh $*\`"
fi

git commit -m "Release package $PKGNAME"

DEBVER=$(dpkg-parsechangelog -l $BUILDDIR/debian/changelog -SVersion)
DEBSRC=$(dpkg-parsechangelog -l $BUILDDIR/debian/changelog -SSource)
DEB_HOST_ARCH=$(dpkg-architecture -q DEB_HOST_ARCH)

if [ "$RERELEASE" = 1 ]; then

( cd build && dput "${DEBSRC}_${DEBVER}_source.changes" )
git push origin "$RELBRANCH"
git checkout master

cat <<eof
Source-only re-release of $CRATE uploaded. You need to perform the following steps:

eof

else

unstable_bin_packages="$(rmadison --noconf --suite unstable --source-and-binary "${DEBSRC}" | grep -v 'source$' | cut -d ' ' -f 1 | sort -u)"
upload_bin_packages="$(grep '^Binary' "build/${DEBSRC}_${DEBVER}.dsc" | sed -e 's/^Binary: //' -e 's/, /,/g' | tr ',' '\n' | sort -u)"
diff_bin_packages="$(diff -u0 <(echo "$unstable_bin_packages") <(echo "$upload_bin_packages") | tail -n-2)"
new_bin_packages="$(echo "$diff_bin_packages" | grep '^+' | sed -e 's/^+//g')"
rm_bin_packages="$(echo "$diff_bin_packages" | grep '^-' | sed -e 's/^-//g')"

show_build_notice() {
cat <<eof
The recommended way to build and upload is to run something like:

  cd build && ./build.sh $CRATE $VER && dput ${DEBSRC}_${DEBVER}_${DEB_HOST_ARCH}.changes
eof
}

cat <<eof
Release of $CRATE ready as a source package in ${BUILDDIR#$PWD/}. You need to
perform the following steps:

Build the package if necessary, and upload
==========================================
eof

if [ -z "$unstable_bin_packages" ]; then
cat <<eof
${RED}
Since this is a NEW source package not already in the Debian archive, you will need to build a binary package out of it.

For your reference, this source package builds $(echo "$upload_bin_packages" | wc -l) binary package(s):
$upload_bin_packages
${NC}
eof
show_build_notice

elif [ -z "$new_bin_packages" ]; then
cat <<eof
Since the source package is already in Debian and this version does not introduce
new binaries, then you can just go ahead and directly dput the source package.

  cd build && dput ${DEBSRC}_${DEBVER}_source.changes

For your reference, this source package builds $(echo "$upload_bin_packages" | wc -l) binary package(s):
$upload_bin_packages
eof
# don't show build notice

else
cat <<eof
${RED}
ATTENTION: this upload introduces NEW binary packages not already in the Debian
archive, you will need to build a binary package out of it.

PLEASE THINK CAREFULLY BEFORE UPLOADING NEW VERSIONS WITH NEW BINARY PACKAGES,
AS SUCH UPLOADS CAN AFFECT ONGOING TRANSITIONS AND DELAY THEM SIGNIFICANTLY.

For your reference, this source package builds $(echo "$upload_bin_packages" | wc -l) binary package(s):
$upload_bin_packages

Of those, the following are NEW:
$new_bin_packages
${NC}
eof
show_build_notice

fi # end decision-making on show_build_notice

if [ -n "$rm_bin_packages" ]; then

cat <<eof

ATTENTION: The following binary packages which are currently available in
Debian unstable are no longer built from ${DEBSRC}, please investigate whether
this is intentional, and file RM requests where appropriate:

$rm_bin_packages
eof

fi

cat <<eof
This assumes you followed the "Build environment" instructions in README.rst,
for setting up a build environment for release.

If the build fails e.g. due to missing Build-Dependencies you should revert
what I did (see below) and package those missing Build-Dependencies first.

Push this pending-release branch
================================

After you have uploaded the package with dput(1), you should push $RELBRANCH so
that other people see it's been uploaded. Then, checkout another branch like
master to continue development on other packages.

  git push origin $RELBRANCH && git checkout master

eof

fi

cat >&2 <<eof
Merge the pending-release branch if/when ACCEPTED
=================================================

When it's ACCEPTED by the Debian FTP masters, you may then merge this branch
back into the master branch, delete it, and push these updates to origin.

  git checkout master && git merge $RELBRANCH && git branch -d $RELBRANCH
  git push origin master :$RELBRANCH

Delete this branch without merging if/when REJECTED
===================================================

If your upload is REJECTED, or if you cannot perform an upload in the first
place e.g. because you are not a Debian Developer, you should revert what I
just did. To do that, run:

  git checkout master && git branch -D $RELBRANCH

Then ask a Debian Developer to re-run me ($0 $*) on your behalf. Also, touch
and commit ${PKGDIR_REL}/debian/RFS so we can track these easier.
eof
