#!/bin/sh
#
# To enable this hook, execute "python misc/install-git-hook.py". It
# makes sure pushed code is checked.

# Redirect output to stderr.
exec 1>&2

if [ "$COMMIT_UNCHECKED" = "1" ]
then
    exit 0
fi

has_errors=0

while read local_ref local_oid remote_ref remote_oid
do
    case "$local_ref" in
        refs/tags/*)
            continue
            ;;
    esac

    if [ "$local_oid" = "0000000000000000000000000000000000000000" ]
    then
        continue
    fi

    if [ "$remote_oid" = "0000000000000000000000000000000000000000" ]
    then
        against="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
    else
        against="$remote_oid"
    fi

    # spell-checker: ignore ACMR
    files=$(git diff --name-only --diff-filter=ACMR $against..$local_oid)

    if [ -z "$files" ]
    then
        continue
    fi

    python_files=""
    c_j2_files=""
    py_j2_files=""
    all_files=""
    has_specialize_tool=0

    for file in $files
    do
        all_files="$all_files $file"

        case "$file" in
            *.py)
                python_files="$python_files $file"
                ;;
            *.c.j2)
                c_j2_files="$c_j2_files $file"
                ;;
            *.py.j2)
                py_j2_files="$py_j2_files $file"
                ;;
        esac

        case "$file" in
            nuitka/tools/specialize/*)
                has_specialize_tool=1
                ;;
        esac
    done

    if [ -n "$all_files" ]
    then
        OPTIONS="--check --assume-yes-for-downloads"

        if [ -f "bin/autoformat-nuitka-source" ]
        then
            ./bin/autoformat-nuitka-source $OPTIONS $all_files
            if [ $? -ne 0 ]; then has_errors=1; fi
        else
            python -m pipenv run python Nuitka-develop/bin/autoformat-nuitka-source $OPTIONS $all_files
            if [ $? -ne 0 ]; then has_errors=1; fi
        fi
    fi

    if [ -n "$python_files" ]
    then
        if [ -f "bin/check-nuitka-with-pylint" ]
        then
            ./bin/check-nuitka-with-pylint $python_files
            if [ $? -ne 0 ]; then has_errors=1; fi
        else
            python -m pipenv run python Nuitka-develop/bin/check-nuitka-with-pylint $python_files
            if [ $? -ne 0 ]; then has_errors=1; fi
        fi
    fi

    if [ -n "$c_j2_files" ] || [ "$has_specialize_tool" = "1" ]
    then
        if [ "$has_specialize_tool" = "1" ]
        then
            c_args=""
        else
            c_args="$c_j2_files"
        fi

        if [ -f "bin/generate-specialized-c-code" ]
        then
            ./bin/generate-specialized-c-code --check $c_args
            if [ $? -ne 0 ]; then has_errors=1; fi
        else
            python -m pipenv run python Nuitka-develop/bin/generate-specialized-c-code --check $c_args
            if [ $? -ne 0 ]; then has_errors=1; fi
        fi
    fi

    if [ -n "$py_j2_files" ] || [ "$has_specialize_tool" = "1" ]
    then
        if [ "$has_specialize_tool" = "1" ]
        then
            py_args=""
        else
            py_args="$py_j2_files"
        fi

        if [ -f "bin/generate-specialized-python-code" ]
        then
            ./bin/generate-specialized-python-code --check $py_args
            if [ $? -ne 0 ]; then has_errors=1; fi
        else
            python -m pipenv run python Nuitka-develop/bin/generate-specialized-python-code --check $py_args
            if [ $? -ne 0 ]; then has_errors=1; fi
        fi
    fi
done

if [ "$has_errors" = "1" ]
then
    echo "Error, pre-push checks failed."
    exit 1
fi

exit 0
