#!/bin/bash

command -v clang-format >/dev/null 2>&1 || {
    echo >&2 "Githooks require clang-format but it's not available in this environment. Bypassing githook."
    exit 0
}

files=()
for file in $(git diff --cached --name-only --diff-filter=ACMRT ':!interfaces' ':!ThirdParty' | grep -E "\.(cpp|hpp|c|h)$"); do
    if ! cmp -s <(git show ":${file}") <(git show ":${file}" | clang-format); then
        files+=("${file}")
    fi
done

if [[ ${#files[@]} -ne 0 ]]; then
    echo "Clang-format found issues with the following files:"
    printf "%s\n" "${files[@]}"
    echo "Run clang-format -i <filename> to resolve the issue before committing, or commit with --no-verify"
else
    echo "Clang-format check passed successfully."
fi
