#!/bin/bash
#
#
# This is the jenkins build script for building/testing
# Blaze.
#
# Jenkins Requirements:
#   - Anaconda should be installed in ~/anaconda
#   - Use a jenkins build matrix for multiple
#     platforms/python versions
#   - Use the XShell plugin to launch this script
#   - Call the script from the root workspace
#     directory as buildscripts/jenkins-build
#

# Require a version of Python to be selected
if [ "${PYTHON_VERSION}" == "" ]; then
 echo You must select a Python version with the PYTHON_VERSION variable.
 exit 1
fi

# Try to delete the build directory twice, with a pause in
# between, because on an OSX build machine this was failing
# on occasion.
rm -rf ${WORKSPACE}/build
sleep 1
rm -rf ${WORKSPACE}/build

if [ -d ${WORKSPACE}/build ]; then
 echo Failed to delete temporary build dir ${WORKSPACE}/build
 exit 1
fi

# Use conda to create a conda environment of the required
# python version and containing the dependencies.
# TODO: Re-add cffi to this list when it is in anaconda python 3.3
export PYENV_PREFIX=${WORKSPACE}/build/pyenv

~/anaconda/bin/conda create --yes \
    --channel https://conda.binstar.org/mwiebe -p ${PYENV_PREFIX} python=${PYTHON_VERSION} \
    cython scipy ply dynd-python nose flask pyparsing pyyaml setuptools \
    dateutil pip pytables sqlalchemy h5py pandas requests pytest \
    toolz cytoolz bcolz psutil || exit 1


export PATH=${PYENV_PREFIX}/bin:${PATH}

if [ -f "${PYENV_PREFIX}/bin/python" ]; then
 export PYTHON_EXECUTABLE=${PYENV_PREFIX}/bin/python
elif [ -f "${PYENV_PREFIX}/bin/python3" ]; then
 export PYTHON_EXECUTABLE=${PYENV_PREFIX}/bin/python3
else
 echo Conda environment creation failed.
 exit 1
fi

# Temporary hack to install datashape
rm -rf datashape
git clone https://github.com/ContinuumIO/datashape.git || exit 1
pushd datashape
${PYTHON_EXECUTABLE} setup.py install || exit 1
popd

# BColz needs unittest2 for Python 2.6
if [ "${PYTHON_VERSION}" == "2.6" ]; then
  pip install unittest2
fi

pip install multipledispatch || exit 1

# Build/install Blaze
${PYTHON_EXECUTABLE} setup.py install || exit 1

py.test --doctest-modules -vv --pyargs blaze --junitxml=test_results.xml || exit 1
echo `ls`
echo `git describe --tags --dirty --always --match [0-9]*`
echo `pwd`
# Retrieve the version number
# X.X.X.dev builds
version=`git describe --tags --dirty --always --match [0-9]*`
BLAZE_VERSION=`echo $version | tr "-" _`
echo BLAZE_VERSION, ${BLAZE_VERSION}
if [ '${BLAZE_VERSION}' == '' ]; then
    echo Could not determine the blaze version.
    exit 1
fi

# Put the conda package by itself in the directory pkgs
rm -rf pkgs
mkdir pkgs
cd pkgs
if [ `uname` == Darwin ]; then
 mkdir osx-64
 cd osx-64
elif [ `uname -m` == x86_64 ]; then
 mkdir linux-64
 cd linux-64
else
 mkdir linux-32
 cd linux-32
fi

# Make sure binstar is installed in the main environment
echo Updating binstar...
~/anaconda/bin/conda install --yes binstar
~/anaconda/bin/binstar --version

# Create a conda package from the build
~/anaconda/bin/conda package -p ${PYENV_PREFIX} --pkg-name=blaze --pkg-version=${BLAZE_VERSION}
echo `ls`
echo ${BLAZE_VERSION}
echo ${PYENV_PREFIX}
# Upload the package to binstar
# ~/anaconda/bin/binstar -t ${BINSTAR_BLAZE_AUTH} upload --force blaze*.tar.bz2

