#! /bin/bash
# Release current libpqxx trunk (or other branch).
#
# Use this after a successful preprelease run in the parent series.
#
# Arguments:
#   <next version number> [parent]
#
# The next version number is the one for a future release in the parent series,
# not the version that's being released now.  So if you're releasing x.y.0, that
# would be x.y+1.0.  If you're releasing some other x.y.z, it's x.y.z+1.
#
# The parent branch defaults to "trunk."  For x.y.z releases (where z > 0), use
# branches/x.y.
set -e

NEXTVERSION="$1"
if test -z "$NEXTVERSION"
then
	echo "Usage: $0 <next-version> [parent]" >&2
	exit 1
fi

PARENT="$2"
if test -z "$PARENT"
then
	PARENT="trunk"
fi

REPO="svn+ssh://pqxx.org/srv/svn/libpqxx/"
FTP="/srv/ftp/libpqxx"
SNAPSHOT="/home/jtv/public_html/tmp/pqxx/snapshot"
DOC="/srv/www/devprojects/libpqxx/doc"

CHECKOUT="`mktemp -p /tmp -d pqxx.XXXXXXXXXX`"
echo "** Checking out source tree to $CHECKOUT **"
cd "$CHECKOUT"
svn co -q "$REPO"
cd libpqxx/

export PATH="$PATH:$CHECKOUT/libpqxx/$PARENT/tools"

pushd "$PARENT" >/dev/null
PQXXVERSION="`extract_version`"
popd >/dev/null

if test "$PQXXVERSION" = "$NEXTVERSION"
then
	cat <<EOF >&2
The given "next version" is the same as the existing version in
$PARENT/PQXXVERSION.  Instead, give the next version for a future
release in the same series.
EOF
	exit 2
fi

RELEASEDATE="`date +'%a, %d %b %y %T %z'`"


echo "** Updating source tree **"
svn cp "$PARENT" "tags/$PQXXVERSION"


# Mark new trunk in debian/changelog.
cat - <<EOF "tags/$PQXXVERSION"/debian/changelog >"$PARENT"/debian/changelog
libpqxx ($NEXTVERSION-1) unstable; urgency=medium

  * Forked release $PQXXVERSION.

 -- Jeroen T. Vermeulen <jtv@xs4all.nl>  $RELEASEDATE

EOF

# Mark next version in NEWS.
cat - <<EOF "tags/$PQXXVERSION/NEWS" >"$PARENT/NEWS"
$NEXTVERSION
EOF

echo "PQXX_VERSION	$NEXTVERSION" >"$PARENT"/VERSION

svn commit -m "Forking release $PQXXVERSION, moving on to $NEXTVERSION."
cd /tmp
rm -rf -- "$CHECKOUT"


echo "** Setting up new documentation **"
TEMPDOC="`mktemp -p /tmp -d pqxxdoc.XXXXXXXXXX`"
cd "$TEMPDOC"
tar xzf "$SNAPSHOT"/libpqxx-*.tar.gz
cd libpqxx-*/

PREVIOUS=''
for d in `ls "$DOC" | grep '[0-9]'`
do
	PREVIOUS="$PREVIOUS --link-dest='$d'"
done

rsync -r $PREVIOUS doc/html "$DOC/$PQXXVERSION"

cd /tmp
rm -rf "$TEMPDOC"

if test "$PARENT" = "trunk"
then
	# Move snapshot tarball to FTP directory.
	mv "$SNAPSHOT"/libpqxx-*.tar.* "$FTP/"
else
	echo "*** Remember: Build tarball and make available for upload ***"
fi

echo "** Done **"
