#!/bin/bash
set -ex

echo "NOMKL: $NOMKL"

if [ `uname` == Darwin ]; then
    if [ $NOMKL == 0 ]; then
        export ATLAS=1
        export LDFLAGS="-headerpad_max_install_names $LDFLAGS"
    fi

    if [ "$CONDA_PY" == "39" ]; then
        COMPILER_ARGS="config_fc --fcompiler=intelem"
    else
        COMPILER_ARGS="config_cc --compiler=intelem config_fc --fcompiler=intelem"
    fi
    WHEELS_BUILD_ARGS=""
    #export CFLAGS="$CFLAGS -mmacosx-version-min=10.10 -isysroot /opt/MacOSX10.10.sdk"
    #export LDFLAGS="$LDFLAGS -bundle -Wl,-search_paths_first -L$(pwd) -mmacosx-version-min=10.10 -isysroot /opt/MacOSX10.10.sdk"
    #export SDKROOT=/opt/MacOSX10.10.sdk
    #export MACOSX_DEPLOYMENT_TARGET=10.10
else
    COMPILER_ARGS="config_cc --compiler=intelem config_fc --fcompiler=intelem"
    if [ "$CONDA_PY" == "36" ]; then
        WHEELS_BUILD_ARGS="-p manylinux1_x86_64"
    else
        WHEELS_BUILD_ARGS="-p manylinux2014_x86_64"
    fi

    # Set RPATH for wheels
    export CFLAGS="-Wl,-rpath,\$ORIGIN/../../../..,-rpath,\$ORIGIN/../../../../..,-rpath,\$ORIGIN/../../../../../.. $CFLAGS"
    export LDFLAGS="-Wl,-rpath,\$ORIGIN/../../../..,-rpath,\$ORIGIN/../../../../..,-rpath,\$ORIGIN/../../../../../.. $LDFLAGS"
fi

$PYTHON setup.py ${COMPILER_ARGS} build --force install --old-and-unmanageable

# Build wheel package
if [ -n "${WHEELS_OUTPUT_FOLDER}" ]; then
    $PYTHON setup.py ${COMPILER_ARGS} bdist_wheel ${WHEELS_BUILD_ARGS}
    cp dist/scipy*.whl ${WHEELS_OUTPUT_FOLDER}
fi
