#!/usr/bin/env bash
set -euxo pipefail

# pip version to install is passed from setup.py
pip_version="$1"

# bootstrap ansible

# Install PPA prerequisites (gnupg-curl is required for hkps protocol to fetch PPA key)
sudo apt-get update
sudo apt-get install --assume-yes --no-install-recommends \
    aptitude \
    gnupg-curl \
    libffi-dev \
    libssl-dev \
    sshpass

# Add deadsnakes PPA for Python 3.6
sudo apt-key adv --no-tty --keyserver hkps://keyserver.ubuntu.com:443 --recv-key BA6932366A755776
sudo sh -c 'echo "deb [arch=amd64] http://apt-mirror.zooxlabs.com/mirror/ppa.launchpad.net/deadsnakes/ppa/ubuntu trusty main" > /etc/apt/sources.list.d/deadsnakes.list'

# Install python3.6
sudo apt-get update
sudo apt-get install --assume-yes --no-install-recommends \
    python3.6-dev

# don't use pip from ubuntu
sudo apt-get remove python-pip python3-pip --assume-yes

# Must uninstall the old ansible so the old setup script will detect that the
# correct version of ansible is not installed and will restore that version.
if pip2.7 show ansible -q; then
    sudo --set-home pip2.7 uninstall ansible --yes
fi

# Install pip for python3.6.
sudo --set-home python3.6 scripts/setup/get-pip.py "pip==${pip_version}" --force-reinstall

sudo patch -p2 -d /usr/local/lib/python3.6/dist-packages/pip < scripts/setup/pip_9.0.1.diff

# Install ansible and colorama using `requirements3.txt` file.
# Note: `requirements3.txt` file is created by installing specific versions of
# ansible and colorma packages in a new virtualenv and calling the pip freeze
# to get the list.
sudo --set-home pip3.6 install --requirement "${ZOOX_WORKSPACE_ROOT}/scripts/setup/requirements3.txt" --ignore-installed
