#!/bin/sh

# Redirect output to stderr.
exec 1>&2

ROOT="$PWD"
PHP_CS_FIXER="${ROOT}/vendor/bin/php-cs-fixer"
PHP_CS_CONFIG="${ROOT}/.php_cs"

if [ ! -f "$PHP_CS_CONFIG" ]; then
    echo "Please run 'git commit' from the root directory of the project"
    exit 1
fi

if [ ! -x "$PHP_CS_FIXER" ]; then
    echo "Please run 'composer install' first"
    exit 1
fi

git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
    $PHP_CS_FIXER fix --config=$PHP_CS_CONFIG --verbose "$line";
    git add "$line";
done
