#!/bin/bash

set -eETxv --
echo $-

# If `true`, do `python setup.py install` on the untarred packages listed in
# the `$DEPS` variable below:
INSTALL_DEPS=false

# If `true`, remove the old `bridges` virtualenv and recreate it:
CREATE_NEW_VENV=true

# ------------------------------------------------------------------------------

. /srv/bridges.torproject.org/etc/environment-production

export PYTHONPATH
export PIP_CONFIG_FILE

if $CREATE_NEW_VENV ; then
    # If we're supposed to recreate the venv, delete it first.
    if test -d "$VIRTUALENV"; then rm -rf "$VIRTUALENV"; fi
    if ! test -x "$VIRTUALENV_ACTIVATE"; then
        python3 -m venv "$VIRTUALENV"
    fi
fi

# Enter the virtual environment.
source $VIRTUALENV_ACTIVATE

# Install our requirements with the venv pip in order to avoid using
# easy_install:
if $CREATE_NEW_VENV ; then
    pip3 install wheel
    cd $BRIDGEDB_SOURCE_REPO && $(which pip3) install -r requirements.txt
fi

# Install BridgeDB from our local copy of https://git.torproject.org/bridgedb.git
cd $BRIDGEDB_SOURCE_REPO && make reinstall

"$BIN_DIR"/run-bridgedb
exit $?
