#!/bin/bash
# Runs the repository-pinned clang-tidy binary from the workspace root.
#
# Bazel's `run` command executes clang-tidy from its runfiles tree, which makes
# workspace-relative source paths and .clang-tidy discovery fail. This wrapper
# builds the tool and then invokes the produced binary directly.

set -e

export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"

repo_root="$(git rev-parse --show-toplevel)"
source "$repo_root/scripts/shardok_cpp_config.sh"

if [ "$#" -eq 0 ]; then
    echo "Usage: scripts/run-clang-tidy.sh [clang-tidy args] <files> [-- <compiler args>]"
    echo ""
    echo "Examples:"
    echo "  scripts/run-clang-tidy.sh --verify-config"
    echo "  scripts/run-clang-tidy.sh src/main/cpp/net/eagle0/shardok/library/CombatDamage.hpp -- -I$repo_root $SHARDOK_CXX_STANDARD_ARG"
    exit 2
fi

bazel build @llvm_toolchain//:clang-tidy

bazel_bin="$(bazel info bazel-bin)"
clang_tidy="$bazel_bin/external/toolchains_llvm++llvm+llvm_toolchain/clang-tidy"

if [ ! -x "$clang_tidy" ]; then
    echo "Unable to find clang-tidy binary at $clang_tidy"
    exit 1
fi

"$clang_tidy" --config-file="$repo_root/.clang-tidy" "$@"
