#!/bin/bash
# Initially based on a snippet from the greenlet project.
# This needs to be run from the root of the project.
# To update: docker pull quay.io/pypa/manylinux2010_x86_64
set -e
export PYTHONUNBUFFERED=1
export PYTHONDONTWRITEBYTECODE=1

# Use a fixed hash seed for reproducability
export PYTHONHASHSEED=8675309
# Disable tests that use external network resources;
# too often we get failures to resolve DNS names or failures
# to connect on AppVeyor.
export GEVENTTEST_USE_RESOURCES="-network"
export CI=1
export TRAVIS=true
export GEVENT_MANYLINUX=1
# Don't get warnings about Python 2 support being deprecated. We
# know. The env var works for pip 20.
export PIP_NO_PYTHON_VERSION_WARNING=1
export PIP_NO_WARN_SCRIPT_LOCATION=1

# Build configuration.
# Temp disabled ccache for 3.3.1.
# export CC="ccache gcc"
# export CXX="ccache g++"
# export LDCXXSHARED="ccache g++ -shared"
# export LDSHARED="ccache gcc -shared"
export CCACHE_NOCPP2=true
export CCACHE_SLOPPINESS=file_macro,time_macros,include_file_ctime,include_file_mtime
export CCACHE_NOHASHDIR=true
export CCACHE_BASEDIR="/RelStorage"
export CFLAGS="-pipe -Ofast"
export CXXFLAGS="-pipe -Ofast"
export BUILD_LIBS=$HOME/.libs
# Share the ccache directory
export CCACHE_DIR="/ccache"

if [ -d /RelStorage -a -d /opt/python ]; then
    # Running inside docker

    # Set a cache directory for pip. This was
    # mounted to be the same as it is outside docker so it
    # can be persisted.
    # XXX: This works for macOS, where everything bind-mounted
    # is seen as owned by root in the container. But when the host is Linux
    # the actual UIDs come through to the container, triggering
    # pip to disable the cache when it detects that the owner doesn't match.
    # The workaround is to use ``-u`` in the call to docker run,
    # but that fails when we need to be root inside the container,
    # such as to run ``yum``. The ``sudo`` command isn't available.
    ## export XDG_CACHE_HOME="/cache"
    id
    ls -ld /cache
    ls -ld /cache/pip

    yum -y install ccache

    cd /RelStorage
    rm -rf wheelhouse
    mkdir wheelhouse
    for variant in `ls -d /opt/python/cp*`; do
        echo "Building $variant"
        mkdir /tmp/build
        cd /tmp/build
        git clone /RelStorage RelStorage
        cd RelStorage
        $variant/bin/pip install -U pip
        $variant/bin/pip install -U 'cython>=3.0a6' setuptools
        PATH=$variant/bin:$PATH $variant/bin/python setup.py bdist_wheel
        auditwheel show dist/RelStorage*.whl
        auditwheel repair dist/RelStorage*.whl
        cp wheelhouse/RelStorage*.whl /RelStorage/wheelhouse
        cd /RelStorage
        rm -rf /tmp/build
    done
    rm -rf dist build *.egg-info
    exit 0
fi

# Mount the current directory as /RelStorage
# Mount the pip cache directory as /cache
# and the ccache directory as /ccache
# `pip cache` requires pip 20.1
echo Setting up caching
python --version
python -mpip --version
LCACHE="$(dirname `python -mpip cache dir`)"
echo Sharing pip cache at $LCACHE $(ls -ld $LCACHE)
echo Sharing ccache dir at $HOME/.ccache
if [ ! -d $HOME/.ccache ]; then
    mkdir $HOME/.ccache
fi

docker run --rm -e GITHUB_ACTIONS -e DOCKER_IMAGE -v "$(pwd):/RelStorage" -v "$LCACHE:/cache" -v "$HOME/.ccache:/ccache" ${DOCKER_IMAGE:-quay.io/pypa/manylinux2010_x86_64} /RelStorage/scripts/releases/$(basename $0)
ls -l wheelhouse
