#!/bin/sh
# NOTE: This script should be sourced! The shebang is only here to help syntax highlighters.

# # conda-build
# pip install --no-deps -U .

devenv="${1:-./devenv}"
pyver="${2:-3}"
_CONDA="$devenv/bin/conda"

if ! [ -f "$devenv/conda-meta/history" ]; then
    if [ "$(uname)" = Darwin ]; then
        if [ ! -f miniconda${pyver}.sh ]; then
            curl https://repo.anaconda.com/miniconda/Miniconda${pyver}-latest-MacOSX-x86_64.sh -o miniconda${pyver}.sh
        fi
        bash miniconda${pyver}.sh -bfp "$devenv"
        "${_CONDA}" install -yq -p $devenv python=$pyver --file dev/test-requirements.txt -c defaults
    elif [ "$(uname)" = Linux ]; then
        if [ ! -f miniconda${pyver}.sh ]; then
            curl https://repo.anaconda.com/miniconda/Miniconda${pyver}-latest-Linux-x86_64.sh -o miniconda${pyver}.sh
        fi
        bash miniconda${pyver}.sh -bfp "$devenv"
        "${_CONDA}" install -yq -p $devenv python=$pyver --file dev/test-requirements.txt -c defaults
        "${_CONDA}" install -yq -p $devenv patchelf  # for conda-build
    else
        if [ ! -f miniconda${pyver}.exe ]; then
            powershell.exe -Command "(new-object System.Net.WebClient).DownloadFile('https://repo.anaconda.com/miniconda/Miniconda$pyver-latest-Windows-x86_64.exe','miniconda${pyver}.exe')"
        fi
        cmd.exe /c "start /wait \"\" miniconda${pyver}.exe /InstallationType=JustMe /RegisterPython=0 /AddToPath=0 /S /D=%CD%\$(cygpath -w $devenv)"
        _CONDA="$devenv/Scripts/conda"
    fi
fi


if ! [ -f "$devenv/.dev-start-ed" ]; then
  "${_CONDA}" install -yq -p $devenv python=$pyver --file dev/test-requirements.txt -c defaults
  touch "$devenv/.dev-start-ed"
fi


case "$(uname)" in
    Darwin|Linux)  eval "$($devenv/bin/python -m conda init --dev bash)" ;;
               *)  eval "$($devenv/python -m conda init --dev bash)" ;;
esac
