#!/bin/bash
# Author: Remigijus Jarmalavičius <remigijus@jarmalavicius.lt> 
# Author: Vytautas Povilaitis <php-checker@vytux.lt>
#
# XDebug check added by William Clemens <http://github.com/wesclemens>

export TERM=xterm-color
ROOT_DIR="$(pwd)/"
LIST=$(git diff-tree -r -M --name-only --no-commit-id ORIG_HEAD HEAD)
ERRORS_BUFFER=""
for file in $LIST
do
    EXTENSION=$(echo "$file" | grep ".php$")
    if [ "$EXTENSION" != "" ]; then
        ERRORS=$(php -l $ROOT_DIR$file 2>&1 | grep "Parse error")
        if [ "$ERRORS" != "" ]; then
            if [ "$ERRORS_BUFFER" != "" ]; then
                ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
            else
                ERRORS_BUFFER="$ERRORS"
            fi
            echo "Syntax errors found in file: $file "
        fi

        # Check for xdebug statments
        ERRORS=$(grep -nH xdebug_ $ROOT_DIR$file | \
                 sed -e 's/^/Found XDebug Statment : /')
        if [ "$ERRORS" != "" ]; then
            if [ "$ERRORS_BUFFER" != "" ]; then
                ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
            else
                ERRORS_BUFFER="$ERRORS"
            fi
        fi
    fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
    echo 
    echo "Found PHP parse errors: "
    echo -e $ERRORS_BUFFER
    echo 
    echo "[ Error! ] PHP parse errors found after merge ! Dammit."
    exit 1
else
    echo "[  Pass  ] No PHP parse errors found."
fi

