#!/usr/bin/env bash

# Check if clang-format is available and pick the preferred binary.
if command -v clang-format-21 >/dev/null 2>&1; then
  CLANG_FORMAT_BIN="clang-format-21"
elif command -v clang-format >/dev/null 2>&1; then
  CLANG_FORMAT_BIN="clang-format"
else
  printf "'clang-format' not found in current environment\n"
  printf "Install clang-format-21 (recommended), clang, clang-tools, or clang-format depending on your distro/os and tooling requirements\n"
  exit 1
fi

set -euo pipefail

GIT_LS_FILES_FLAGS=""
# -g scopes formatting to tracked files currently modified in git status.
if [[ "${1:-}" == "-g" ]]; then
  GIT_LS_FILES_FLAGS="--modified"
fi

CLANG_FORMAT_VERSION_RAW="$(${CLANG_FORMAT_BIN} --version)"
CLANG_FORMAT_MAJOR="$(printf '%s\n' "${CLANG_FORMAT_VERSION_RAW}" | grep -oE '[0-9]+' | head -n1)"

# Guard against local binaries older than the repo formatting config.
if [[ -z "${CLANG_FORMAT_MAJOR}" || "${CLANG_FORMAT_MAJOR}" -lt 21 ]]; then
  echo "Error: ${CLANG_FORMAT_BIN} is too old: ${CLANG_FORMAT_VERSION_RAW}"
  echo "This repository's .clang-format requires clang-format 21 or newer."
  echo "Install clang-format-21 and rerun ./bin/clang-format-fix"
  exit 1
fi

# --- Main Logic ---

# Format all files (or only modified files if -g is passed)

# Use git diff in -g mode to capture staged and unstaged changes relative to HEAD.
# Otherwise use git ls-files to format all tracked source files.
# --exclude-standard ignores files in .gitignore.
# Also exclude generated, vendored, and build directories.
# Keep the no-match case non-fatal: grep returns 1 when no files match,
# which is expected when there are no modified C/C++ files.
set +o pipefail
if [[ "${1:-}" == "-g" ]]; then
  git diff --name-only HEAD -- \
      | grep -E '\.(c|cpp|h|hpp)$' \
      | grep -v -E '^(open-x4-sdk/|\.pio/|\.venv/|lib/EpdFont/builtinFonts/|lib/Epub/Epub/hyphenation/generated/|lib/uzlib/)' \
      | grep -v -E '\.generated\.h$' \
      | while IFS= read -r file; do
          [[ -f "$file" ]] && printf '%s\n' "$file"
        done \
      | xargs -r "${CLANG_FORMAT_BIN}" -style=file -i
else
  git ls-files --exclude-standard \
      | grep -E '\.(c|cpp|h|hpp)$' \
      | grep -v -E '^(open-x4-sdk/|\.pio/|\.venv/|lib/EpdFont/builtinFonts/|lib/Epub/Epub/hyphenation/generated/|lib/uzlib/)' \
      | grep -v -E '\.generated\.h$' \
      | xargs -r "${CLANG_FORMAT_BIN}" -style=file -i
fi
# Restore strict pipeline failure handling for the rest of the script.
set -o pipefail