#!/bin/bash

# This script outputs the environment for the application or shell.

WARPDRIVE_PHASE=deploy
export WARPDRIVE_PHASE

echo "export WARPDRIVE_PHASE=$WARPDRIVE_PHASE"

# Root directory for the runtime environment of the application.

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

echo "export WARPDRIVE_APP_ROOT=$WARPDRIVE_APP_ROOT"

# Root directory for the source code of the application.

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

echo "export WARPDRIVE_SRC_ROOT=$WARPDRIVE_SRC_ROOT"

# Home directory of the project within the source code.

if [ -f ${WARPDRIVE_SRC_ROOT}/.warpdrive/project_root ]; then
    WARPDRIVE_PRJ_ROOT="`cat ${WARPDRIVE_SRC_ROOT}/.warpdrive/project_root`"
fi

case "$WARPDRIVE_PRJ_ROOT" in
    /*)
        ;;
    "")
        WARPDRIVE_PRJ_ROOT=$WARPDRIVE_SRC_ROOT
        ;;
    *)
        WARPDRIVE_PRJ_ROOT=$WARPDRIVE_SRC_ROOT/$WARPDRIVE_PRJ_ROOT
        ;;
esac

export WARPDRIVE_PRJ_ROOT

echo "export WARPDRIVE_PRJ_ROOT=$WARPDRIVE_PRJ_ROOT"

# Set the default listener port for the web application server.

WARPDRIVE_HTTP_PORT=${WARPDRIVE_HTTP_PORT:-8080}
export WARPDRIVE_HTTP_PORT

echo "export WARPDRIVE_HTTP_PORT=$WARPDRIVE_HTTP_PORT"

# Override uid and gid lookup to cope with being randomly assigned IDs
# using the -u option to 'docker run'.
#
# For now we base whether we can run this or not on the existence of
# libnss_wrapper.so file. This really needs to simple be gated on whether
# running inside of Docker which is where it needs to be done.

WARPDRIVE_USER_ID=$(id -u)

NSS_WRAPPER_PASSWD=$WARPDRIVE_APP_ROOT/tmp/passwd
NSS_WRAPPER_GROUP=/etc/group

NSS_WRAPPER_LIBRARY=

if [ -f /usr/local/nss_wrapper/lib64/libnss_wrapper.so ]; then
    NSS_WRAPPER_LIBRARY=/usr/local/nss_wrapper/lib64/libnss_wrapper.so
else
    if [ -f /lib64/libnss_wrapper.so ]; then
        NSS_WRAPPER_LIBRARY=/lib64/libnss_wrapper.so
    fi
fi

if [ "$NSS_WRAPPER_LIBRARY" != "" ]; then
    if [ x"$WARPDRIVE_USER_ID" != x"0" -a x"$WARPDRIVE_USER_ID" != x"1001" ]; then
        if [ ! -f $NSS_WRAPPER_PASSWD ]; then
            cat /etc/passwd | sed -e 's/^default:/builder:/' > $NSS_WRAPPER_PASSWD

            echo "default:x:$WARPDRIVE_USER_ID:0:Warp Drive,,,:$HOME:/bin/bash" >> $NSS_WRAPPER_PASSWD
        fi

        echo "export NSS_WRAPPER_PASSWD=$NSS_WRAPPER_PASSWD"
        echo "export NSS_WRAPPER_GROUP=$NSS_WRAPPER_GROUP"

        echo "export LD_PRELOAD=$NSS_WRAPPER_LIBRARY"
    fi
fi

# Ensure that the source directory and project home directory are in
# the Python module search path and that they are searched first.

PYTHONPATH=$WARPDRIVE_SRC_ROOT:$PYTHONPATH

if [ "$WARPDRIVE_PRJ_ROOT" != "$WARPDRIVE_SRC_ROOT" ]; then
    PYTHONPATH=$WARPDRIVE_PRJ_ROOT:$PYTHONPATH
fi

echo "export PYTHONPATH=$PYTHONPATH"

# Add the bin directory for the Python virtual environment if it exists.

if [ -f $WARPDRIVE_APP_ROOT/bin/activate ]; then
    PATH=$WARPDRIVE_APP_ROOT/bin:$PATH
fi

echo "export PATH=$PATH"

# Now source environment variables. First evaluate common environment
# variables from '.warpdrive/environment' if it exists. Then source
# variables from '.warpdrive/action_hooks/deploy-env' if it exists. Any
# variables set in either script will be automatically exported. It is
# possible to use script logic, but temporary variables would need to be
# cleaned up manually due to variables set being automatically exported.

if [ -f $WARPDRIVE_SRC_ROOT/.warpdrive/environment ]; then
    set -a; . $WARPDRIVE_SRC_ROOT/.warpdrive/environment; set +a
fi

if [ -f $WARPDRIVE_SRC_ROOT/.warpdrive/action_hooks/deploy-env ]; then
    set -a; . $WARPDRIVE_SRC_ROOT/.warpdrive/action_hooks/deploy-env; set +a
fi

# Output how environment variables should be incorporated.

echo '# Run this command to configure your shell:'
echo '# eval "$(warpdrive env)"'
