#!/bin/bash
# run static code checks like pyflakes and pep8
set -eu

echo "1..7"

builddir=$(pwd)
cd "${srcdir:-.}"
fail=0

# we don't dist bots, so only check it when running in git
[ -d bots ] &&  BOTS=$(grep -lI '#!.*python' `find bots/ -type f ! -name '.*'` 2>/dev/null || true) || BOTS=

#
# pyflakes
#
PYFLAKES="python3 -m pyflakes"

if ! $PYFLAKES /dev/null >/dev/null 2>&1; then
    echo "ok 1 pyflakes bots pkg tools # SKIP pyflakes3 not installed"

elif $PYFLAKES $BOTS pkg/docker/cockpit-atomic-storage tools/build-debian-copyright >&2; then
    echo "ok 1 pyflakes bots pkg tools"

else
    echo "not ok 1 pyflakes bots pkg tools"
    fail=1
fi

# TODO: there are currently a lot of pyflakes errors like
#   'parent' imported but unused
#   'from testlib import *' used; unable to detect undefined names
# Filter these out until these get fixed properly.
if ! $PYFLAKES /dev/null >/dev/null 2>&1; then
    echo "ok 2 pyflakes test # SKIP pyflakes3 not installed"

else
    out=$($PYFLAKES test/ 2>&1 | grep -Ev "(unable to detect undefined names|defined from star imports|'parent' imported but unused)") || true
    if [ -n "$out" ]; then
        echo "$out" >&2
        echo "not ok 2 pyflakes test"
        fail=1
    else
        echo "ok 2 pyflakes test"
    fi
fi

#
# wrongly marked translatable strings
#

if out=$(find src/ pkg/ -name '*.js' -o -name '*.jsx' | xargs grep "_('"); then
    echo 'ERROR: translatable strings must be marked with _("")' >&2
    echo "$out" >&2
    echo "not ok 3 js-translatable-strings"
    fail=1
else
    echo "ok 3 js-translatable-strings"
fi

#
# Unsafe content-security-policy
#
# It's dangerous to have 'unsafe-inline' or 'unsafe-eval' in our
# content-security-policy entries. This is the browser equivalent
# of setenforce 0

shopt -s nullglob
safe=yes
for d in pkg/*; do
    if ! test -f "$d"/content-security-policy.override; then
        if grep -HE 'content-security-policy.*(\*|unsafe)' "$d"/*.json* /dev/null; then
            safe="no"
        fi
    fi
done

if [ $safe == "no" ]; then
   echo "not ok 4 unsafe-security-policy"
   fail=1
else
    echo "ok 4 unsafe-security-policy"
fi

#
# Bad paths or modifications in patternfly for fonts
#

if grep "\.\./fonts/OpenSans\|fonts/.*eot\|truetype" `ls dist/*/*.css $builddir/dist/*/*.css` >&2; then
    echo "ERROR: invalid patternfly fonts paths found" >&2
    echo "not ok 5 patternfly-font-paths"
    fail=1
else
    echo "ok 5 patternfly-font-paths"
fi

# Valid JSON files
if [ -d .git ]; then
    for f in $(git ls-tree --name-only -r --full-name HEAD | grep '\.json$'); do
        if ! python3 -m json.tool $f /dev/null; then
            echo "ERROR: $f is invalid JSON" >&2
            echo "not ok 6 json-verify"
            fail=1
        fi
    done
    echo "ok 6 json-verify"
else
    echo "ok 6 json-verify # SKIP not a git tree"
fi

# pycodestyle/pep8 python syntax check
if python3 -m pycodestyle /dev/null >/dev/null 2>&1; then
    PYTHON_STYLE_CHECKER="pycodestyle"
elif python3 -m pep8 /dev/null >/dev/null 2>&1; then
    PYTHON_STYLE_CHECKER="pep8"
fi

if [ -z "${PYTHON_STYLE_CHECKER-}" ]; then
    echo "ok 7 pycodestyle test # SKIP pycodestyle not installed"
else
    # FIXME: Fix code for the warnings and re-enable them
    out=$(python3 -m "$PYTHON_STYLE_CHECKER" --ignore E501,E265,E261,W504,W605 test/* --exclude=test/verify/nested-kvm,test/vm-reset,test/vm-run,test/README.md) || true
    if [ -n "$out" ]; then
        echo "$out" >&2
        echo "not ok 7 $PYTHON_STYLE_CHECKER test"
        fail=1
    else
        echo "ok 7 $PYTHON_STYLE_CHECKER test"
    fi
fi

exit $fail
