#!/usr/bin/env bash

echo "php-cs-fixer pre commit hook start"

PHP_CS_FIXER="vendor/bin/php-cs-fixer"
HAS_PHP_CS_FIXER=false

if [ -x $PHP_CS_FIXER ]; then
    HAS_PHP_CS_FIXER=true
fi

if $HAS_PHP_CS_FIXER; then
    FILES=`git status --porcelain | grep -E  '^\s*[AM]\s+.*\.php$' | cut -c 4- | tr '\n' ' '`
    if [ -z "$FILES" ]
	then
		  echo "No php files found in commit."
	else
		  echo ${FILES}
		  $PHP_CS_FIXER fix --config=.php-cs-fixer.php --verbose ${FILES}
		  git add ${FILES}
	fi
else
    echo ""
    echo "Please install php-cs-fixer, e.g.:"
    echo ""
    echo "  composer require --dev fabpot/php-cs-fixer:dev-master"
    echo ""
    exit 0
fi

echo "php-cs-fixer pre commit hook finish"
