#!/usr/bin/env bash
set -euo pipefail

APP_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Run with sudo if not root
sudo () {
  [[ $EUID = 0 ]] || set -- command sudo "$@"
  "$@"
}

# if needed, install uuid
if ! which uuid ; then
  echo "installing uuid"
  sudo apt-get install --assume-yes  uuid
fi

# if needed, install virtualenv
if ! virtualenv --version ; then
  sudo apt-get install -y python-virtualenv
fi

# create python virtualenv
rm -rf /tmp/sim_service
virtualenv -p python3.6 /tmp/sim_service

# activate python virtualenv, install python package
PS1=${PS1:-} source /tmp/sim_service/bin/activate
pip install -e $APP_ROOT

# speeds up first time usage of bazel targets used at runtime
$APP_ROOT/build_bazel_deps.sh
