#!/bin/bash

function failCommit() {
    tput setaf 1
    echo "----------------------------------------"
    echo "FATAL ERROR: $1"
    echo "----------------------------------------"
    tput sgr0
    exit 1
}

function testFail() {
    tput setaf 3
    echo "----------------------------------------"
    echo "$1"
    echo "----------------------------------------"
    tput sgr0
}

if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=af6266a03b218876a11655e4f653dd05d0424641
fi

# Remove all of the trailing whitespace in this commit
for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -E 's/:[0-9]+:.*//' | uniq` ; do
    sed -i '' -E 's/[[:space:]]*$//' "$FILE"
    git add $FILE
done

echo 'Running Jshint...'
result=$(make lint)
if grep "error" <<< $result; then
    num=$(grep "[0-9]+ error" <<< "$result")
    testFail "Jshint: $num"
    echo "$result"
    echo ''
    echo 'Jslint fail'
    lintFailed=1
fi

if [[ $lint_errors -gt 0 ]]; then
    failCommit "Lint Errors"
fi

echo 'Running Tests...'
result=$(make test)
if grep -q FAILURES <<< $result; then
    num=$(grep "FAILURES" <<< "$result")
    testFail "Test $num"
    echo "$result"
    echo ''
    echo 'Tests fail'
    testsFailed=1
fi

if [[ $testsFailed || $lintFailed ]]; then
    failCommit "Unable To Commit"
fi
