#!/bin/bash

#  Source in the utility functions.
source "${OPENSHIFT_PYTHON_DIR}/versions/${OPENSHIFT_PYTHON_VERSION}/lib/utils"

# Run when jenkins is not being used or run when inside a build.
clean_build_marker=".openshift/markers/force_clean_build"
if [ -f "${OPENSHIFT_REPO_DIR}$clean_build_marker" ]; then
   echo "  - Found a $clean_build_marker marker - recreating virtenv ..." 1>&2
   delete_virtualenv
fi

#  Create (or update if exists) and activate the virtualenv.
create_virtualenv
activate_virtualenv
echo "  - Created/Updated virtenv and activated it."

if [ -f "${OPENSHIFT_REPO_DIR}setup.py" ]; then
   echo " - Found setup.py. Processing it ..."

   if [ -n "$OPENSHIFT_PYPI_MIRROR_URL" ]; then
      m=$OPENSHIFT_PYPI_MIRROR_URL
      if curl -m 15 -f -s "$m" &>/dev/null
      then
          mirror_opts="-i $m"
      else
         echo "  - PyPi mirror is not available."
      fi

   fi

   pushd "$OPENSHIFT_REPO_DIR" > /dev/null
   python "${OPENSHIFT_REPO_DIR}setup.py" develop $mirror_opts
   echo " - Done processing setup.py"
   popd  > /dev/null
fi

#  Fix the scripts + .pth files in the virtualenv - make it relocatable.
make_virtualenv_relocatable
