#!/usr/bin/env bash
set -eu

# usage: $1: <src-dir>

src_dir="${1:-"$(readlink -f "$(dirname "${0}")/..")"}"

echo 'Running flake8 for all python modules..'
error=0

export PYTHONPATH="${src_dir}"

echo 'running pylama for all modules (errors only)'
(
    pushd "${src_dir}"
    set -x
    if pylama -iW,R,C  -lpylint --options "${src_dir}/pylama.ini" "${src_dir}"; then
        echo 'pylama succeeded'
    else
        ((error|=1))
    fi

    if "${src_dir}/.ci/lint-flake8"; then
        echo 'pyflake8 succeeded'
    else
        ((error|=1))
    fi

    if [ $error -eq 0 ]; then
        exit 0
    elif [ $error -gt 0 ]; then
        exit 1
    fi
    popd
)

if ! which bandit &>/dev/null; then
    pip3 install bandit &>/dev/null
fi

echo 'running bandit (sast-linter)'
bandit \
    --configfile "${src_dir}/pyproject.toml" \
    --recursive \
    "${src_dir}"

exit $?
