#!/bin/bash
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

ROOT_DIR="$(dirname $(dirname $(cd -P ${BASH_SOURCE[0]%/*}; pwd)))"

if [ "$(uname -s)" == "Darwin" ]; then
readonly NODE_DIR="$ROOT_DIR/buildtools/mac/nodejs"
else
readonly NODE_DIR="$ROOT_DIR/buildtools/linux64/nodejs"
fi

if [ ! -e "$NODE_DIR" ]; then
  echo 'Could not find Node.js hermetic installation. Please run:'
  echo 'tools/install-build-deps --ui'
  exit 1
fi

# cd into the //infra/perfetto.dev dir, so this works uniformely when invoked by
# other paths. the cd is required for npm install.
cd -P ${BASH_SOURCE[0]%/*}

# Ensure that `pnpm` has been called and is current. Note that the set of
# packages that the docs website requires is != the UI ones.
if ! test node_modules/.stamp -nt pnpm-lock.yaml; then
  export PATH="$NODE_DIR/bin:$ROOT_DIR/tools:$PATH"
  ../../tools/pnpm install --shamefully-hoist --frozen-lockfile
  touch node_modules/.stamp
fi

../../tools/node ./build.js "$@"
