#!/bin/sh

# NOTE: Run this script from the root of the ViewVC checkout.
#
# $ ./tools/check-code-formatting

# The linting tools will generally find all the .py files, but we want
# to also add into the set of checked paths other Python files
# (scripts that reference Python in the shebang line, WSGI stubs,
# etc.)

# If the user passes a --fix flag, we should fix the files
if [ "$1" = "--fix" ]; then
    echo "Checking code formatting..."
    CHECK_ARGS=""
else
    echo "Checking code formatting (read-only)..."
    CHECK_ARGS="--check"
fi
PYFILES=`grep -rl '^#\(!.*/\|.*-\)python' --exclude=*.py .`
black --target-version=py310 --line-length=100 --quiet ${CHECK_ARGS} . ${PYFILES} && flake8 . ${PYFILES}
if [ $? -eq 0 ]; then
    echo ""
    echo "Linting succeeded!"
else
    echo ""
    echo "Linting failed."
    if [ "$1" != "--fix" ]; then
        echo "Trying running this script again with the --fix option."
    fi
fi
