#!/usr/bin/env bash

set -euo pipefail

# Check if this is a CI machine on Bamboo;
# if so, clone ssh keys so we can install submodules, and install AWS config.
echo "Checking if we need to copy ssh"
if [[ $(id --user --name) == root ]] && [[ "${ZOOX_PROJECT_NAME:-}" == spectrum3d ]]; then
  echo "Copying zoox's SSH credentials from '/home/zoox/.ssh' to '/root/.ssh'..."
  cp --recursive /home/zoox/.ssh /root/.ssh
  chmod 600 /root/.ssh/*
fi

export NODE_VER="v12.13.0"

echo "Installing nvm..."

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

nvm install $NODE_VER
nvm use $NODE_VER

echo "Using node version..."
node --version

./install_yarn.sh

pushd ../tsprotos
yarn install --frozen-lockfile --mutex file:../../yarn.mutex
yarn build
popd

# Build dependencies.
pushd ../../mapping/zrn/web_viewer/worker/
yarn install --frozen-lockfile --mutex file:../../yarn.mutex
yarn build
popd

pushd ../../mapping/zrn/web_viewer/three_zrn/
yarn install --frozen-lockfile --mutex file:../../../../yarn.mutex
yarn build
popd

# Install dependencies.
yarn install --frozen-lockfile --mutex file:../../yarn.mutex

# Lint the entire project.
yarn lint --max-warnings 0

# Run unit tests.
yarn test

# Make a production build.
yarn --mutex file:../../yarn.mutex build
