#!/bin/bash

set -ex

# Setup CMake build location
mkdir build
cd build

# Configure, build, test, and install.
if [ "$(uname)" == "Linux" ];
then
    # Stop Boost from using libquadmath.
    export CXXFLAGS="${CXXFLAGS} -DBOOST_MATH_DISABLE_FLOAT128"
fi

BLAS=open

cmake -DCPU_ONLY=1                                          \
      -DBLAS="${BLAS}"                                      \
      -DCMAKE_INSTALL_PREFIX="${PREFIX}"                    \
      -DNUMPY_INCLUDE_DIR="${SITE_PKGS}/numpy/core/include" \
      -DNUMPY_VERSION=${NPY_VER}                            \
      -DPYTHON_EXECUTABLE="${PREFIX}/bin/python"            \
      -DBUILD_docs="OFF"                                    \
      ..
make -j${CPU_COUNT}
# Tests disabled, currently failing with core dump in HDF5 library
make -j${CPU_COUNT} runtest
make install

# Python installation is non-standard. So, we're fixing it.
mv "${PREFIX}/python/caffe" "${SP_DIR}/"
for FILENAME in $( cd "${PREFIX}/python/" && find . -name "*.py" | sed 's|./||' );
do
    chmod +x "${PREFIX}/python/${FILENAME}"
    cp "${PREFIX}/python/${FILENAME}" "${PREFIX}/bin/${FILENAME//.py}"
done
rm -rf "${PREFIX}/python/"
