#!/bin/bash -l
git config --global --add safe.directory /var/www/html
ghprbActualCommit=$(git rev-parse HEAD)
masterCommit=$(git rev-parse HEAD~1)
echo Checking revision ${ghprbActualCommit}
echo against ${masterCommit}
changedFiles="$(git diff-tree --no-commit-id --name-only -r ${ghprbActualCommit} ${masterCommit})"
echo Changed files are: ${changedFiles}
IFS=$'\n' read -rd '' -a y <<<"$changedFiles"

for i in "${y[@]}"
do
    if [ -f ${i} ] && ([ ${i: -4} == ".php" ] || [ ${i: -4} == ".inc" ] || [ ${i: -6} == ".phtml" ]); then
        echo "Linting $i";
        ant -DchangedFile=${i} lint-changes
        antReturnCode=$?
        if [ ${antReturnCode} -ne 0 ]; then
            exit 1
        fi
    else
        echo "Not a php/inc/phtml file. Skipping lint on $i"
    fi
done
