#!/bin/bash
set -eux

manifest=$(get-pip-manifest os-cloud-config)

virtualenv --setuptools ${OS_CLOUD_CONFIG_VENV_DIR}
set +u
source ${OS_CLOUD_CONFIG_VENV_DIR}/bin/activate
set -u

if [ -n "$manifest" ]; then
    use-pip-manifest $manifest
else
    # bug #1201253 : virtualenv-1.10.1 embeds setuptools-0.9.8, which
    # doesn't manage correctly HTTPS sockets when downloading pbr from
    # https://pypi.python.org/simple/ if using http_proxy and https_proxy
    # envvars
    ${OS_CLOUD_CONFIG_VENV_DIR}/bin/pip install -U 'setuptools>=1.0'
    # bug #1293812 : Avoid easy_install triggering on pbr.
    ${OS_CLOUD_CONFIG_VENV_DIR}/bin/pip install -U 'pbr>=0.11,<2.0'
    if [ -e /opt/stack/os-cloud-config/requirements.txt ]; then
        reqs=/opt/stack/os-cloud-config/requirements.txt
    elif [ -e /opt/stack/os-cloud-config/tools/pip-requires ]; then
        reqs=/opt/stack/os-cloud-config/tools/pip-requires
    else
        reqs=""
    fi
    if [ -n "$reqs" ] ; then
        pip install -r $reqs
    fi
fi

# All reqs are installed, install from source (dev versions are not installed from manifests)
pip install /opt/stack/os-cloud-config

# Write the manifest of what was installed
write-pip-manifest os-cloud-config

set +u
deactivate
set -u
