#!/usr/bin/env bash

# Fail fast and fail hard.
set -eo pipefail

# Setup Path variables, for later use in the Buildpack.
BIN_DIR=$(cd "$(dirname "$0")"; pwd) # absolute path
ROOT_DIR=$(dirname "$BIN_DIR")
BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3


# Set the Buildpack's internet target for downloading Python distributions.
# The user can provide BUILDPACK_VENDOR_URL to specify a custom target.
# Note: this is designed for non-Heroku use, as it does not use the user-provided
# environment variable mechanism (the ENV_DIR).
VENDOR_URL="https://lang-python.s3.amazonaws.com/$STACK"
if [[ -n ${BUILDPACK_VENDOR_URL:-} ]]; then
    VENDOR_URL="$BUILDPACK_VENDOR_URL"
fi
export VENDOR_URL

# Which versions of Python are we using?
# These variables are used to specify which versions of Python to install by default,
# as well as prompt the user to upgrade if they are using an un–supported version.
# Note: When 3.7 lands, I recommend switching to LATEST_36 and LATEST_37.
DEFAULT_PYTHON_VERSION="python-3.7.2"

mkdir -p /app/.heroku
mkdir -p "$CACHE_DIR"

# Restore old artifacts from the cache.
mkdir -p .heroku

# The Python installation.
cp -R "$CACHE_DIR/.heroku/python" .heroku/ &> /dev/null || true

# The location of the pre-compiled python binary.
VENDORED_PYTHON="${VENDOR_URL}/runtimes/$PYTHON_VERSION.tar.gz"

puts-step "Installing $PYTHON_VERSION"

set +e
# Prepare destination directory.
mkdir -p .heroku/python
if ! curl "${VENDORED_PYTHON}" -s | tar zxv -C .heroku/python &> /dev/null; then
    puts-warn "Requested runtime ($PYTHON_VERSION) is not available for this stack ($STACK)."
    puts-warn "Aborting.  More info: https://devcenter.heroku.com/articles/python-support"
    exit 1
fi

hash -r
set -e

if [[ -f Pipfile.lock ]]; then
    if [[ -f .heroku/python/Pipfile.lock.sha256 ]]; then
        if [[ $(openssl dgst -sha256 Pipfile.lock) == $(cat .heroku/python/Pipfile.lock.sha256) ]]; then
            
            if ! grep -q 'git' Pipfile.lock; then
                echo "Skipping installation, as Pipfile.lock hasn't changed since last deploy." | indent
                
                export SKIP_PIPENV_INSTALL=1
            fi
        fi
    fi
fi



if [ ! "$SKIP_PIPENV_INSTALL" ]; then
    # Pipenv support (Generate requriements.txt with pipenv).
    if [[ -f Pipfile ]]; then
        # Measure that we're using Pipenv.
        mcount "tool.pipenv"
        
        # Skip pip install, later.
        export SKIP_PIP_INSTALL=1
        
        # Set Pip env vars
        # This reads certain environment variables set on the Heroku app config
        # and makes them accessible to the pip install process.
        #
        # PIP_EXTRA_INDEX_URL allows for an alternate pypi URL to be used.
        if [[ -r "$ENV_DIR/PIP_EXTRA_INDEX_URL" ]]; then
            PIP_EXTRA_INDEX_URL="$(cat "$ENV_DIR/PIP_EXTRA_INDEX_URL")"
            export PIP_EXTRA_INDEX_URL
            mcount "buildvar.PIP_EXTRA_INDEX_URL"
        fi
        
        # Set SLUGIFY_USES_TEXT_UNIDECODE, required for Airflow versions >=1.10
        if [[ -r "$ENV_DIR/SLUGIFY_USES_TEXT_UNIDECODE" ]]; then
            SLUGIFY_USES_TEXT_UNIDECODE="$(cat "$ENV_DIR/SLUGIFY_USES_TEXT_UNIDECODE")"
            export SLUGIFY_USES_TEXT_UNIDECODE
            mcount "buildvar.SLUGIFY_USES_TEXT_UNIDECODE"
        fi
        
        export PIPENV_VERSION="2018.5.18"
        
        # Install pipenv.
        # Due to weird old pip behavior and pipenv behavior, pipenv upgrades pip
        # to latest if only --upgrade is specified. Specify upgrade strategy to
        # avoid this eager behavior.
        /app/.heroku/python/bin/pip install pipenv==$PIPENV_VERSION --upgrade --upgrade-strategy only-if-needed &> /dev/null
        
        # Install the dependencies.
        if [[ ! -f Pipfile.lock ]]; then
            puts-step "Installing dependencies with Pipenv $PIPENV_VERSION…"
            /app/.heroku/python/bin/pipenv install --system --skip-lock 2>&1 | indent
        else
            "$BIN_DIR/steps/pip-uninstall"
            cp requirements.txt .heroku/python/requirements-declared.txt
            openssl dgst -sha256 Pipfile.lock > .heroku/python/Pipfile.lock.sha256
            
            puts-step "Installing dependencies with Pipenv $PIPENV_VERSION…"
            /app/.heroku/python/bin/pipenv install --system --deploy 2>&1 | indent
        fi
        
        # Install the test dependencies, for CI.
        if [ "$INSTALL_TEST" ]; then
            puts-step "Installing test dependencies…"
            /app/.heroku/python/bin/pipenv install --dev --system --deploy 2>&1 | cleanup | indent
        fi
    fi
else
    export SKIP_PIP_INSTALL=1
fi
