#!/bin/sh
set -e
set -x

ANT_HOME=/export/home/hudson/tools/ant/latest
SVNVERSION_EXE=/opt/subversion-1.6.9/bin/svnversion
SVN_EXE=/opt/subversion-1.6.9/bin/svn
MAVEN_SUPPORT=/export/home/nigel/hudsonSupport/maven
CLOVER=/export/home/hudson/tools/clover/clover2latest

SVN_BRANCH_DIR=trunk

#Update the Version # when doing a release
MAVEN_ARTIFACT_VERSION=3.1-SNAPSHOT

ARTIFACTS=$WORKSPACE/artifacts
MAVEN_ARTIFACTS=$WORKSPACE/maven_artifacts

mkdir -p $ARTIFACTS
mkdir -p $MAVEN_ARTIFACTS
cd $WORKSPACE/$SVN_BRANCH_DIR

echo "Workspace: $WORKSPACE"

#Package the Source
$ANT_HOME/bin/ant -Dversion=$BUILD_ID \
  -Dsvnversion.exe=$SVNVERSION_EXE \
  -Dsvn.exe=$SVN_EXE \
  clean package-tgz-src
# release it
cp dist/*-src.tar.gz $ARTIFACTS

# run build
$ANT_HOME/bin/ant \
  -Dsvnversion.exe=$SVNVERSION_EXE \
  -Dsvn.exe=$SVN_EXE \
  -Dversion=$BUILD_ID \
  test package-tgz
# release it
cp dist/*.tar.gz $ARTIFACTS

#Generate the Maven snapshot
$ANT_HOME/bin/ant -lib $MAVEN_SUPPORT \
  -Dsvnversion.exe=$SVNVERSION_EXE \
  -Dsvn.exe=$SVN_EXE \
  -Dversion=$MAVEN_ARTIFACT_VERSION \
  clean generate-maven-artifacts
#copy the artifacts to the side so the cron job can publish them
echo "Copying Maven artifacts to $MAVEN_ARTIFACTS"
cp -R dist/maven/org/apache/lucene $MAVEN_ARTIFACTS
echo "Done Copying Maven Artifacts"

#Rerun testsuite with clover on
set +e
$ANT_HOME/bin/ant \
  -lib $CLOVER \
  -Dsvnversion.exe=$SVNVERSION_EXE \
  -Dsvn.exe=$SVN_EXE \
  -Dversion=$BUILD_ID \
  -Drun.clover=true \
  clean test
# preserve the return value
RET=$?
set -e

#generate the clover reports and javadocs even when tests failed before
ANT_OPTS="-Xmx896M" $ANT_HOME/bin/ant \
  -lib $CLOVER \
  -Dsvnversion.exe=$SVNVERSION_EXE \
  -Dsvn.exe=$SVN_EXE \
  -Dversion=$BUILD_ID \
  -Drun.clover=true \
  generate-clover-reports

#build javadocs & changes again
$ANT_HOME/bin/ant -Dversion=$BUILD_ID \
  -Dsvnversion.exe=$SVNVERSION_EXE \
  -Dsvn.exe=$SVN_EXE \
  javadocs changes-to-html

# return the test return value
exit $RET