#!/bin/bash
# Copyright (c) 2009 Cloudera, inc
#
# Performs a release build

set -e

if [ $(uname -s) = "SunOS" ]; then
if [ $(isainfo -b) != "64" ]; then
  echo Release build should be done on a 64-bit box to generate 1>&2
  echo both 64 and 32 bit native libraries. 1>&2
  exit 1
fi
else
if [ $(uname -m) != "x86_64" ]; then
  echo Release build should be done on a 64-bit box to generate 1>&2
  echo both 64 and 32 bit native libraries. 1>&2
  exit 1
fi
fi

JAVA32_HOME=${JAVA32_HOME:-$JAVA_HOME}
JAVA64_HOME=${JAVA64_HOME:-$JAVA_HOME}

# Check that JVMs support the right architectures
# Passing -d with an architecture that's not supported results
# in a non-zero exit code even with -version
if ! $JAVA32_HOME/bin/java -d32 -version ; then
  echo Your jvm in $JAVA32_HOME seems like it doesnt support 32-bit mode 1>&2
  echo Please set JAVA32_HOME to point to a 32-bit JDK
  exit 1
fi

if ! $JAVA64_HOME/bin/java -d64 -version ; then
  echo Your jvm in $JAVA64_HOME seems like it doesnt support 64-bit mode 1>&2
  echo Please set JAVA64_HOME to point to a 64-bit JDK
  exit 1
fi

if ! [ -e $JAVA5_HOME/bin/java ]; then
  echo No JAVA5_HOME set. Forrest documentation requires Java 5 to be 1>&2
  echo installed. 1>&2
  exit 1
fi

if ! [ -e $FORREST_HOME/bin/forrest ]; then
  echo No FORREST_HOME set. Forrest documentation requires Forrest to be 1>&2
  echo installed. 1>&2
  exit 1
fi

# Do the build
BIN_DIR=$(readlink -f $(dirname $0))
RELEASE_DIR=$BIN_DIR/..

cd $RELEASE_DIR

if which aclocal-1.9 && which autoconf ; then
  ant -Dcompile.c++=true \
    -Dcompile.native=true create-c++-configure
fi

if which libtoolize && libtoolize --version | grep --silent 2.2 ; then
  for dir in src/native src/examples/pipes src/c++/libhdfs/ src/c++/pipes src/c++/utils ; do
    (cd $dir && libtoolize --force)
  done
fi
  
for dir in src/native src/examples/pipes src/c++/libhdfs/ src/c++/pipes src/c++/utils ; do
    (cd $dir && rm -rf autom4te.cache) 
done


JAVA_HOME=$JAVA32_HOME \
  CFLAGS=-m32 \
  CXXFLAGS=-m32 \
  ant \
  -Dlibhdfs=true \
  -Dcompile.native=true \
  -Dcompile.c++=true \
  -Djava5.home=$JAVA5_HOME \
  -Dforrest.home=$FORREST_HOME \
  -propertyfile cloudera/build.properties \
  clean package-native

if [ -z "$SKIP_JDIFF" ]; then
ant \
  -Djdiff.stable=0.20.1 \
  -Djdiff.build.dir=build/docs/jdiff-cloudera \
  -propertyfile cloudera/build.properties api-report
fi

# Build the sqoop docs
pushd $RELEASE_DIR/src/contrib/sqoop
JAVA_HOME=$JAVA64_HOME \
  ant \
  -Djava5.home=$JAVA5_HOME \
  -Dforrest.home=$FORREST_HOME \
  -propertyfile cloudera/build.properties \
  doc
popd
# Copy them into the main build directory to be included in the tarball
mkdir -p build/hadoop-$FULL_VERSION/docs/
cp -r build/contrib/sqoop/doc/ build/hadoop-$FULL_VERSION/docs/sqoop

cp build/hadoop-$FULL_VERSION/docs/sqoop/sqoop.1.gz \
   build/hadoop-$FULL_VERSION/docs/sqoop/sqoop-0.20.1.gz

JAVA_HOME=$JAVA64_HOME \
  CFLAGS=-m64 \
  CXXFLAGS=-m64 \
  ant \
  -Dlibhdfs=true \
  -Dcompile.native=true \
  -Dcompile.c++=true \
  -Djava5.home=$JAVA5_HOME \
  -Dforrest.home=$FORREST_HOME \
  -propertyfile cloudera/build.properties \
  compile-core-native compile-c++ compile-c++-examples tar
