#!/bin/bash

if [ x"$WARPDRIVE_DEBUG" != x"" ]; then
    set -x
fi

# This is the script used to generate a set of Python wheels for any
# nominated packages.

# Ensure that any failure within this script or a user provided script
# causes this script to fail immediately. This eliminates the need to
# check individual statuses for anything which is run and prematurely
# exit. Note that the feature of bash to exit in this way isn't
# foolproof. Ensure that you heed any advice in:
#
#   http://mywiki.wooledge.org/BashFAQ/105
#   http://fvue.nl/wiki/Bash:_Error_handling
#
# and use best practices to ensure that failures are always detected.
# Any user supplied scripts should also use this failure mode.

set -eo pipefail

# Root directory for the runtime environment of the application.

WARPDRIVE_APP_ROOT=${WARPDRIVE_APP_ROOT:-/opt/app-root}

# Root directory for the source code of the application.

WARPDRIVE_SRC_ROOT=${WARPDRIVE_SRC_ROOT:-$WARPDRIVE_APP_ROOT/src}

# Make sure we are in the correct working directory for the application.

cd $WARPDRIVE_SRC_ROOT

# Activate the Python virtual environment if one has been created.

if [ -f $WARPDRIVE_APP_ROOT/bin/activate ]; then
    . $WARPDRIVE_APP_ROOT/bin/activate
fi

# Run any user supplied script to be run prior to installing application
# dependencies. This is to allow additional system packages to be
# installed that may be required by any Python modules which are being
# installed. The script must be executable in order to be run. It is not
# possible for this script to change the permissions so it is executable
# and then run it, due to some docker bug which results in the text file
# being busy. For more details see:
#
#   https://github.com/docker/docker/issues/9547

if [ -f ${WARPDRIVE_SRC_ROOT}/.warpdrive/action_hooks/install ]; then
    if [ ! -x ${WARPDRIVE_SRC_ROOT}/.warpdrive/action_hooks/install ]; then
        echo "WARNING: Script ${WARPDRIVE_SRC_ROOT}/.warpdrive/action_hooks/install not executable."
    fi
fi

if [ -x ${WARPDRIVE_SRC_ROOT}/.warpdrive/action_hooks/install ]; then
    echo " -----> Running ${WARPDRIVE_SRC_ROOT}/.warpdrive/action_hooks/install"
    ${WARPDRIVE_SRC_ROOT}/.warpdrive/action_hooks/install
fi

# Create and populate the wheelhouse directory.

WARPDRIVE_PIP_PACKAGES=${WARPDRIVE_PIP_PACKAGES:-requirements.txt}
WARPDRIVE_PIP_WHEELS=${WARPDRIVE_PIP_WHEELS:-$WARPDRIVE_PIP_PACKAGES}

WARPDRIVE_WHEEL_DIR=${WARPDRIVE_WHEEL_DIR:-$WARPDRIVE_SRC_ROOT/.warpdrive/wheelhouse}

WARPDRIVE_BLD_ROOT=${WARPDRIVE_BLD_ROOT:-/tmp/warpdrive-wheelhouse.$$}

mkdir -p $WARPDRIVE_WHEEL_DIR

WARPDRIVE_PIP_OPTIONS=

if [ "$WARPDRIVE_BUILD_TARGET" = "wheelhouse" ]; then
    WARPDRIVE_PIP_OPTIONS="--cache-dir $WARPDRIVE_SRC_ROOT/.warpdrive/packages"
fi

for filename in $WARPDRIVE_PIP_WHEELS; do
    if [ -f $filename ]; then
        echo " -----> Installing dependencies as wheels with pip ($filename)"
        pip wheel $WARPDRIVE_PIP_OPTIONS --wheel-dir $WARPDRIVE_WHEEL_DIR \
            --exists-action=w --src=$WARPDRIVE_BLD_ROOT -r $filename
    fi
done

# Clean up any temporary files, including the results of checking out
# any source code repositories when doing a 'pip install' from a VCS.

rm -rf $WARPDRIVE_BLD_ROOT
