#!/bin/bash

PROGDIR=$(dirname $(readlink -f $0))
#ANT_JAVAC_TARGET="-Dant.build.javac.target=1.6 -Dant.build.javac.source=1.6"
#ANT_JAVAC_TARGET="-Dant.build.javac.target=1.7 -Dant.build.javac.source=1.7"

export JAVA_HOME=/opt/java
export PATH="$JAVA_HOME/bin:$VIRTUAL_ENV/bin:/bin:/usr/bin:/usr/local/bin:$PROGDIR"
export LANG=en_GB.UTF-8

source mksnapfuncs

typeset -i VERSION
while getopts "r:d:b:" c
do
	case $c in
	b)	BRANCH=$OPTARG
		if [ $OPTARG != 'trunk' ];then
			BRANCH_URL=branches/$OPTARG 
		fi
		;;
	r) VERSION=$OPTARG;;
	d) SNAPDIR=$OPTARG;;
	esac
done
shift $[OPTIND-1]

PRODUCT=$1
if [ "$PRODUCT" = "" ]; then
	echo Usage: "mksnap [-d dir] [-b branch] [-r vers] <product>"
	exit
fi

SVN=http://svn.mkgmap.org.uk/$PRODUCT
BRANCH=${BRANCH:-trunk}
BRANCH_URL=${BRANCH_URL:-trunk}
SNAPDIR=${SNAPDIR:-$HOME/www/mkgmap.org.uk/docroot/download}

#
# Get the real highest revision that was commited to the given branch
# that is less that the given revision.
# If no revision given then get the latest on the branch.
#
CHECK_URL=$SVN/$BRANCH_URL
if [ "$VERSION" != "" ]; then
	CHECK_URL="${CHECK_URL}@$VERSION"
fi
VERSION=$(svn log --xml --limit 1 $CHECK_URL 2> /dev/null | \
	sed -n '/<logentry/N;/\n/s/.*revision="\([0-9]*\)".*/\1/p')

RELNAME=$PRODUCT-$BRANCH-r$VERSION
RELNAME=${RELNAME/-trunk/}

TARGET=$SNAPDIR/$RELNAME.tar.gz
TARGET_ZIP=$SNAPDIR/$RELNAME.zip
TARGET_JAR=$SNAPDIR/$RELNAME.jar
TARGET_SRC=$SNAPDIR/$RELNAME-src.tar.gz
TARGET_SRC_ZIP=$SNAPDIR/$RELNAME-src.zip

BUILD_DIST=1
BUILD_TARGETS='dist'

# No standard between different products, so set the build target
# appropriate to the product.
case "$PRODUCT" in
display)
	BUILD_DIST=0
	BUILD_TARGETS='dist'
	;;
mkgmap)
	BUILD_TARGETS='dist obtain-test-input-files test'
	;;
splitter)
	BUILD_TARGETS='dist run.tests'
	;;
esac

# On a branch we do not run the tests, anything goes!
if [ $BRANCH != 'trunk' ]; then
	BUILD_TARGETS='dist'
fi

if [ ! -f $TARGET ]; then
	echo Rebuilding $TARGET
	cd /usr/tmp

	BUILD_DIR=/usr/tmp/build-mkgmap/$RELNAME
	mkdir -p $(dirname $BUILD_DIR)
	trap "rm -rf $BUILD_DIR" 0

	rm -rf $BUILD_DIR    # make sure it does not exist before export
	svn export -q -r$VERSION $SVN/$BRANCH_URL $BUILD_DIR

	cd $BUILD_DIR

	# Create version files to record the version that is being built
	if [ -d resources ]; then
		echo "svn.version: $VERSION" > resources/$PRODUCT-version.properties
		echo "build.timestamp: $(date -Iseconds)" >> resources/$PRODUCT-version.properties
	fi

	# Create a pure source distribution as tar
	tar czCf $(dirname $BUILD_DIR) $TARGET_SRC $RELNAME

	# Create source as zip
	(cd $(dirname $BUILD_DIR); zip -r $TARGET_SRC_ZIP $RELNAME) > /dev/null

	if [ "$BUILD_DIST" = 1 ]; then
		ant $ANT_JAVAC_TARGET -Dhave.version=1 $BUILD_TARGETS || {
			redis-cli lpush svn:build "failed:$PRODUCT:$BRANCH:$VERSION"
			rm -f $TARGET_SRC_ZIP $TARGET_SRC
			exit 1
		}

		CURRENT=$(redis-cli hget svn:current:$PRODUCT version)
		if [ -x $PROGDIR/mkdoc -a $VERSION -gt $CURRENT ]; then
			$PROGDIR/mkdoc $PRODUCT
		fi

		# Save the jar file separately
		cp dist/$PRODUCT.jar $TARGET_JAR

		mv dist $RELNAME
		tar czf $TARGET $RELNAME
		zip -r $TARGET_ZIP $RELNAME

		redis-cli lpush svn:build "success:$PRODUCT:$BRANCH:$VERSION"
	fi
fi

cd $SNAPDIR

if [ "$BUILD_DIST" = 1 ]; then
	mksnap_setdate $VERSION $TARGET $TARGET_ZIP \
		$TARGET_SRC $TARGET_SRC_ZIP \
		$TARGET_JAR
fi

