#!/bin/bash

set -e

# When building under Rosetta, use a separate output root, so that repo rules don't
# need to be run again when switching between x86_64 and arm64 builds.
extra_args=""
if [[ $OSTYPE == 'darwin'* ]]; then
    if [ $(uname -m) = x86_64 -a "$(sysctl -in sysctl.proc_translated)" = 1 ]; then
        extra_args="--output_base=$HOME/.cache/anki-rosetta"
    fi
fi

# Bazelisk will place the tools folder at the front of the path. This breaks
# genrule() invocations like //:buildinfo, as they call 'bazel run python', which
# fails as BAZEL_REAL is not passed to the child process. Work around it by removing
# the tools folder from the path.
toolsPath=$(dirname $0)
export PATH=$(echo "$PATH" | sed "s@${toolsPath}:@@")

exec $BAZEL_REAL $extra_args "$@"
