#!/bin/bash
set -eo pipefail

if [[ -z "${BOBJS_ROOT}" ]]; then
  # Use 'readlink -e' to expand relative path to absolute
  # because the package won't be found otherwise.
  BOBJS_ROOT=$(readlink -e ../../../spectrum/bobjs)
fi

echo "bobjs root: ${BOBJS_ROOT}"

LINK=node_modules/@zoox/bobjs
if [ -L $LINK ]; then
  echo "bobjs link exists... deleting"
  rm $LINK
fi

ZOOX_SUBFOLDER=node_modules/@zoox
if [ ! -e $ZOOX_SUBFOLDER ]; then
  mkdir $ZOOX_SUBFOLDER
fi

echo "Linking $BOBJS_ROOT to $LINK"
ln -s $BOBJS_ROOT $LINK
pushd $BOBJS_ROOT
yarn install
popd
