#!/usr/bin/env bash

PHPFILES=$(git diff --cached --name-only --diff-filter=ACMR "*.php" | sed 's| |\\ |g')

if [ ! -z "$PHPFILES" ]; then
    PHP_CS_FIXER="vendor/bin/php-cs-fixer"

    if [ -x "$PHP_CS_FIXER" ]; then
        echo "Running PHP-CS-Fixer..."
        $PHP_CS_FIXER fix --config=".php_cs.dist" --using-cache=no $PHPFILES;
        git add $PHPFILES;
    else
        echo ""
        echo "Please install php-cs-fixer, e.g.:"
        echo ""
        echo "  composer require friendsofphp/php-cs-fixer"
        echo ""
        exit 1
    fi
fi

FILES=$(git diff --cached --name-only | sed 's| |\\ |g')

if [ -z "$FILES" ]; then
    echo "No files left to commit after formatting. Aborting"
    exit 1;
fi
