#!/usr/bin/env bash

set -e

# Ensure we are at the root of the repo
ROOT_DIR=$(git rev-parse --show-toplevel)
cd "${ROOT_DIR}"

source ./scripts/test_lib.sh

TOOL_SRC="${ETCD_ROOT_DIR}/tools/check-grpc-experimental"
ALLOWLIST="${TOOL_SRC}/allowlist.txt"

FAILURES=0

modules=()
load_workspace_relative_modules modules

for module in "${modules[@]}"; do
  echo "------------------------------------------------"
  echo "Checking module: ${module}"
  if ! go run "${TOOL_SRC}" -allow-list="${ALLOWLIST}" "${module}"; then
    echo "ERROR: Experimental usage found in ${module}"
    FAILURES=$((FAILURES+1))
  fi
done

echo "------------------------------------------------"
if [ "$FAILURES" -eq 0 ]; then
  echo "SUCCESS: No experimental gRPC APIs found in any module."
  exit 0
else
  echo "FAILURE: Found experimental gRPC API usage in ${FAILURES} module(s)."
  exit 1
fi
