#!/bin/bash -e

# Copyright (C) 2022 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.

# Common script utilities
source $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../../make/shell_utils.sh
require_top

if [[ -z ${OUT_DIR+x} ]]; then
	PROFILE_OUT=$TOP/out
else
	PROFILE_OUT=$OUT_DIR
fi
mkdir -p $PROFILE_OUT

# Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
bazel_args=""
skip_tests="--skip-soong-tests"
for i in $@; do
    if [[ $i != "--run-soong-tests" ]]; then
        bazel_args+="$i "
    else
        skip_tests=""
    fi
done

# Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
# RBE is disabled because it's not used with b builds and adds overhead: b/251441524
# TODO(b/262904551) - this is Darwin incompatible and should eventually be updated.
BUILD_STARTED_TIME=`date +%s%3N`
B_ARGS=$*
USE_RBE=false "$TOP/build/soong/soong_ui.bash" --build-mode --all-modules --dir="$(pwd)" $skip_tests bp2build USE_BAZEL_ANALYSIS= --build-command="b ${B_ARGS}" --skip-metrics-upload --build-started-time-unix-millis=$BUILD_STARTED_TIME || exit 1


# Then, run Bazel using the synthetic workspace as the --package_path.
if [[ -z "$bazel_args" ]]; then
    # If there are no args, show help and exit.
    "$TOP/build/bazel/bin/bazel" help
else
    # Else, always run with the bp2build configuration, which sets Bazel's package path to
    # the synthetic workspace.
    # Add the --config=bp2build after the first argument that doesn't start with a dash. That
    # should be the bazel
    # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work
    # with commands like:  b run //foo -- --args-for-foo
    config_set=0

    # Represent the args as an array, not a string.
    bazel_args_with_config=()
    for arg in $bazel_args; do
        if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here
        then
            bazel_args_with_config+=("--profile=$PROFILE_OUT/bazel_metrics-profile --config=bp2build -- ")
            config_set=1
        else
            bazel_args_with_config+=("$arg ")
        fi
    done
    if [[ $config_set -ne 1 ]]; then
        bazel_args_with_config+=("--profile=$PROFILE_OUT/bazel_metrics-profile --config=bp2build ")
    fi

    # Call Bazel.
    "$TOP/build/bazel/bin/bazel" ${bazel_args_with_config[@]}
    "$TOP/build/bazel/bin/bazel" analyze-profile $PROFILE_OUT/bazel_metrics-profile > $PROFILE_OUT/analyzed_bazel_profile.txt
    rm $PROFILE_OUT/bazel_metrics-profile
    "$TOP/build/soong/soong_ui.bash" --upload-metrics-only
fi
