#!/bin/bash

cp Makefile.config.example Makefile.config

if [ "${PY3K}" -eq "0" ]; then
    PY_DIR_EXT="2.7"
else
    if [[ ${CONDA_PY} == 35 ]]; then
       PY_DIR_EXT="3.5m"
       sed -i 's,# PYTHON_LIBRARIES := boost_python3 python3.5m,PYTHON_LIBRARIES := boost_python3 python3.5m,g' Makefile.config
    fi
    if [[ ${CONDA_PY} == 36 ]]; then
        PY_DIR_EXT="3.6m"
        sed -i 's,# PYTHON_LIBRARIES := boost_python3 python3.5m,PYTHON_LIBRARIES := boost_python3 python3.6m,g' Makefile.config
    fi
fi

# Adjust Makefile.config
sed -i 's/# CPU_ONLY := 1/CPU_ONLY := 1/g' Makefile.config
#sed -i 's/# USE_OPENCV := 0/USE_OPENCV := 0/g' Makefile.config
sed -i 's/# OPENCV_VERSION := 3/OPENCV_VERSION := 3/g' Makefile.config
sed -i 's/BLAS := atlas/BLAS := mkl/g' Makefile.config
sed -i "s,PYTHON_INCLUDE := /usr/include/python2.7,PYTHON_INCLUDE := ${PREFIX}/include/python${PY_DIR_EXT},g" Makefile.config
sed -i "s,\t\t/usr/lib/python2.7/dist-packages/numpy/core/include,\t\t${SP_DIR}/numpy/core/include,g" Makefile.config
sed -i "s,PYTHON_LIB := /usr/lib,PYTHON_LIB := ${PREFIX}/lib,g" Makefile.config
sed -i "s,INCLUDE_DIRS := \$(PYTHON_INCLUDE) /usr/local/include,INCLUDE_DIRS := \$(PYTHON_INCLUDE) ${PREFIX}/include,g" Makefile.config
sed -i "s,LIBRARY_DIRS := \$(PYTHON_LIB) /usr/local/lib /usr/lib,LIBRARY_DIRS := \$(PYTHON_LIB) ${PREFIX}/lib,g" Makefile.config
sed -i "s,# CUSTOM_CXX := g++,CUSTOM_CXX := icpc,g" Makefile.config
sed -i "s,# USE_MLSL := 1,USE_MLSL := 1,g" Makefile.config
sed -i "s,# BLAS_INCLUDE := /path/to/your/blas,BLAS_INCLUDE := ${PREFIX}/include,g" Makefile.config
sed -i "s,# BLAS_LIB := /path/to/your/blas,BLAS_LIB := ${PREFIX}/lib,g" Makefile.config
sed -i "s,Q ?= @,# Q ?= @,g" Makefile.config

echo "MKLROOT=$PREFIX" >> Makefile.config
echo "MKLDNNROOT=$PREFIX" >> Makefile.config
echo "I_MPI_ROOT=$PREFIX" >> Makefile.config
echo "MLSL_ROOT=$PREFIX" >> Makefile.config
echo "WITH_PYTHON_LAYER=1" >> Makefile.config

export https_proxy=http://proxy-chain.intel.com:912

CC=icc CXX=icpc make all
CC=icc CXX=icpc make pycaffe
CC=icc CXX=icpc make DISTRIBUTE_DIR=$PREFIX distribute

# Copy MKLML libs to prefix
# cp $SRC_DIR/external/mkl/mklml_lnx_2017.0.2.20170110/lib/*.so $PREFIX/lib

# Make links to .bin files without the .bin suffix
for FILENAME in $(find "${SRC_DIR}/tools" "${SRC_DIR}/examples" -name "*.cpp" -printf "%f\n");
do
    ln -s "${PREFIX}/bin/${FILENAME/.cpp/.bin}" "${PREFIX}/bin/${FILENAME//.cpp}"
done

# Python installation is non-standard. Correct it here.
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/"

rm $PREFIX/lib/libcaffe.a

#remove utils that seg-fault on execution
util_names='device_query finetune_net net_speed_benchmark test_net train_net'
for util in $util_names; do
    if [ -f ${PREFIX}/bin/${util} ]; then
        rm -f ${PREFIX}/bin/${util}
        rm -f ${PREFIX}/bin/${util}.bin
    fi
done
