.PHONY: help
.DEFAULT_GOAL := help

USER_ID = $(shell id -u)
GROUP_ID = $(shell id -g)
export UID = $(USER_ID)
export GID = $(GROUP_ID)

help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

shell: ## Open dockerized Bash terminal
	@docker compose run --no-deps --rm shell

pycli: ## Open dockerized python command line interface
	@docker compose run --no-deps --rm shell python3

python: ## Launch PYTHON daemon for one directory,  eg make python ./domains-classifier
	WORKING_DIR="$(filter-out $@,$(MAKECMDGOALS))" docker compose up --no-deps --force-recreate python

nodejs: ## Launch NODEJS daemon for one directory,  eg make nodejs ./base-line
	WORKING_DIR="$(filter-out $@,$(MAKECMDGOALS))" docker compose up --no-deps --force-recreate nodejs

reset: ## Reset files permissions for all directory
	@docker compose run --no-deps --rm shell bash -c "find /app -user daemon -exec chown ${UID}:${GID} {} \;"

version-major: ## Create a major version for a directory, e.g. make version-major mapping-tools
	@./bin/version-major.sh "$(filter-out $@,$(MAKECMDGOALS))"

version-minor: ## Create a minor version for a directory, e.g. make version-minor mapping-tools
	@./bin/version-minor.sh "$(filter-out $@,$(MAKECMDGOALS))"

version-patch: ## Create a patch version for a directory, e.g. make version-patch mapping-tools
	@./bin/version-patch.sh "$(filter-out $@,$(MAKECMDGOALS))"

publish: ## Publish all web services
	@./bin/publish.sh

get-version: ## Get the last version of a directory (useful to deployment)
	@git tag | grep "$(filter-out $@,$(MAKECMDGOALS))" | sort -V | tail -1

examples: ## Launch examples for a directory, e.g. make examples mapping-tools
	@npx httpyac --all "$(filter-out $@,$(MAKECMDGOALS))/examples.http"

example-metadata: ## Give an example metadata, from examples.http, e.g. make example-metadata affiliations-tools 3
	@./bin/generate-example-metadata.mjs $(filter-out $@,$(MAKECMDGOALS))

generate-example-tests: ## Convert examples.http to test.hurl, e.g. make generate-example-tests affiliations-tools
	@./bin/generate-example-tests.mjs $(filter-out $@,$(MAKECMDGOALS)) > $(filter-out $@,$(MAKECMDGOALS))/tests.hurl

test: ## Launch tests for a directory, e.g. make test mapping-tools
	@hurl --test "$(filter-out $@,$(MAKECMDGOALS))/tests.hurl"

test-all: ## Launch tests for all directories
	@hurl --test */tests.hurl

test-report: ## Add a test to the tests results, e.g. make test-report affiliations-tools
	@hurl --test "$(filter-out $@,$(MAKECMDGOALS))/tests.hurl" --report-html tests-results

test-report-all: ## Launch tests for all directories
	@rm -rf tests-results
	@hurl --test */tests.hurl --report-html tests-results

# Remove error message about lacking rules for targets' parameters
%:
	@:
