#!/usr/bin/env sh
#
# Install each required version of Python (from the .python-version file),
# if it's not installed already.
#
# Also install tox in each pyenv copy of Python, if not installed already.
#
# Requirements
# ============
#
# * pyenv (https://github.com/pyenv/pyenv) to install versions of Python.
#
# Usage
# =====
#
# $ ./bin/install-python

# Exit if we're running on GitHub Actions.
# On GitHub Actions we just want to use the versions of Python provided in the
# GitHub Actions VM.
if [ "$GITHUB_ACTIONS" = "true" ]
then
  exit
fi

# Loop over every $python_version in the .python-version file.
while IFS= read -r python_version
do
    # Install this version of Python in pyenv if it's not installed already.
    pyenv install --skip-existing "$python_version"

    # Install tox in this version of Python if it's not already installed.
    if ! "$(pyenv root)/versions/$python_version/bin/tox" --version > /dev/null 2>&1
    then
        "$(pyenv root)/versions/$python_version/bin/pip" install --quiet --disable-pip-version-check 'tox<4' > /dev/null
        pyenv rehash
    fi
done < .python-version
