#!/bin/bash
# We need to install this early in install.d because other elements will
# need to use os-refresh-config --print-base to know where to put files

set -eux

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

# pip and virtualenv is installed by the pip-and-virtualenv element

virtualenv --setuptools /opt/stack/venvs/os-refresh-config
set +u
source /opt/stack/venvs/os-refresh-config/bin/activate
set -u

if [ -n "$manifest" ]; then
  use-pip-manifest $manifest
else
  # Need setuptools>=1.0 to manage connections when
  # downloading from pypi using http_proxy and https_proxy
  /opt/stack/venvs/os-refresh-config/bin/pip install -U pip
  /opt/stack/venvs/os-refresh-config/bin/pip install -U 'setuptools>=1.0'
  # bug #1293812 : Avoid easy_install triggering on pbr.
  /opt/stack/venvs/os-refresh-config/bin/pip install -U 'pbr>=0.5.21,<1.0'
  /opt/stack/venvs/os-refresh-config/bin/pip install -U os-refresh-config
fi

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

ln -s /opt/stack/venvs/os-refresh-config/bin/os-refresh-config /usr/local/bin/os-refresh-config

for d in pre-configure.d configure.d migration.d post-configure.d; do
  install -m 0755 -o root -g root -d /opt/stack/os-config-refresh/$d
done

set +u
deactivate
set -u
