#!/bin/bash

# This hook will inspect pyflakes3 warnings on the python files to be
# committed. The hook should exit with non-zero status after issuing an
# appropriate message to the user asking wether to ignore the warnings or
# not.

MODIFIED_FILES=$(git diff --name-only --cached)
WARNINGS=0
for file in ${MODIFIED_FILES}
do
    IS_PYTHON_SCRIPT=$(file $file |grep "Python script")
    if [[ $IS_PYTHON_SCRIPT != "" ]] ;
    then
      # if we only have pyflakes3 available then use it, otherwise if we
        # have both then use pyflakes only if python2 is specified as the
      # interpreter-
      PYFLAKES=pyflakes3
      if head -n1 $file | grep -Eq "python2" ; then
        if which pyflakes >/dev/null; then
          PYFLAKES=pyflakes
        fi
      fi
      $PYFLAKES $file
      WARNINGS=$((WARNINGS + $?))
    fi
done

if [[ $WARNINGS -gt 0 ]];
then
    read -p 'Found pyflakes3 warnings. Enter "ignore" to continue or anything else to exit: ' answer < /dev/tty
    if ! [ "${answer}" = "ignore" ] ; then
        exit 1
    fi
fi

