#!/usr/bin/env bash

PLATFORM=$(uname)
TOOLSPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
CLANGFORMAT="${TOOLSPATH}/${PLATFORM}/clang-format -style=file"

if [ "$1" == "--all" ]
then
    pushd "$TOOLSPATH/.." > /dev/null
    CHECK_DIRS=()
    for DIR in "include" "src" "tests/src" "tests/unit" "tools"; do [ -d "$TOOLSPATH/../$DIR" ] && CHECK_DIRS+=("$DIR") ; done
    find ${CHECK_DIRS[@]} \
       \( -name '*.cpp' -o -name '*.h' -o -name '*.cu' -o -name '*.cuh' -o -name '*.hpp' \) \
       -not -name pugi* -not -name json.hpp -not -path '*/third-party/*' \
       -print0 \
    | xargs -n1 -0 ${CLANGFORMAT} -output-replacements-xml \
    | grep -c "<replacement " > /dev/null
    grepCode=$?
    popd > /dev/null
elif [ "$1" == "--staged" ]
then
    pushd "$TOOLSPATH/.." > /dev/null
    git diff --cached --name-only --diff-filter=ACMRT | grep -e '.*\.h$' -e '.*\.cpp$' -e '.*\.hpp$' -e '.*\.cu$' -e '.*\.cuh$' -v '**third-party/*' \
    | xargs -n1 ${CLANGFORMAT} -output-replacements-xml \
    | grep -c "<replacement " >/dev/null
    grepCode=$?
    popd > /dev/null
else
    echo "Please specify --all or --staged"
    exit 1
fi

# grep exits 0 => found needed formatting changes
if [ $grepCode -ne 0 ]
then
    echo "Formatting looks good!"
    exit 0
else
    echo "****************************************************"
    echo "Code needs formatting!  Please use 'tools/format-all'"
    echo "****************************************************"
    exit 1
fi
