ROLE ?= authelia
FIXTURE ?= minimal
ROOT := $(CURDIR)/roles/$(ROLE)/tmp
ANSIBLE_ARGS := roles/$(ROLE)/tests/main.yaml --inventory localhost, --extra-vars fixture=$(FIXTURE)

ifneq ($(shell command -v podman 2>/dev/null),)
export PODMAN_COMPOSE_WARNING_LOGS := false
export CNT_BIN ?= podman
else ifneq ($(shell command -v docker 2>/dev/null),)
export CNT_BIN ?= docker
else
export CNT_BIN ?= echo "ERROR: install either Podman or Docker to run validations"; exit 1;
endif

.PHONY: lint
lint:
	@ansible-lint

.PHONY: check
check: clean
	@ansible-playbook $(ANSIBLE_ARGS) --check

.PHONY: test
test: clean
	@ansible-playbook $(ANSIBLE_ARGS)

.PHONY: validate
validate:
	@if [ ! -d "$(ROOT)" ]; then \
		$(MAKE) --no-print-directory test; \
	fi
	@$(CNT_BIN) compose --project-directory "$(ROOT)" config -q
	@if [ -f "roles/$(ROLE)/Makefile" ]; then \
		$(MAKE) --no-print-directory -C "roles/$(ROLE)" validate; \
	fi

.PHONY: build
build:
	@ansible-galaxy collection build --output-path build

.PHONY: clean
clean:
	@if [ -d "$(ROOT)" ]; then \
		$(CNT_BIN) compose --project-directory "$(ROOT)" down --volumes --remove-orphans ||: ; \
	fi
	@rm -rf "$(ROOT)"
