.DEFAULT_GOAL := help

.PHONY: help setup env install configure up health test check php

help:
	@printf '%s\n' \
	  'make setup   Create .env, install Node packages and configure Drupal.' \
	  'make up      Start the realtime gateway in the foreground.' \
	  'make health  Verify that the configured gateway is ready.' \
	  'make test    Run the Node.js test suite.' \
	  'make check   Run lint, tests and dependency audit.' \
	  'make php     Run Drupal coding standards and PHPStan.'

setup: env install configure

env:
	@bash bin/init-env.sh

install: env
	@npm ci

configure: env
	@bash bin/configure-local.sh

up: env
	@npm start

health: env
	@set -a; . ./.env; set +a; \
	  curl --fail --silent --show-error \
	  -H "NodejsServiceKey: $$NODEJS_SERVICE_KEY" \
	  "http://$${NODEJS_HOST}:$${NODEJS_PORT}/nodejs/health/check" | \
	  grep -q '"ready":true'
	@echo 'Gateway is ready.'

test:
	@npm test

check:
	@npm run lint
	@npm test
	@npm audit --omit=dev

php:
	@../../../../vendor/bin/phpcs --standard=Drupal,DrupalPractice --ignore=node_modules \
	  --extensions=php,module,inc,install .
	@../../../../vendor/bin/phpstan analyse -c phpstan.neon
