#!/bin/sh -e
#
#    release-build - convenient script for "building" an upstream project,
#                    just prior to release to 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}')
VER=$(head -n 1 debian/changelog | sed 's/^.*(//' | sed 's/).*//' | sed 's/-.*//')

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

get_orig_source() {
	# bzr export needs a --exclude option for the following to work:
	#  $ bzr export ../${PKG}_${VER}.orig.tar.gz
	# use tar instead
	tmp=$(mktemp -d)
	cp -a . $tmp/${PKG}-${VER}
	sed -e "s/ unreleased; urgency=/ released; urgency=/g" debian/changelog > $tmp/${PKG}-${VER}/ChangeLog
	if [ -r "$tmp/${PKG}-${VER}/configure.ac" ]; then
		cd $tmp/${PKG}-${VER}
		aclocal
		automake --add-missing --copy
		autoconf
		cd -
	fi
	rm -f ../${PKG}_${VER}.orig.tar.gz
	tar -C $tmp --exclude .bzr --exclude debian --exclude "*~" -zcvf ../${PKG}_${VER}.orig.tar.gz ${PKG}-${VER}
	rm -rf $tmp
	# Sign the tarball
	rm -f ../${PKG}_${VER}.orig.tar.gz.asc
	ls -halF ../${PKG}_${VER}.orig.tar.gz*
}

if ! head -n1 debian/changelog | grep -i "unreleased"; then
	if bzr diff debian/changelog | diffstat | grep -qs '1 file changed, 1 insertion(+), 1 deletion(-)'; then
		bzr revert debian/changelog
	fi
fi
head -n1 debian/changelog | grep -i "unreleased" || error "This version must be 'unreleased'"

rm -rf ../build-area/
get_orig_source

if tar -tvf ../"$PKG"_*.orig.tar.gz | egrep "\.bzr|debian|\.o$"; then
	error "Release tarball has invalid files"
fi

if [ "$(basename $0)" = "release-test" ]; then
	echo
	echo "# Test this build:"
	echo "  sudo dpkg -Oi ../$PKG_*.deb"
	echo
	echo "# If testing looks good, release-build:"
	echo "  release-build"
	echo
	exit 0
fi

olddir=$(pwd)
dir=$(mktemp -d)
cd $dir
cp -af "$olddir"/../${PKG}_${VER}.orig.tar.gz .
tar zxvf ${PKG}_${VER}.orig.tar.gz
cd "${PKG}-${VER}/"
cp -af "$olddir"/debian .
sed -i "s/) unreleased;/-0ubuntu1~lucid) lucid;/i" debian/changelog
debuild -S
sed -i "s/lucid) lucid;/precise) precise;/" debian/changelog
debuild -S
sed -i "s/precise) precise;/saucy) saucy;/" debian/changelog
debuild -S
sed -i "s/saucy) saucy;/trusty) trusty;/" debian/changelog
debuild -S
sed -i "s/~trusty) trusty;/) trusty;/" debian/changelog
debuild
debuild -S
sed -i "s/-0ubuntu1.*) .*;/-1) unstable;/" debian/changelog
debuild -S
cd ..
cp -af * "$olddir/.."
cd "$olddir"
rm -rf "$dir"

# Sign the tarball
gpg --armor --sign --detach-sig ../${PKG}_${VER}.orig.tar.gz

echo
echo
echo "# Test this build:"
echo "  sudo dpkg -Oi ../*_${VER}-0ubuntu1*.deb"
echo
echo "# If everything looks good, release:"
echo "  release"
echo
echo "# Otherwise:"
echo "  bzr revert debian/changelog; rm debian/changelog*~"
echo
echo
