#!/bin/sh -e
#
#    release - convenient script for "releasing" an upstream project for Ubuntu
#
#    Copyright (C) 2008-2010 Dustin Kirkland <kirkland@ubuntu.com>
#
#    Authors:
#        Dustin Kirkland <kirkland@ubuntu.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


PKG=$(head -n1 debian/changelog | awk '{print $1}')
LATEST=$(distro-info --devel || echo "unstable")
VER=$(head -n 1 debian/changelog | sed 's/^.*(//' | sed 's/).*//' | sed 's/-.*//')
MAJOR=$(echo "$VER" | awk -F. '{print $1}')
MINOR=$(head -n1 debian/changelog | sed -e "s/^.*($MAJOR.//" -e "s/).*$//")


error() {
	echo "ERROR: $@" >&2
	exit 1
}

# Check for "UNRELEASED" messages in our changes file
grep -qsi "unreleased" ../${PKG}_${MAJOR}.${MINOR}-0ubuntu1_source.changes && error "'UNRELEASED' appears in ../${PKG}_${MAJOR}.${MINOR}-0ubuntu1_source.changes" || true

# Ensure that we don't have uncommitted changes
bzr_stat="$(bzr stat)" || error "Cannot 'bzr stat'"
if "$bzr_stat" | grep -v "bzr shelve"; then
	error "'bzr stat' is dirty... \n$bzr_stat"
fi

# Append our packaging to the version in the changelog
sed -i "s/) unreleased;/-0ubuntu1) $LATEST;/i" debian/changelog
(head -n1 debian/changelog | grep "$LATEST") || error "This version must be ready for release"

# Tag the release in bzr
dch --release
bzr tag --delete $MAJOR.$MINOR-0ubuntu1 || true
debcommit --release --message="releasing $MAJOR.$MINOR"

changelog=$(mktemp ../changelog-XXXXXXX)
grep -B 10000 -m1 '^ \-\- ' debian/changelog > "$changelog"

# Open the next release for development
nextminor=`expr $MINOR + 1`
if [ -r "usr/bin/$PKG" ]; then
	sed -i "s/^VERSION=.*$/VERSION=$MAJOR.$nextminor/" usr/bin/$PKG
elif [ -r "usr/bin/${PKG}.in" ]; then
	sed -i "s/^VERSION=.*$/VERSION=$MAJOR.$nextminor/" usr/bin/${PKG}.in
fi
dch -v "$MAJOR.$nextminor" "UNRELEASED"
sed -i "s/$MAJOR.$nextminor) .*;/$MAJOR.$nextminor) unreleased;/" debian/changelog
if [ -f setup.py ]; then
	sed -i "s/version='$MAJOR.$MINOR'/version='$MAJOR.$nextminor'/" setup.py
fi
if [ -f */__init__.py ]; then
	sed -i "s/__version__ = '$MAJOR.$MINOR'/__version__ = '$MAJOR.$nextminor'/" */__init__.py
fi
if [ -r snapcraft.yaml ]; then
	sed -i "s/^version:.*$/version: \"$MAJOR.$nextminor\"/" snapcraft.yaml
fi
bzr commit -m "opening $MAJOR.$nextminor"

echo
echo "# Changes:"
echo
cat "$changelog"
echo
echo "# To push:"
if [ -d "../${PKG}.git" ] && command -v multi-push >/dev/null; then
	echo "  multi-push lp:$PKG"
else
	echo "  bzr push lp:$PKG"
fi
echo
echo "# Publish tarball to https://launchpad.net/$PKG/trunk/+addrelease"
echo "  lp-project-upload ${PKG} ${MAJOR}.${MINOR} ../"$PKG"_${MAJOR}.${MINOR}.orig.tar.gz ${MAJOR}.${nextminor}" "$changelog" /dev/null
echo
if [ -f "setup.py" ]; then
	echo "# To push to PyPI:"
	echo "  cd ../${PKG}-${MAJOR}.${MINOR}/ && ./setup.py sdist upload && cd -"
	echo
fi
echo "# To upload packages:"
echo "  dput ppa:$PKG/ppa ../${PKG}_${MAJOR}.${MINOR}-0ubuntu1~*_source.changes"
echo "  dput ../${PKG}_${MAJOR}.${MINOR}-0ubuntu1_source.changes"
echo
if [ -n "$DEBIAN_STAGING" ]; then
	echo "# To upload for Debian sponsoring:"
	echo " ssh ${DEBIAN_STAGING%%:*} rm -f ${DEBIAN_STAGING##*:}/${PKG}/*"
	echo " rsync -aP ../${PKG}_${MAJOR}.${MINOR}-1* ../"$PKG"_${MAJOR}.${MINOR}.orig.tar.gz* ${DEBIAN_STAGING}/${PKG}/"
	echo
fi
