#!/bin/bash
#
# Copyright (C) 2011-13 Mark Wiebe, DyND Developers
# BSD 2-Clause License, see LICENSE.txt
#
#
#
# This is the master linux/osx build + test script for building
# the dynd python bindings on jenkins.
#
# 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
#
#   - This script is currently hacked for centos5,
#     with http://people.centos.org/tru/devtools-2/
#     (a newer gcc) installed.
#

# Set options to echo every command and exit on error
set -xe

# 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

if [ `uname` == Darwin ]; then
  # Use default compiler on OSX
  export CC=cc
  export CXX=c++
else
  # Enable devtoolset-2, a newer gcc toolchain
  . /opt/rh/devtoolset-2/enable
  # Statically link the standard C/C++ library, because
  # we are building on an old centos5 machine.
  export LDFLAGS="-static-libgcc -static-libstdc++"
  export CC=gcc
  export CXX=g++
fi

# Remove the build subdirectory from last time
rm -rf build

# Get libdynd into the libraries subdirectory
# TODO: Build libdynd in a separate jenkins project,
#       and use its build artifact here.
rm -rf libraries
mkdir libraries
pushd libraries
git clone https://github.com/libdynd/libdynd.git
popd

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

# Use conda to create a conda environment of the required
# python version and containing the dependencies.
export PYENV_PREFIX=${WORKSPACE}/build/pyenv
rm -rf ${PYENV_PREFIX}
~/anaconda/bin/conda create --yes -p ${PYENV_PREFIX} python=${PYTHON_VERSION} cython scipy nose
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

if [ `uname` == 'Darwin' ]; then
    # On OSX, use @rpath for relative linking
    export EXTRA=-DUSE_RELATIVE_RPATH=ON
else
    export EXTRA=
fi

# Don't build the C++ tests, slightly faster builds
export EXTRA="-DDYND_BUILD_TESTS=OFF ${EXTRA}"

# Create a fresh makefile with cmake, and do the build/install
pushd build
cmake ${EXTRA} \
      -DDYND_INSTALL_LIB=OFF \
      -DCMAKE_INSTALL_PREFIX=${PYENV_PREFIX} \
      -DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE} \
      -DCMAKE_C_COMPILER=${CC} \
      -DCMAKE_CXX_COMPILER=${CXX} ..
make VERBOSE=1
make install

# Run the unit tests
${PYTHON_EXECUTABLE} -c 'import dynd;dynd.test(xunitfile="../test_results.xml", exit=1)'

# Retrieve the version number
export PYDYND_VERSION=`${PYTHON_EXECUTABLE} -c 'import dynd;print(dynd.__version__)'`
if [ '${PYDYND_VERSION}' == '' ]; then
    echo Could not determine the dynd-python version.
    exit 1
fi
export PYDYND_VERSION=${PYDYND_VERSION//-/_}
popd

# 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

# Create a conda package from the build
~/anaconda/bin/conda package -p ${PYENV_PREFIX} --pkg-name=dynd-python --pkg-version=${PYDYND_VERSION}

# Upload the package to binstar
~/anaconda/bin/binstar -t ${BINSTAR_MWIEBE_AUTH} upload --force dynd-python*.tar.bz2
~/anaconda/bin/binstar -t ${BINSTAR_BLAZE_AUTH} upload --force dynd-python*.tar.bz2

cd ..
