#!/usr/bin/env bash

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


# Fail fast and fail hard.
set -eo pipefail

# 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

CACHED_DIRS=".heroku"

# Static configurations for virtualenv caches.
VIRTUALENV_LOC=".heroku/venv"
LEGACY_TRIGGER="lib/python2.7"
PROFILE_PATH="$BUILD_DIR/.profile.d/python.sh"

# Python version. This will be used in the future to specify custom Pythons.
DEFAULT_PYTHON_VERSION="python-2.7.3"
PYTHON_EXE="/app/.heroku/python/bin/python"

# Sanitizing environment variables.
unset GIT_DIR PYTHONHOME PYTHONPATH LD_LIBRARY_PATH LIBRARY_PATH

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

# Syntax sugar.
source $BIN_DIR/utils

# Directory Hacks for path consistiency.
APP_DIR='/app'
TMP_APP_DIR=$CACHE_DIR/tmp_app_dir

# Copy Anvil app dir to temporary storage...
mkdir -p $TMP_APP_DIR
deep-mv $APP_DIR $TMP_APP_DIR

# Copy Application code in.
deep-mv $BUILD_DIR $APP_DIR

# Set new context.
ORIG_BUILD_DIR=$BUILD_DIR
BUILD_DIR=$APP_DIR

# Prepend proper path buildpack use.
export PATH=$BUILD_DIR/.heroku/python/bin:$PATH
export PYTHONUNBUFFERED=1
export LANG=en_US.UTF-8
export LIBRARY_PATH=/app/.heroku/vendor/lib
export LD_LIBRARY_PATH=/app/.heroku/vendor/lib

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

# Experimental pre_compile hook.
source $BIN_DIR/steps/hooks/pre_compile

# If no requirements given, assume `setup.py develop`.
if [ ! -f requirements.txt ]; then
  puts-step "No requirements.txt provided; assuming dist package."
  echo "-e ." > requirements.txt
fi

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

# ### The Cache
mkdir -p $CACHE_DIR

# Purge "old-style" virtualenvs.
[ -d $CACHE_DIR/$LEGACY_TRIGGER ] && rm -fr $CACHE_DIR/.heroku/bin $CACHE_DIR/.heroku/lib $CACHE_DIR/.heroku/include
[ -d $CACHE_DIR/$VIRTUALENV_LOC ] && rm -fr $CACHE_DIR/.heroku/venv $CACHE_DIR/.heroku/src

# Restore old artifacts from the cache.
for dir in $CACHED_DIRS; do
  cp -R $CACHE_DIR/$dir . &> /dev/null || true
done

set +e
# Create set-aside `.heroku` folder.
mkdir .heroku &> /dev/null
set -e

mkdir -p $(dirname $PROFILE_PATH)

set +e
PYTHON_VERSION=$(cat runtime.txt)

# Install Python.
if [ -f .heroku/python-version ]; then
  if [ ! $(cat .heroku/python-version) = $PYTHON_VERSION ]; then
    puts-step "Found $(cat .heroku/python-version), removing."
    rm -fr .heroku/python
  else
    SKIP_INSTALL=1
  fi
fi


if [ ! "$SKIP_INSTALL" ]; then
  puts-step "Preparing Python runtime ($PYTHON_VERSION)"
  curl http://envy-versions.s3.amazonaws.com/$PYTHON_VERSION.tar.bz2 -s | tar jx &> /dev/null
  if [[ $? != 0 ]] ; then
    puts-warn "Requested runtime ($PYTHON_VERSION) was not found."
    puts-warn "Aborting.  More info: https://devcenter.heroku.com/articles/python-support"
    exit 1
  fi
  mv python .heroku/python

  # Record for future reference.
  echo $PYTHON_VERSION > .heroku/python-version
  WORKING_DIR=$(pwd)

  # Prepare it for the real world
  puts-step "Installing Distribute (0.6.34)"
  cd $ROOT_DIR/vendor/distribute-0.6.34/
  python setup.py install  &> /dev/null
  cd $WORKING_DIR

  puts-step "Installing Pip (1.2.1)"
  cd $ROOT_DIR/vendor/pip-1.2.1/
  python setup.py install  &> /dev/null
  cd $WORKING_DIR

  hash -r
else
  puts-step "Using Python runtime ($PYTHON_VERSION)"
fi

set -e

# Pylibmc support.
# See [`bin/steps/pylibmc`](pylibmc.html).
source $BIN_DIR/steps/pylibmc

# Install Mercurial if it appears to be required.
if (grep -Fiq "hg+" requirements.txt) then
  pip install --use-mirrors mercurial | cleanup | indent
fi

# Install dependencies with Pip.
puts-step "Installing dependencies using Pip (1.2.1)"
pip install --use-mirrors -r requirements.txt --exists-action=w --src=./.heroku/src | cleanup | indent

# Django collectstatic support.
source $BIN_DIR/steps/collectstatic

# ### Finalize
#

# Store new artifacts in cache.
for dir in $CACHED_DIRS; do
  rm -rf $CACHE_DIR/$dir
  cp -R $dir $CACHE_DIR/
done

# Set context environment variables.
set-env PATH '$HOME/.heroku/python/bin:$PATH'
set-env PYTHONUNBUFFERED true
set-env PYTHONHOME /app/.heroku/python
set-default-env LIBRARY_PATH /app/.heroku/vendor/lib
set-default-env LD_LIBRARY_PATH /app/.heroku/vendor/lib
set-default-env LANG en_US.UTF-8
set-default-env PYTHONHASHSEED random
set-default-env PYTHONPATH /app/


# Experimental post_compile hook.
source $BIN_DIR/steps/hooks/post_compile

# ### Fin.

deep-mv $BUILD_DIR $ORIG_BUILD_DIR
deep-mv $TMP_APP_DIR $APP_DIR
