#!/bin/bash

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

# Setup the environment if not already done.

if [ x"$WARPDRIVE_ACTION" = x"" ]; then
    eval "$(warpdrive env)"
fi

WARPDRIVE_ACTION=setup
export WARPDRIVE_ACTION

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

cd $WARPDRIVE_SRC_ROOT

# Run any user supplied script to be run to update configuration files
# based on environment variables available at deployment time.

if [ ! -f $WARPDRIVE_APP_ROOT/markers/deploy-cfg ]; then
    # Create the marker file so we know if we have been run already.

    mkdir -p $WARPDRIVE_APP_ROOT/markers
    date > $WARPDRIVE_APP_ROOT/markers/deploy-cfg

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

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

# Determine whether we have been told that we are running a specific web
# application server type. If we haven't, we will try and automatically
# determine how the server should be started and what WSGI server to use.

if [ -f ${WARPDRIVE_SRC_ROOT}/.warpdrive/server_type ]; then
    WARPDRIVE_SERVER_TYPE="`cat ${WARPDRIVE_SRC_ROOT}/.warpdrive/server_type`"
else
    WARPDRIVE_SERVER_TYPE="auto"
fi

WARPDRIVE_WSGI_SERVER=${WARPDRIVE_WSGI_SERVER:-mod_wsgi}

case $WARPDRIVE_SERVER_TYPE in
    auto+gunicorn|gunicorn)
        WARPDRIVE_SERVER_TYPE=auto
        WARPDRIVE_WSGI_SERVER=gunicorn
        ;;
    auto+mod_wsgi|mod_wsgi)
        WARPDRIVE_SERVER_TYPE=auto
        WARPDRIVE_WSGI_SERVER=mod_wsgi
        ;;
    auto+uwsgi|uwsgi)
        WARPDRIVE_SERVER_TYPE=auto
        WARPDRIVE_WSGI_SERVER=uwsgi
        ;;
    auto+waitress|waitress)
        WARPDRIVE_SERVER_TYPE=auto
        WARPDRIVE_WSGI_SERVER=waitress
        ;;
esac

# If we are automatically detect the server type and we find a Django
# application, trigger database migration.

if [ "$WARPDRIVE_SERVER_TYPE" = "auto" -o "$WARPDRIVE_SERVER_TYPE" = "django" ]; then
    if [ -f manage.py ]; then
        if grep -q DJANGO_SETTINGS_MODULE manage.py; then
            echo " -----> Running Django database migration"
            python manage.py migrate

            if (tty > /dev/null 2>&1); then
                echo " -----> Running Django super user creation"
                python manage.py createsuperuser
            fi
        fi
    fi
fi

# Run any user supplied script to be run prior to starting the
# application in the actual container. 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/action_hooks/setup ]; then
    if [ ! -x .warpdrive/action_hooks/setup ]; then
        echo "WARNING: Script .warpdrive/action_hooks/setup not executable."
    fi
fi

if [ -x .warpdrive/action_hooks/setup ]; then
    echo " -----> Running .warpdrive/action_hooks/setup"
    .warpdrive/action_hooks/setup
fi
