#!/bin/bash

set -euxo pipefail

if [[ -z "${GIT_SHA}" ]]; then
  echo "GIT_SHA not set!"
  exit 1
fi

git fetch --tags -v --progress origin "$GIT_SHA"
git rev-parse --verify --quiet "$GIT_SHA"
git checkout --quiet "$GIT_SHA"

# install ethtool for eth link speed query
sudo apt-get update
sudo apt-get -y install ethtool
sudo apt-get -y install pciutils
sudo apt-get -y install udisks2

# set target for playbook
ZOOX_ANSIBLE_INVENTORY_TARGET=dev

# run subset of setup playbook
ansible-playbook --inventory-file scripts/ansible/dev_hosts.py --extra-vars driving_root=/home/zoox/driving --extra-vars {"mount":false} scripts/ansible/install.yml --tags=bazel,buildifier,file_validators,python,python_deps,python_dev_deps,python_pip,python_pip_conf,python_ssl,versions_bazel

# build all for ingest
bazel build //data/vehicle_data_sync:all

# build tar package
bazel build //data/vehicle_data_sync:ingest_pkg_tar

# move tar package to /opt/zoox/ingest
ingest_directory="/opt/zoox/ingest"
mkdir --parents "$ingest_directory"
tar --extract --verbose --file ~/driving/bazel-bin/data/vehicle_data_sync/ingest_pkg_tar.tar --directory "$ingest_directory"

# clear the driving repo
rm --recursive --force ~/driving/*

# clean bazel cache `.cache/bazel`
rm --recursive --force ~/.cache/bazel

# clean apt cache
sudo apt-get clean

# set zoox role to ingest inside container
echo -n "kiosk" > /etc/zoox/role
