#!/usr/bin/env bash

function join_by() { local IFS="$1"; shift; echo "$*"; }

set -e

# We check only our code, that's why we skip contrib
GIT_ROOT=$(git rev-parse --show-cdup)
GIT_ROOT=${GIT_ROOT:-./}

# Find all *.py, *.python files and executable files without extension
# that are determined as python scripts by 'file' util
# in the repo except the contrib directory.
find_cmd=(
  find "$GIT_ROOT" -type f -not -path "${GIT_ROOT}contrib/*"
    \(
      \(
        -name '*.py' -or -name "*.python" -or
          \(
            -executable -not -name "*.*" -exec sh -c 'file {} | grep -q "Python script"' \;
          \)
      \)
      # We skip modules generated by the protocol buffer compiler from *.proto files.
      -and -not -name '*_pb2.py' -and -not -name '*_pb2_grpc.py'
    \) -print0
)

ignores=(
    E101 # Indentation contains mixed spaces and tabs
    E203 # Whitespace before ':'
    E226 # missing whitespace around arithmetic operator
    E266 # Too many leading '#' for block comment
    E401 # Multiple imports on one line
    E402 # Module level import not at top of file
    E501 # line too long
    E711 # Comparison to None should be 'cond is None:'
    E712 # Comparison to true should be 'if cond is true:' or 'if cond:'
    E713 # Test for membership should be 'not in'
    E714 # Test for object identity should be 'is not'
    E722 # Do not use bare except, specify exception instead
    E731 # Do not assign a lambda expression, use a def
    E741 # Do not use variables named 'I', 'O', or 'l'
    F401 # Module imported but unused
    F403 # 'from module import *' used; unable to detect undefined names
    F405 # Name may be undefined, or defined from star imports: module
    F522 # .format(...) unused named arguments
    F541 # f-string without any placeholders
    F811 # redefinition of unused name from line N
    F841 # local variable name is assigned to but never used
    W191 # Indentation contains tabs
    W291 # Trailing whitespace
    W293 # Blank line contains whitespace
    W503 # Line break occurred before a binary operator
)
"${find_cmd[@]}" | xargs -0 flake8 --ignore "$(join_by , "${ignores[@]}")"
