#!/bin/sh

if test ! -f version.properties
then
    echo >&2 Could not find version.properties. Run this command from the root of the project.
    exit 1
fi
VERSION=`sed -n 's|version=||p' version.properties`

echo Attempting to publish: $VERSION
echo

JAVA_VERSION=`java -version 2>&1 | head -1`
echo $JAVA_VERSION | grep -q '1\.6\.'
if test $? -ne 0
then
    echo "ParSeq releases can only be built using a Java 1.6 JDK."
    echo "Java reports its version to be: $JAVA_VERSION"
    exit 1
fi

DIRTY="`git status --porcelain || echo FAIL`"
if test -n "$DIRTY"
then
    echo "Dirty index or working tree. Use git status to check." 2>&1
    echo "After resolution, run this command again." 2>&1
    exit 1
fi

INCONSISTENT="`git diff origin/master || echo FAIL`"
if test -n "$INCONSISTENT"
then
    echo "origin/master and current branch are inconsistent." 2>&1
    echo "Use git diff origin/master to see changes." 2>&1
    echo "Rebase or push, as appropriate, and run this command again." 2>&1
    exit 1
fi

CHANGELOG=`grep v$VERSION CHANGELOG`
if test $? -ne 0
then
    echo "No entry in the CHANGELOG for version $VERSION." 2>&1
    echo "To get a list of changes, use git log previous_tag.." 2>&1
    echo "Add an entry to the CHANGELOG and run this command again." 2>&1
    exit 1
fi

ant clean build test dist && \
git tag v$VERSION && \
git push origin v$VERSION && \
echo "Publish completed successfully"
