#! /bin/bash

#
# A simple script for verifying that all the the Python code correctly formatted
# with the Black code formatter.
#
# Usage:
#   see --help
#
#
set -e

while [[ $# -gt 0 ]]
do
    case "$1" in
        -h|--help)
            echo "Usage: $(basename $0) [OPTIONS] <git-range>"
            echo ""
            echo "This script runs the Black formatter on all the Python code"
            echo "in the current repository."
            echo "The files formatted are controlled by the local pyproject.toml file's"
            echo "include and exclude regexes."
            echo "and exits with exit-code 1 if any python code found is not formatted"
            echo ""
            echo "Recognized environment variables:"
            echo ""
            echo "BLACK_FORMATTER_VERSION:"
            echo "The version of the black formatter used"
            exit 0
            ;;
        *)
            break
            ;;
    esac
done

if  ! black --check --verbose .; then
    echo >&2 ""
    echo >&2 "Please fix your code locally with: black ."
    echo >&2 "This will automatically format the code as needed."
    echo >&2 "Note that the Black version used is: " "$BLACK_FORMATTER_VERSION."
    exit 1
fi

exit 0
