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

# yarn install, with up to 3 retries to workaround:
#   > "ENOENT: no such file or directory, scandir
#   > '/zoox/driving/sim/scenario_editor/web/frontend/node_modules/@zoox/argus-component-library/node_modules/core-js'".

APP_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
install_command="yarn --frozen-lockfile --mutex file:../../../../yarn.mutex install"
counter=1
counter_max=3

pushd $APP_ROOT  # move to app root directory

npm config set scripts-prepend-node-path true

while ! eval $install_command
do
  ((counter++))
  if [ $counter -gt $counter_max ]
  then
    echo "\nyarn install failed after max retries"
    exit 1
  else
    echo "\nretrying yarn install, attempt: $counter"
  fi
done

popd  # move back to prior directory
