#!/usr/bin/env bash

# The Heroku Python Buildpack. This script accepts parameters for a build
# directory, a cache directory, and a directory for app environment variables.

# Warning: there are a few hacks in this script to accommodate excellent builds
# on Heroku. No guarantee for external compatibility is made. However,
# everything should work fine outside of the Heroku environment, if the
# environment is setup correctly.

# Usage:
#
#     $ bin/compile <build-dir> <cache-dir> <env-path>

# Fail fast and fail hard.
set -eo pipefail

sysctl -w net.core.somaxconn=4096

# Standard Library.
export BPLOG_PREFIX="buildpack.python"
export BUILDPACK_LOG_FILE=${BUILDPACK_LOG_FILE:-/dev/null}

[ "$BUILDPACK_XTRACE" ] && set -o xtrace

# Prepend proper path for virtualenv hackery. This will be deprecated soon.
export PATH=:/usr/local/bin:$PATH

# Paths.
BIN_DIR=$(cd "$(dirname "$0")"; pwd) # absolute path
ROOT_DIR=$(dirname "$BIN_DIR")
BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3

export BUILD_DIR CACHE_DIR ENV_DIR

# Python defaults
DEFAULT_PYTHON_VERSION="python-3.6.4"
LATEST_3="python-3.6.4"
LATEST_2="python-2.7.14"

DEFAULT_PYTHON_STACK="cedar-14"
PIP_UPDATE="9.0.1"

export DEFAULT_PYTHON_VERSION DEFAULT_PYTHON_STACK PIP_UPDATE LATEST_2 LATEST_3

# Common Problem Warnings
WARNINGS_LOG=$(mktemp)
export WARNINGS_LOG
export RECOMMENDED_PYTHON_VERSION=$DEFAULT_PYTHON_VERSION

# Setup vendored tools and pip-pop (pip-diff)
export PATH=$PATH:$ROOT_DIR/vendor/:$ROOT_DIR/vendor/pip-pop

# Support Anvil Build_IDs
[ ! "$SLUG_ID" ] && SLUG_ID="defaultslug"
[ ! "$REQUEST_ID" ] && REQUEST_ID=$SLUG_ID
[ ! "$STACK" ] && STACK=$DEFAULT_PYTHON_STACK

# Sanitizing environment variables.
unset GIT_DIR PYTHONHOME PYTHONPATH
unset RECEIVE_DATA RUN_KEY BUILD_INFO DEPLOY LOG_TOKEN
unset CYTOKINE_LOG_FILE GEM_PATH

# Syntax sugar.
# shellcheck source=bin/utils
source "$BIN_DIR/utils"

# Import collection of warnings.
# shellcheck source=bin/warnings
source "$BIN_DIR/warnings"

# we need to put a bunch of symlinks in there later
mkdir -p /app/.heroku

# Set up outputs under new context
PROFILE_PATH="$BUILD_DIR/.profile.d/python.sh"
EXPORT_PATH="$BIN_DIR/../export"
GUNICORN_PROFILE_PATH="$BUILD_DIR/.profile.d/python.gunicorn.sh"
WEB_CONCURRENCY_PROFILE_PATH="$BUILD_DIR/.profile.d/WEB_CONCURRENCY.sh"

# We'll need to send these statics to other scripts we `source`.
export BUILD_DIR CACHE_DIR BIN_DIR PROFILE_PATH EXPORT_PATH

# Prepend proper environment variables for Python use.
export PATH=/app/.heroku/python/bin:/app/.heroku/vendor/bin:$PATH
export PYTHONUNBUFFERED=1
export LANG=en_US.UTF-8
export C_INCLUDE_PATH=/app/.heroku/vendor/include:/app/.heroku/python/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/app/.heroku/vendor/include:/app/.heroku/python/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/app/.heroku/vendor/lib:/app/.heroku/python/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/app/.heroku/vendor/lib:/app/.heroku/python/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/app/.heroku/vendor/lib/pkg-config:/app/.heroku/python/lib/pkg-config:$PKG_CONFIG_PATH

# Switch to the repo's context.
cd "$BUILD_DIR"

# Warn for lack of Procfile.
if [[ ! -f Procfile ]]; then
  puts-warn 'Warning: Your application is missing a Procfile. This file tells Heroku how to run your application.'
  puts-warn 'Learn more: https://devcenter.heroku.com/articles/procfile'
fi

# Prepare the cache.
mkdir -p "$CACHE_DIR"

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

cp -R "$CACHE_DIR/.heroku/python" .heroku/ &> /dev/null || true
cp -R "$CACHE_DIR/.heroku/python-stack" .heroku/ &> /dev/null || true
cp -R "$CACHE_DIR/.heroku/python-version" .heroku/ &> /dev/null || true
cp -R "$CACHE_DIR/.heroku/vendor" .heroku/ &> /dev/null || true
if [[ -d "$CACHE_DIR/.heroku/src" ]]; then
  cp -R "$CACHE_DIR/.heroku/src" .heroku/ &> /dev/null || true
fi

# Experimental pre_compile hook.
# shellcheck source=bin/steps/hooks/pre_compile
source "$BIN_DIR/steps/hooks/pre_compile"

# Sticky runtimes.
if [ -f "$CACHE_DIR/.heroku/python-version" ]; then
  DEFAULT_PYTHON_VERSION=$(cat "$CACHE_DIR/.heroku/python-version")
fi

# Stack fallback for non-declared caches.
if [ -f "$CACHE_DIR/.heroku/python-stack" ]; then
  CACHED_PYTHON_STACK=$(cat "$CACHE_DIR/.heroku/python-stack")
else
  CACHED_PYTHON_STACK=$STACK
fi

export CACHED_PYTHON_STACK

# Pipenv Python version support.
# shellcheck source=bin/steps/pipenv-python-version
source "$BIN_DIR/steps/pipenv-python-version"

# If no runtime given, assume default version.
if [ ! -f runtime.txt ]; then
  echo "$DEFAULT_PYTHON_VERSION" > runtime.txt
fi

mkdir -p "$(dirname "$PROFILE_PATH")"
mkdir -p /app/.heroku/src

if [[ $BUILD_DIR != '/app' ]]; then
    # python expects to reside in /app, so set up symlinks
    # we will not remove these later so subsequent buildpacks can still invoke it
    ln -nsf "$BUILD_DIR/.heroku/python" /app/.heroku/python
    ln -nsf "$BUILD_DIR/.heroku/vendor" /app/.heroku/vendor
    # Note: .heroku/src is copied in later.
fi

# Install Python.
let start=$(nowms)
# shellcheck source=bin/steps/python
source "$BIN_DIR/steps/python"
mtime "python.install.time" "${start}"

# Pipenv support.
# shellcheck source=bin/steps/pipenv
source "$BIN_DIR/steps/pipenv"

# If no requirements.txt file given, assume `setup.py develop` is intended.
if [ ! -f requirements.txt ] && [ ! -f Pipfile ]; then
  echo "-e ." > requirements.txt
fi

# Fix egg-links.
# shellcheck source=bin/steps/eggpath-fix
source "$BIN_DIR/steps/eggpath-fix"

# Mercurial support.
# shellcheck source=bin/steps/mercurial
source "$BIN_DIR/steps/mercurial"

# Pylibmc support.
# shellcheck source=bin/steps/pylibmc
source "$BIN_DIR/steps/pylibmc"

# Libffi support.
# shellcheck source=bin/steps/cryptography
source "$BIN_DIR/steps/cryptography"

# Support for Geo libraries.
# shellcheck source=bin/steps/geo-libs
sub_env "$BIN_DIR/steps/geo-libs"

# GDAL support.
# shellcheck source=bin/steps/gdal
source "$BIN_DIR/steps/gdal"

# Uninstall removed dependencies with Pip.
let start=$(nowms)
# shellcheck source=bin/steps/pip-uninstall
source "$BIN_DIR/steps/pip-uninstall"
mtime "pip.uninstall.time" "${start}"

# Install dependencies with Pip (where the magic happens).
let start=$(nowms)
# shellcheck source=bin/steps/pip-install
source "$BIN_DIR/steps/pip-install"
mtime "pip.install.time" "${start}"

# Support for NLTK corpora.
let start=$(nowms)
sub_env "$BIN_DIR/steps/nltk"
mtime "nltk.download.time" "${start}"

# Support for pip install -e.
# In CI, $BUILD_DIR is /app.
if [[ ! "$BUILD_DIR" == "/app" ]]; then
  rm -fr "$BUILD_DIR/.heroku/src"
  deep-cp /app/.heroku/src "$BUILD_DIR/.heroku/src"
fi


# Django collectstatic support.
let start=$(nowms)
sub_env "$BIN_DIR/steps/collectstatic"
mtime "collectstatic.time" "${start}"

# Create .profile script for application runtime environment variables.
set_env PATH "\$HOME/.heroku/python/bin:\$PATH"
set_env PYTHONUNBUFFERED true
set_env PYTHONHOME /app/.heroku/python

set_env LIBRARY_PATH "/app/.heroku/vendor/lib:/app/.heroku/python/lib:\$LIBRARY_PATH"
set_env LD_LIBRARY_PATH "/app/.heroku/vendor/lib:/app/.heroku/python/lib:\$LD_LIBRARY_PATH"

set_default_env LANG en_US.UTF-8
set_default_env PYTHONHASHSEED random
set_default_env PYTHONPATH /app/

# Install sane-default script for $WEB_CONCURRENCY and $FORWARDED_ALLOW_IPS.
cp "$ROOT_DIR/vendor/WEB_CONCURRENCY.sh" "$WEB_CONCURRENCY_PROFILE_PATH"
cp "$ROOT_DIR/vendor/python.gunicorn.sh" "$GUNICORN_PROFILE_PATH"


# Experimental post_compile hook.
# shellcheck source=bin/steps/hooks/post_compile
source "$BIN_DIR/steps/hooks/post_compile"

# Fix egg-links, again.
# shellcheck source=bin/steps/eggpath-fix2
source "$BIN_DIR/steps/eggpath-fix2"

# Store new artifacts in cache.

rm -rf "$CACHE_DIR/.heroku/python"
rm -rf "$CACHE_DIR/.heroku/python-version"
rm -rf "$CACHE_DIR/.heroku/python-stack"
rm -rf "$CACHE_DIR/.heroku/vendor"
rm -rf "$CACHE_DIR/.heroku/src"

mkdir -p "$CACHE_DIR/.heroku"
cp -R .heroku/python "$CACHE_DIR/.heroku/"
cp -R .heroku/python-version "$CACHE_DIR/.heroku/"
cp -R .heroku/python-stack "$CACHE_DIR/.heroku/" &> /dev/null || true
cp -R .heroku/vendor "$CACHE_DIR/.heroku/" &> /dev/null || true
if [[ -d .heroku/src ]]; then
  cp -R .heroku/src "$CACHE_DIR/.heroku/" &> /dev/null || true
fi

# Measure the size of the Python installation.
# shellcheck disable=SC2119
mmeasure 'python.size' "$(measure-size)"
