#!/usr/bin/env bash

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

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

SECURITY_UPDATE="Python has released a security update! Please consider upgrading to"

# check if runtime exists
if curl --output /dev/null --silent --head --fail "$VENDORED_PYTHON"; then
  if [[ "$PYTHON_VERSION" == $PY37* ]]; then
    # do things to alert the user of security release available
    if [ "$PYTHON_VERSION" != "$LATEST_37" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_37"
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
  if [[ "$PYTHON_VERSION" == $PY36* ]]; then
    # security update note
    if [ "$PYTHON_VERSION" != "$LATEST_36" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_36"
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
  if [[ "$PYTHON_VERSION" == $PY35* ]]; then
    # security update note
    if [ "$PYTHON_VERSION" != "$LATEST_35" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_35"
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
  if [[ "$PYTHON_VERSION" == $PY34* ]]; then
    # security update note
    if [ "$PYTHON_VERSION" != "$LATEST_34" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_34"
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
  if [[ "$PYTHON_VERSION" == $PY27* ]]; then
    # security update note
    if [ "$PYTHON_VERSION" != "$LATEST_27" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_27"
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
else
  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

if [[ "$STACK" != "$CACHED_PYTHON_STACK" ]]; then
    puts-step "Stack has changed from $CACHED_PYTHON_STACK to $STACK, clearing cache"
    rm -fr .heroku/python-stack .heroku/python-version .heroku/python .heroku/vendor .heroku/python .heroku/python-sqlite3-version
fi

# need to clear the cache for first time installing SQLite3,
# since the version is changing and could lead to runtime errors
# with compiled extensions.
if [ -d .heroku/python ] && [ ! -f .heroku/python-sqlite3-version ] && python_sqlite3_check "$PYTHON_VERSION"; then
  puts-step "Need to update SQLite3, clearing cache"
  rm -fr .heroku/python-stack .heroku/python-version .heroku/python .heroku/vendor
fi

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 "Installing $PYTHON_VERSION"

    # Prepare destination directory.
    mkdir -p .heroku/python

    mcount "version.python.$PYTHON_VERSION"

    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

  # Record for future reference.
  echo "$PYTHON_VERSION" > .heroku/python-version
  echo "$STACK" > .heroku/python-stack
  FRESH_PYTHON=true

  hash -r
fi


# If Pip isn't up to date:
if [ "$FRESH_PYTHON" ] || [[ ! $(pip --version) == *$PIP_UPDATE* ]]; then

  puts-step "Installing pip"

  # Remove old installations.
  rm -fr /app/.heroku/python/lib/python*/site-packages/pip-*
  rm -fr /app/.heroku/python/lib/python*/site-packages/setuptools-*

  /app/.heroku/python/bin/python "$ROOT_DIR/vendor/get-pip.py" pip=="$PIP_UPDATE" &> /dev/null
  /app/.heroku/python/bin/pip install "$ROOT_DIR/vendor/setuptools-39.0.1-py2.py3-none-any.whl" &> /dev/null

fi

set -e
hash -r
