#!/bin/bash

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

WARPDRIVE_SERVER_ARGS="--log-file - --bind 0.0.0.0:$WARPDRIVE_HTTP_PORT"

if [ -d .warpdrive/server_args ]; then
    WARPDRIVE_SERVER_ARGS_FILE=".warpdrive/server_args/gunicorn"
else
    WARPDRIVE_SERVER_ARGS_FILE=".warpdrive/server_args"
fi

if [ -f $WARPDRIVE_SERVER_ARGS_FILE ]; then
    WARPDRIVE_SERVER_ARGS="$WARPDRIVE_SERVER_ARGS `cat $WARPDRIVE_SERVER_ARGS_FILE`"

    # Expand any environment variable references in options.

    WARPDRIVE_TMP_FILE=/tmp/server_args.$$

    cat > $WARPDRIVE_TMP_FILE << EOF
#!/bin/sh
cat << !
$WARPDRIVE_SERVER_ARGS
!
EOF

    chmod +x $WARPDRIVE_TMP_FILE

    WARPDRIVE_SERVER_ARGS=`$WARPDRIVE_TMP_FILE | tr '\n' ' '`

    rm -f $WARPDRIVE_TMP_FILE
fi

echo " -----> Executing server command 'gunicorn $WARPDRIVE_SERVER_ARGS $@'"

exec warpdrive exec gunicorn $WARPDRIVE_SERVER_ARGS "$@"
