#!/usr/bin/env bash

# Consolidated installation script for use by both Travis and humans.
#
# First installs a known-good version of pip, then any requirements
# specified in the EXTERNAL_REQUIREMENTS environment variable (e.g.,
# coveralls); then installs the project requirements, constrained by
# etc/requirements_locked.txt; then editably installs zipline itself.
#
# Forwards positional arguments to all invocations of pip install.

# Travis' env command doesn't permit options in the shebang line, so
# set them here.
set -euvxo pipefail

echo
echo "Installing zipline using $(which python)"
echo

# New releases of pip have frequently caused strange issues. Make sure
# we know exactly which version we're working with.
python -m pip install pip==19.2.2 'setuptools<46' $@

# Install external requirements first: if they share any of our
# transitive dependencies, we want our pinned versions to win.
if [ "${EXTERNAL_REQUIREMENTS:-}" ]; then
    # Note: If EXTERNAL_REQUIREMENTS is unset, the expression in the
    # above test expands to the empty string, which fails the test.
    # (Simply expanding $EXTERNAL_REQUIREMENTS causes an error with the
    # -u option, which helps prevent many other kinds of errors.)
    echo "Installing additional packages: $EXTERNAL_REQUIREMENTS"
    python -m pip install "$EXTERNAL_REQUIREMENTS" $@
fi

# These have to be installed first so that the other requirements can be
# compiled against the specific versions we use.
python -m pip install -r etc/requirements_build.in -c etc/requirements_locked.txt $@

# XXX: bcolz has to be compiled against our specific version of numpy:
# by default, it uses an incompatible pre-compiled binary.
python -m pip install --no-binary=bcolz -e .[all] -r etc/requirements_blaze.in -c etc/requirements_locked.txt $@

# TODO: resolve these error messages:
# flake8 3.6.0 has requirement setuptools>=30, but you'll have setuptools 28.8.0 which is incompatible.
# blaze keepalive-30.g31060532 has requirement odo>=0.5.0, but you'll have odo 0.3.2+729.gda7f26d which is incompatible.

echo
echo "Installation complete! Try running 'zipline --help'."
echo
