#! /bin/bash

# This file is part of IVRE.
# Copyright 2011 - 2026 Pierre LALET <pierre@droids-corp.org>
#
# IVRE is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IVRE is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with IVRE. If not, see <http://www.gnu.org/licenses/>.

# pip install -U '.[linting]'

black -t py312 --check --extend-exclude="_version\.py" ./doc/conf.py ./tests/tests.py ./tests/tests_no_backend.py ./ivre/ ./pkg/stubs/ && echo "black OK" || exit 1

flake8 --ignore=E402,E501,F401 ./doc/conf.py && flake8 --ignore=E203,E402,E501,W503 ./tests/tests.py ./tests/tests_no_backend.py && flake8 --ignore=E203,E501,E704,F821,W503 ./ivre/ && flake8 --ignore=E302,E305,E701,E704 ./pkg/stubs/*.pyi && echo "flake8 OK" || exit 1

bandit --severity-level high -r ./doc/conf.py ./tests/tests.py ./tests/tests_no_backend.py ./ivre/ ./pkg/stubs/ && echo "bandit OK" || exit 1

# TODO: remove --follow-imports=skip
MYPYPATH=./pkg/stubs/ mypy --follow-imports=skip --disallow-untyped-calls --disallow-untyped-decorators --disallow-untyped-defs --disallow-incomplete-defs --no-implicit-optional --warn-redundant-casts --warn-unused-ignores --warn-return-any ./ivre/{active,analyzer,data,parser,tags,tools,types}/*.py ./ivre/{__init__,activecli,config,flow,geoiputils,graphroute,keys,plugins,utils,zgrabout}.py && echo "mypy OK" || exit 1
# partial sub-modules
MYPYPATH=./pkg/stubs/ mypy --follow-imports=skip --disallow-untyped-calls --disallow-untyped-decorators --disallow-incomplete-defs --no-implicit-optional --warn-redundant-casts --warn-unused-ignores --warn-return-any ./ivre/db/mongo.py && echo "mypy (partial) OK" || exit 1

# bash_completion/ is a generated artifact (pkg/buildcompletion);
# shtab's bash template carries an upstream spelling typo.
git ls-files | grep -vE '^web/static/(doc|an|bs|d3|jq|lk|ui)/|^bash_completion/|^data/|\.(png|gif|svg|lock)$|-lock.yaml$' | xargs -r codespell --ignore-words=pkg/codespell_ignore && echo "codespell OK" || exit 1

pylint -e all -d abstract-method,arguments-differ,attribute-defined-outside-init,broad-except,broad-exception-raised,duplicate-code,fixme,function-redefined,global-statement,import-error,invalid-name,locally-disabled,missing-docstring,no-member,protected-access,super-init-not-called,suppressed-message,too-few-public-methods,too-many-ancestors,too-many-arguments,too-many-boolean-expressions,too-many-branches,too-many-instance-attributes,too-many-lines,too-many-locals,too-many-nested-blocks,too-many-positional-arguments,too-many-public-methods,too-many-return-statements,too-many-statements,unsubscriptable-object,unused-argument,line-too-long ivre ./doc/conf.py && echo "pylint OK" || exit 1

pylint -e all -d unused-argument,too-many-arguments,too-many-positional-arguments,missing-function-docstring,missing-class-docstring,missing-module-docstring,multiple-statements,invalid-name,too-few-public-methods ./pkg/stubs/*.pyi && echo "pylint stubs OK" || exit 1

isort --profile black --check-only ivre ./doc/conf.py ./tests/tests.py ./tests/tests_no_backend.py ./pkg/stubs/*.pyi && echo "isort OK" || exit 1

rstcheck -r ./doc/ && echo "rstcheck OK" || exit 1
if ! find ./doc/ -type f -name '*.rst' -print0 | xargs -0 grep '[[:space:]]$'; then echo "trailing spaces OK"; else echo "!! trailing spaces !!"; exit 1; fi

sphinx-lint -e all -d line-too-long -d default-role ./doc/ && echo "sphinx-lint OK" || exit 1

shellcheck .github/actions/install/install*.sh pkg/buildcompletion pkg/builddockers pkg/builddocs pkg/runchecks && echo "shellcheck OK" || exit 1
