#! /bin/bash

set -ex

VERSIONS_TO_INSTALL=${1}
if [[ ! -n ${VERSIONS_TO_INSTALL} ]]; then
  VERSIONS_TO_INSTALL=${PYTHON_VERSIONS}
fi
if [[ ! -n ${VERSIONS_TO_INSTALL} ]]; then
  echo "Either PYTHON_VERSIONS must be set or provide and argument containing required versions"
  exit 1
fi

IFS=',' read -r -a array <<< ${VERSIONS_TO_INSTALL}
for python_version in "${array[@]}"; do
  echo "###### Installing Python ${python_version} ######"
  apt-get -y install python${python_version} python${python_version}-dev

  echo "###### Creating Virtualenv for Python ${python_version} ######"
  virtualenv -p python${python_version} /envs/h2o_env_python${python_version}
  chmod a+w -R /envs/h2o_env_python${python_version}

  echo "###### Installing dependencies for Python ${python_version} ######"
  source /envs/h2o_env_python${python_version}/bin/activate
  pip install pip wheel --upgrade
  deactivate
done
