#!/bin/bash

set -eux

client=$1
repo=python-${client}client
client_manifest=$(get-pip-manifest ${repo})
venv_base=/opt/stack/venvs

# We would like to use --system-site-packages here but if requirements.txt
# contains libraries that are installed globally with versions that don't
# satisfy our requirements.txt, we end up using the incorrect global library.
# Because the global site-packages appears first in sys.path
# TODO : Add this back in when we are using virtualenv >= 1.11
virtualenv $venv_base/$repo
set +u
source $venv_base/$repo/bin/activate
set -u

pushd /opt/stack/$repo
if [ -n "$client_manifest" ]; then
  use-pip-manifest $client_manifest
else
  # Need setuptools>=1.0 to manage connections when
  # downloading from pypi using http_proxy and https_proxy
  pip install -U 'setuptools>=1.0'

  # bug #1293812 : Avoid easy_install triggering on pbr.
  pip install -U 'pbr>=0.5.21,<1.0'

  if [ -e requirements.txt ]; then
      pip install -r requirements.txt
  elif [ -e tools/pip-requires ]; then
      pip install -r tools/pip-requires
  fi
fi

# Always replay this, as we cannot use the entry this would generate in the manifest
python setup.py develop

# Write the manifest of what was installed
write-pip-manifest $repo

ln -s $venv_base/$repo/bin/$client /usr/local/bin/$client
popd

set +u
deactivate
set -u
