#!/usr/bin/env bash

set -exu

usage()
{
    echo "Usage: prep-for-macos-build [python2|python3]"
}

pyver="${1:-python2}"

if ! command -v brew; then
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

brew install par2 readline rsync pkg-config md5sha1sum

# This avoid's macos interference, i.e. without this it looks like we
# won't actually be able to use the readline we just installed above.
brew link --force readline
# "brew unlink readline" will undo this hack

case "$pyver" in
    python2)
        brew install pyenv
        pyenv install 2.7.18
        pyenv global 2.7.18
        eval "$(pyenv init -)"
        pyenv exec python -V
        pyenv exec pip install --user pathlib pytest pytest-xdist
        ;;
    python3)
        brew install python
        pip3 install --user pytest pytest-xdist
        ;;
    *)
        usage 1>&2
        exit 2
        ;;
esac
