#!/bin/sh -eu

# Project directory in guest and CMD to execute PHP commands from host.
dir="/var/www/html"
phpCmd="docker compose exec -w ${dir} -T php";

echo ""
echo "--==( Setting up git pre-commit hook )==--"

# Create git pre-commit hook file.
HOOK_FILE="/var/www/html/.git/hooks/pre-commit"

if [ -f $HOOK_FILE ]; then
  rm -f $HOOK_FILE
fi

touch $HOOK_FILE
chmod +x $HOOK_FILE

cat <<EOT >> $HOOK_FILE
#!/usr/bin/env -S bash

ERRORS=0

echo "==========================================================="
echo "#             APPLYING AUTOMATIC ADJUSTMENTS              #"
echo "==========================================================="

SRC_FILES=""
TEST_FILES=""

for file in \$(git diff --name-only --cached)
do
  if [ "\$(echo \$file | cut -d/ -f1)" = "src" ]; then
      SRC_FILES="\$SRC_FILES $dir/\$file"
  elif [ "\$(echo \$file | cut -d/ -f1)" = "tests" ]; then
      TEST_FILES="\$TEST_FILES $dir/\$file"
  fi
done

if [ -n "\$SRC_FILES" ]; then
  echo ""
  echo "---==( Automatically adjusting src files )==---"
  echo "[APPLYING PSR-12 RULESET]"
  $phpCmd $dir/vendor/bin/phpcbf --extensions=php --standard=PSR12 \$SRC_FILES
  echo "[APPLYING CUSTOM RULESET]"
  $phpCmd $dir/vendor/bin/phpcbf --extensions=php --standard=qa/phpcs/src.xml \$SRC_FILES
fi

if [ -n "\$TEST_FILES" ]; then
  echo ""
  echo "---==( Automatically adjusting test files )==---"
  echo "[APPLYING PSR-12 RULESET]"
  $phpCmd $dir/vendor/bin/phpcbf --extensions=php --standard=PSR12 \$TEST_FILES
  echo "[APPLYING CUSTOM RULESET]"
  $phpCmd $dir/vendor/bin/phpcbf --extensions=php --standard=qa/phpcs/tests.xml \$TEST_FILES
fi

if [ -z "\$SRC_FILES\$TEST_FILES" ]; then
  echo ""
  echo "No fixable errors found."
  echo ""
fi

echo "==========================================================="
echo "#                  SCANNING SOURCE FILES                  #"
echo "==========================================================="
echo ""

echo "---==( Executing PHPCS )==---"
if ! $phpCmd $dir/vendor/bin/phpcs --standard=PSR12 $dir/src; then
  ERRORS=1
fi
if ! $phpCmd $dir/vendor/bin/phpcs --extensions=php --standard=qa/phpcs/src.xml $dir/src; then
  ERRORS=1
fi
if ! $phpCmd $dir/vendor/bin/phpcs --extensions=phtml --standard=qa/phpcs/templates.xml $dir/src; then
  ERRORS=1
fi
echo ""

echo "---==( Executing PHPMD )==---"
if ! $phpCmd $dir/vendor/bin/phpmd $dir/src text $dir/qa/phpmd/src.xml 2>&1 | grep -v '^Deprecated:'; then
  ERRORS=1
fi
echo ""

echo "---==( Executing PHPStan )==---"
if ! $phpCmd $dir/vendor/bin/phpstan analyse --memory-limit 512M -c $dir/qa/phpstan/phpstan.neon $dir/src; then
  ERRORS=1
fi
echo ""

echo "==========================================================="
echo "#                   SCANNING TEST FILES                   #"
echo "==========================================================="
echo ""

echo "---==( Executing PHPCS )==---"
if ! $phpCmd $dir/vendor/bin/phpcs --extensions=php --standard=PSR12 $dir/tests; then
  ERRORS=1
fi
if ! $phpCmd $dir/vendor/bin/phpcs --extensions=php --standard=qa/phpcs/tests.xml $dir/tests; then
  ERRORS=1
fi
echo ""

echo "---==( Executing PHPMD )==---"
if ! $phpCmd $dir/vendor/bin/phpmd $dir/tests text $dir/qa/phpmd/tests.xml 2>&1 | grep -v '^Deprecated:'; then
  ERRORS=1
fi
echo ""

echo "---==( Executing PHPStan )==---"
if ! $phpCmd $dir/vendor/bin/phpstan analyse --memory-limit 512M -c $dir/qa/phpstan/phpstan.neon $dir/tests; then
  ERRORS=1
fi
echo ""

echo "==========================================================="
echo "#                   RUNNING UNIT TESTS                    #"
echo "==========================================================="
echo ""

if ! $phpCmd $dir/vendor/bin/phpunit -c $dir/phpunit.xml $dir/tests/Unit/; then
  ERRORS=1
fi

if [ "\${ERRORS}" = "1" ]; then
  exec < /dev/tty
  read -p "Continue commit despite remaining errors? (y/n)?" continueCommit
  case "\$continueCommit" in
    y|Y ) exit 0;;
    * ) echo "Aborting."; exit 1;;
  esac
fi

EOT

echo OK
exit 0
