#!/bin/bash

# 2 core available on Travis CI OS X workers: https://docs.travis-ci.com/user/ci-environment/#Virtualization-environments
# CPU_COUNT is passed through conda build: https://github.com/conda/conda-build/pull/1149
export CPU_COUNT=2

export PYTHONUNBUFFERED=1

# TODO: this has moved to conda_build_config.yaml, but we are keep it here for a little while, until
#   the dust settles on using conda-build 3.  If this value differs from the one in conda_build_config.yaml,
#   things might get confusing, so make sure to keep them the same, and remove this value when the new
#   way is working well enough.
export MACOSX_DEPLOYMENT_TARGET="10.9"

if [ "$CIRCLECI" == "true" ]; then
    echo "Downloading sdk"
    curl -L -O https://github.com/phracker/MacOSX-SDKs/releases/download/10.13/MacOSX10.9.sdk.tar.xz
    tar -xf MacOSX10.9.sdk.tar.xz -C $(xcode-select -p)/Platforms/MacOSX.platform/Developer/SDKs/
    sed -i '' "s|<string>10.11</string>|<string>10.9</string>|g" $(xcode-select -p)/Platforms/MacOSX.platform/Info.plist
fi

export CONDA_BUILD_SYSROOT="$(xcode-select -p)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${MACOSX_DEPLOYMENT_TARGET}.sdk"

if [ -d "${CONDA_BUILD_SYSROOT}" ]
then
    echo "Found CONDA_BUILD_SYSROOT: ${CONDA_BUILD_SYSROOT}"
else
    echo "Missing CONDA_BUILD_SYSROOT: ${CONDA_BUILD_SYSROOT}"
    exit 1
fi

if [ ! -z "$CONFIG" ]; then
    echo "" >> ./.ci_support/${CONFIG}.yaml
    echo "CONDA_BUILD_SYSROOT:" >> ./.ci_support/${CONFIG}.yaml
    echo "- ${CONDA_BUILD_SYSROOT}" >> ./.ci_support/${CONFIG}.yaml
    echo "" >> ./.ci_support/${CONFIG}.yaml
    cat ./.ci_support/${CONFIG}.yaml
fi

conda config --set show_channel_urls true
conda config --set auto_update_conda false
conda config --set add_pip_as_python_dependency false

conda info
conda config --show-sources
