#!/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/# 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,# DISABLE_MKLDNN_DOWNLOAD := 1,DISABLE_MKLDNN_DOWNLOAD := 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 "MLSL_ROOT=$PREFIX" >> Makefile.config
echo "WITH_PYTHON_LAYER=1" >> Makefile.config
#echo "CAFFE_PER_LAYER_TIMINGS=1" >> Makefile.config

export https_proxy=http://child-prc.intel.com:913
source /opt/intel/bin/compilervars.sh intel64

# Enabled mkl-dnn
MKLDNNROOT=${PREFIX}
MKLDNN_COMMIT=`cat ./mkldnn.commit`
MKLDNN_SRC=${PREFIX}/mkldnn
mkdir ${MKLDNN_SRC}
MKLDNN_GITHUB=https://github.com/01org/mkl-dnn.git
git clone --no-checkout ${MKLDNN_GITHUB} ${MKLDNN_SRC}
checkout=`cd ${MKLDNN_SRC} && git reset --hard ${MKLDNN_COMMIT}`
echo $checkout
cmake ${MKLDNN_SRC} -DCMAKE_INSTALL_PREFIX=${PREFIX} -DMKLROOT=${MKLROOT} -B${MKLDNN_SRC} -DCMAKE_CXX_COMPILER="icpc" -DCMAKE_C_COMPILER="icc"
make -C ${MKLDNN_SRC} -j
make -C ${MKLDNN_SRC} install
rm -rf ${MKLDNN_SRC}

# Enabled mlsl
MLSL_DST=${PREFIX}/mlsl
mkdir ${MLSL_DST}
GITHUB_RELEASE_TAG=v2018.1-Preview
ARCHIVE_BASENAME=l_mlsl_2018.1.005.tgz
MLSLURL="https://github.com/01org/MLSL/releases/download/$GITHUB_RELEASE_TAG/$ARCHIVE_BASENAME"
wget --no-check-certificate -P ${MLSL_DST} $MLSLURL -O ${MLSL_DST}/${ARCHIVE_BASENAME}
tar -xzf ${MLSL_DST}/${ARCHIVE_BASENAME} -C ${MLSL_DST}
tar -xzf ${MLSL_DST}/l_mlsl*/files.tar.gz -C ${MLSL_DST}
sed -i -e "s|MLSL_SUBSTITUTE_INSTALLDIR|${PREFIX}|g" ${MLSL_DST}/intel64/bin/mlslvars.sh
#sed -i -e "s|intel64/||g" ${MLSL_DST}/intel64/bin/mlslvars.sh
cp -r ${MLSL_DST}/intel64/* ${PREFIX}/
cp -r ${MLSL_DST}/intel64 ${PREFIX}/
rm -rf ${MLSL_DST}

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

cp $SRC_DIR/external/ideep/dlcp/lib/* $PREFIX/lib
#Copy total dir to prefix
sed -i "s/external\/mlsl/..\/bin/g" scripts/set_env.sh
sed -i "s/build\/tools/..\/bin/g" scripts/run_intelcaffe.sh
sed -i "/pycaffe/d" scripts/calibrator.py scripts/sampling.py scripts/check_winograd.py
sed -i "s;tools\/;\/;g" scripts/calibrator.py
mkdir ${PREFIX}/caffe
mkdir ${PREFIX}/caffe/examples
cp -r $SRC_DIR/scripts $SRC_DIR/models $PREFIX/caffe/
cp -r $SRC_DIR/examples/faster-rcnn  $SRC_DIR/examples/rfcn $SRC_DIR/examples/ssd $SRC_DIR/examples/yolo $PREFIX/caffe/examples/

# 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

#remove info dir
rm -rf ${PREFIX}/info/test
