#!/bin/sh
set -eu

# Copyright 2023, Canonical Ltd.
# SPDX-License-Identifier: GPL-3.0

PYTHON_SCRIPTS=$(find . -maxdepth 1 -type f -exec grep -l '^#! */usr/bin/python3$' {} +)

run_black() {
    echo "Running black..."
    black -C --check --diff . ${PYTHON_SCRIPTS}
}

run_isort() {
    echo "Running isort..."
    isort --check-only --diff .
}

run_flake8() {
    echo "Running flake8..."
    flake8 --max-line-length=99 --ignore=E203,W503 . $PYTHON_SCRIPTS
}

run_mypy() {
    echo "Running mypy..."
    mypy .
    mypy --scripts-are-modules $PYTHON_SCRIPTS
}

run_pylint() {
    echo "Running pylint..."
    pylint "$@" $(find * -name '*.py') $PYTHON_SCRIPTS
}

if test "${1-}" = "--errors-only"; then
    # Run only linters that can detect real errors (ignore formatting)
    run_black || true
    run_isort || true
    run_flake8 || true
    run_mypy
    run_pylint --errors-only
else
    run_black
    run_isort
    run_flake8
    run_mypy
    run_pylint
fi
