#!/bin/bash

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

# Directory for the application.

WARPDRIVE_APPL_DIR=${WARPDRIVE_APPL_DIR:-/opt/warpdrive}
export WARPDRIVE_APPL_DIR

echo "export WARPDRIVE_APPL_DIR=$WARPDRIVE_APPL_DIR"

# Home directory for the user and where any additional generated files
# will be placed.

WARPDRIVE_HOME_DIR=${WARPDRIVE_HOME_DIR:=/home/warpdrive}
export WARPDRIVE_HOME_DIR

echo "export WARPDRIVE_HOME_DIR=$WARPDRIVE_HOME_DIR"

# Override HOME directory for the user, in case it isn't set to a
# sensible value.

HOME=$WARPDRIVE_HOME_DIR
export HOME

echo "export HOME=$HOME"

# 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'.

WARPDRIVE_USER_ID=$(id -u)

NSS_WRAPPER_PASSWD=$WARPDRIVE_HOME_DIR/passwd
NSS_WRAPPER_GROUP=/etc/group

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/^warpdrive:/builder:/' > $NSS_WRAPPER_PASSWD

        echo "warpdrive:x:$WARPDRIVE_USER_ID:0:Warp Drive,,,:$WARPDRIVE_HOME_DIR:/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=/usr/local/nss_wrapper/lib64/libnss_wrapper.so"
fi

# Copy environment variable configuration to the system directory used
# for runtime files. This is done so that the '.warpdrive/deploy-env' can
# safely add or remove files without modifying the original source code
# directory. This is necessary as the original source code directory
# may not be writable, or could be a mounted directory and we do not
# want to modify any original outside of Docker.

WARPDRIVE_USER_VARS=$WARPDRIVE_HOME_DIR/user_vars.$$
export WARPDRIVE_USER_VARS

mkdir -p $WARPDRIVE_USER_VARS

if [ -d $WARPDRIVE_APPL_DIR/.warpdrive/user_vars ]; then
    for name in `ls $WARPDRIVE_APPL_DIR/.warpdrive/user_vars/*`; do
        cp $name $WARPDRIVE_USER_VARS
    done
fi

# Docker will have set any environment variables defined in the image or
# on the command line when the container has been run. Here we are going
# to look for any statically defined environment variables provided by
# the user as part of the actual application. These will have been
# placed in the '.warpdrive/user_vars' directory. The name of the file
# corresponds to the name of the environment variable and the contents
# of the file the value to set the environment variable to. Each of the
# environment variables is set and exported.

envvars=

for name in `ls $WARPDRIVE_USER_VARS`; do
    export $name="`cat $WARPDRIVE_USER_VARS/$name`"
    envvars="$envvars $name"
done

# Run any user supplied script to be run to set, modify or delete the
# environment variables.

if [ -f $WARPDRIVE_APPL_DIR/.warpdrive/action_hooks/deploy-env ]; then
    if [ ! -x $WARPDRIVE_APPL_DIR/.warpdrive/action_hooks/deploy-env ]; then
        echo "# WARNING: Script $WARPDRIVE_APPL_DIR/.warpdrive/action_hooks/deploy-env not executable."
    fi
fi

if [ -x $WARPDRIVE_APPL_DIR/.warpdrive/action_hooks/deploy-env ]; then
    echo "# INFO: Running script $WARPDRIVE_APPL_DIR/.warpdrive/action_hooks/deploy-env"
    $WARPDRIVE_APPL_DIR/.warpdrive/action_hooks/deploy-env
fi

# Go back and output the final values of the user supplied environment
# variables.

for name in `ls $WARPDRIVE_USER_VARS`; do
    echo "export $name=\"`cat $WARPDRIVE_USER_VARS/$name`\""
done

# Clean up the environment variables directory.

rm -rf $WARPDRIVE_USER_VARS

# Output how environment variables should be incorporated.

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