#!/bin/bash

CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
CLANG_FORMAT_VERSION=${CLANG_FORMAT_VERSION:-}

if ! type $CLANG_FORMAT >/dev/null || \
        ! $CLANG_FORMAT --version | grep -q "version ${CLANG_FORMAT_VERSION}"; then
    # If running tests, mark this test as skipped.
    exit 77
fi

errors=0
paths=$(git ls-files | grep '\.[ch]$')
for path in $paths; do
    echo "Checking $path"
    $CLANG_FORMAT $path > $path.formatted
    in=$(cat $path)
    out=$(cat $path.formatted)

    if [ "$in" != "$out" ]; then
        diff -u $path $path.formatted
        errors=1
    fi
    rm $path.formatted
done

if [ $errors -ne 0 ]; then
    echo "Formatting errors detected, run ./scripts/clang-format to fix!"
    exit 1
fi
