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

export LC_ALL="en_US.UTF-8"

DRIVING="/home/zoox/driving"
if [ -d $DRIVING ] && [ "$(ls -A $DRIVING)" ]; then
  cd $DRIVING
  git fetch && git checkout origin/master
else
  git clone git@git.zooxlabs.com:zooxco/driving.git $DRIVING
fi
if [ ! -d /etc/zoox/calibration ]; then
  git clone git@git.zooxlabs.com:zooxco/calibration.git /etc/zoox/calibration
fi

compute_version=`sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g' /etc/zoox/os_metadata.yml | grep compute_version | cut -d'"' -f 2`
cd $DRIVING
./imaging/zimage/run_playbook.py os $compute_version --live -- --tags cgroups --extra-vars '{"is_live_update": false}'

# install and start bamboo
cd $DRIVING/infra/ansible
ansible-playbook ../../ci/zimbeast/ansible/bamboo-agent.yaml --limit {{ inventory_hostname }} -i ../../ci/zimbeast/ansible/inventory.yaml
# re-apply netbind capability to python executables and re-install needed python3 dependencies
ansible-playbook ../../ci/zimbeast/ansible/install-zimbot.yaml --limit {{ inventory_hostname }} --tags net_bind_perms,python3_deps -i ../../ci/zimbeast/ansible/inventory.yaml
