#!/usr/bin/env bash

set -e

if [ -d ".git" ] && [ ! -e ".git/hooks/pre-commit" ]; then
    ln -s ../../bin/pre-commit .git/hooks/"$(basename $0)"
fi

echo "!!! Running pre-commit hook !!!"

echo "1. Enforcing style \`autopep8 --in-place --aggressive --aggressive \"*.py\"\`"
autopep8 --in-place --aggressive --aggressive $(find . -name "*.py")

echo "Checking for syntax errors \`flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics\`"
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics

echo "Exit with error if flake8 fails \`flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics\`"
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

echo "Running tests \`pytest\`"
pytest

# TODO: increase required coverate to al least 90
echo "Running coverage \`pytest-cov\`"
pytest --cov src --cov-fail-under 25
