#!/usr/bin/env bash

set -euo pipefail

current_directory="$(pwd)"

cleanup() {
    cd ${current_directory}
    exit
}
trap cleanup INT TERM

if [[ "$OSTYPE" == "darwin"* ]]; then
  DRIVING_ROOT="$(cd ../../../../ && pwd)"
  echo $DRIVING_ROOT
else
  DRIVING_ROOT=$(readlink -f $(dirname "${BASH_SOURCE[0]}")/../../../..)
fi
SPECTRUM_ROOT=$DRIVING_ROOT/spectrum
FIREFLY_ROOT=$SPECTRUM_ROOT/apps/firefly
cd "$(dirname "$0")"

echo "Checking if we need to copy ssh"

if [[ "$(uname)" != "Darwin" ]] && [[ $(id --user --name) == root ]] && [[ "${ZOOX_PROJECT_NAME:-}" == firefly ]]; 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/*
  echo "Copying zoox's AWS credentials from '/home/zoox/.aws' to '/root/.aws'..."
  cp --recursive /home/zoox/.aws /root/.aws
fi

# nvm is not compatible with the "PREFIX" environment variable: currently set to "/usr/local"
unset PREFIX
source ${SPECTRUM_ROOT}/scripts/setup_environment.sh
source ${SPECTRUM_ROOT}/scripts/install_yarn.sh

cd ${FIREFLY_ROOT}/client/


nvm install && nvm use

# Install dependencies.
yarn install --frozen-lockfile --mutex file:${DRIVING_ROOT}/yarn.mutex

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

# Run unit tests.
yarn test:unit

# Make a production build.
yarn electron:build
