#!/usr/bin/env bash

# Leave if GrumPHP ain't installed.
if [[ ! -x vendor/bin/grumphp ]]; then
  exit 0
fi

# Fetch the git diff and format it as command input.
DIFF=$(git -c diff.mnemonicprefix=false -c diff.noprefix=false --no-pager diff -r -p -m -M --full-index --no-color --staged | cat)

# Get the web container name.
CONTAINER=$(cat .ddev/.ddev-docker-compose-full.yaml | grep ' container_name: ddev-.*-web$' | tr -s ' ' | cut -d ' ' -f 3)

# Run GrumPHP.
docker exec -e DIFF -u "${USER}:${USER}" -i $CONTAINER bash <<'COMMAND'
  (cd "./" && printf "%s\n" "$DIFF" | vendor/bin/grumphp git:pre-commit --skip-success-output)
COMMAND
