#!/usr/bin/env bash

# Find all Python scripts and check their PEP8 compliance using pycodestyle

# Copyright (C) 2017 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2017 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-or-later


set -o nounset -o pipefail -o errexit

declare -a file_list

if ! type pycodestyle > /dev/null 2>&1 ; then
    printf "%s\\n" "Error: pycodestyle not found"
    exit 1
fi

printf "%s " "Searching for Python scripts"

counter=0

while read -r in ; do
    if file -i "${in}" | grep -q x-python ; then
        file_list+=("${in}")
        counter=$(( counter + 1 ))
        counter_state=$(( counter % 5 ))
        if [ ${counter_state} -eq 0 ] ; then
            printf "%s" "."
        fi
    fi
done < <(find . -type f -not -iwholename "./.git/*" \
    -not -wholename "./docs/ansible/roles/system_users/defaults-detailed.rst" \
    -not -wholename "./docs/ansible/roles/users/defaults-detailed.rst" \
    -not -wholename "./ansible/roles/gunicorn/templates/etc/gunicorn/application.conf.py.j2" \
    -not -wholename "./ansible/roles/mailman/templates/etc/mailman/mm_cfg.py.j2")

printf " %s\\n" "found ${#file_list[@]} Python scripts"

pycodestyle --count --show-source --statistics "${file_list[@]}"
