git config core.hookspath .git/hooks
ln -sf …/…/.githooks/pre-commit.sh .git/hooks/pre-commit

#!/bin/bash
function abort() {
    local msg=$1
    printf "\e[5m\e[1m\e[31m[COMMIT FAILED]\e[0m\e[1m %s\e[0m\n" "$msg"
    exit 1
}

function check_commands() {
    for cmd in $@; do
        which $cmd > /dev/null || abort "$cmd : command not found"
    done
}

check_commands flake8 clang-format isort

NEWFILES=$(git diff --cached --name-only --diff-filter=ACM)

for FILE in $NEWFILES; do
    case $FILE in
        *.cpp|*.hpp)
            clang-format -i $FILE
            echo  $FILE "auto corrected"
            ;;
        *.py)
            printf "\e[1mChecking\e[0m %s\n" "$FILE"
            isort --ac $FILE && {
                echo "isort: pass "
                git add $FILE
            }
            flake8 $FILE && echo "flake8: pass" || abort "Failed to pass flake8 style check"
            ;;
        *.json)
            python3 utils/update_json.py $FILE $FILE
            ;;
        *)
            ;;
    esac
    git add $FILE
done