#!/bin/bash
#
# This script sets up a Kokoro MacOS worker for running Protobuf tests

set -eux

export HOMEBREW_PREFIX=$(brew --prefix)

##
# Remove any pre-existing protobuf installation.
brew uninstall protobuf

##
# Select Xcode version

##
# Select Xcode version
export DEVELOPER_DIR=/Applications/Xcode_14.app/Contents/Developer
sudo xcode-select -s "${DEVELOPER_DIR}"

##
# Select C/C++ compilers

export CC=gcc
export CXX=g++

##
# Install Python 2 by default

eval "$(pyenv init -)"
pyenv install -v -s 2.7.18 && pyenv global 2.7.18

##
# Install Tox

if [[ "${KOKORO_INSTALL_TOX:-}" == "yes" ]] ; then
  pyenv install -v -s 3.7.13
  pyenv global 3.7.13
  sudo python -m pip install --upgrade pip 'tox==3.27.1' tox-pyenv
fi

##
# Setup RVM
if [[ "${KOKORO_INSTALL_RVM:-}" == "yes" ]] ; then
    git config --global --add safe.directory $HOMEBREW_PREFIX/Library/Taps/homebrew/homebrew-cask
    git config --global --add safe.directory $HOMEBREW_PREFIX/Library/Taps/homebrew/homebrew-core
    git config --global --add safe.directory $HOMEBREW_PREFIX/Library/Taps/homebrew/homebrew-services
    sudo chown -R $(whoami) $HOME/.rvm/
fi
