#!/usr/bin/env bash

set -e

if [ -f ".git/MERGE_HEAD" ]; then
  # Don't run when merging
  exit
fi

php_files=$(git diff --cached --name-only --diff-filter=ACMR "*.php" | sed 's| |\\ |g' | grep -Ev 'config/(bundles.php|preload.php|secrets/)' || [[ $? == 1 ]])

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

  if [ -x "$php_cs_fixer" ]; then
    echo "Running PHP-CS-Fixer..."
    docker compose run -T --rm --no-deps php $php_cs_fixer fix --config=".php-cs-fixer.dist.php" --using-cache=no $php_files
    git add $php_files
  else
    echo "\

[ERROR] Commit Failed. Please install dependencies with:

    ./composer install
        "
    exit 1
  fi
fi
