#!/usr/bin/env bash

# This script is for setting up environment in which unit test is ran.
# To speed up the CI time, the resulting environment is cached.
#
# Do not install PyTorch and torchaudio here, otherwise they also get cached.

set -ex

root_dir="$(git rev-parse --show-toplevel)"
conda_dir="${root_dir}/conda"
env_dir="${root_dir}/env"

cd "${root_dir}"

case "$(uname -s)" in
    Darwin*) os=MacOSX;;
    *) os=Linux
esac

# 1. Install conda at ./conda
if [ ! -d "${conda_dir}" ]; then
    printf "* Installing conda\n"
    curl --silent -L -o miniconda.sh "http://repo.continuum.io/miniconda/Miniconda3-latest-${os}-x86_64.sh"
    bash ./miniconda.sh -b -f -p "${conda_dir}"
    eval "$("${conda_dir}/bin/conda" shell.bash hook)"
    printf "* Updating the base Python version to %s\n" "${PYTHON_VERSION}"

    ADDITIONAL_CHANNELS=""
    if [[ ${PYTHON_VERSION} == 3.10 ]]; then
        ADDITIONAL_CHANNELS="-c conda-forge"
    fi

    # Need to disable shell check since this'll fail out if ADDITIONAL_CHANNELS is empty
    # shellcheck disable=SC2086
    conda install ${ADDITIONAL_CHANNELS} --quiet -y python="${PYTHON_VERSION}"
else
    eval "$("${conda_dir}/bin/conda" shell.bash hook)"
fi


# 2. Create test environment at ./env
if [ ! -d "${env_dir}" ]; then
    printf "* Creating a test environment with PYTHON_VERSION=%s\n" "${PYTHON_VERSION}\n"
    conda create --prefix "${env_dir}" -y python="${PYTHON_VERSION}"
fi
conda activate "${env_dir}"

# 3. Install minimal build tools
pip --quiet install cmake ninja
conda install --quiet -y 'ffmpeg>=4.1' pkg-config
