JOBS := $(patsubst -j%,%,$(filter -j%,$(MAKEFLAGS)))
ifeq ($(JOBS),)
	JOBS := 1
endif

ifeq ($(ROLE),)
TEST_DEPS := test_files test_podman_gentoo test_podman_debian lint
else
TEST_DEPS := test_role
endif

.NOTPARALLEL: test_local depends build deploy test_init test_init_podman_gentoo test_init_podman_debian clean mostlyclean
.PHONY: test test_role

build: depends
	# Build.
	command -v yq >/dev/null 2>&1
	bin/ansible-galaxy-collection-build
	# OK

deploy:
	# Deploy.
	command -v yq >/dev/null 2>&1
	bin/ansible-galaxy-collection-publish
	# OK

clean:
	# Clean dependencies.
	rm -rfv "$$(pwd)/.ansible/collections"

mostlyclean:
	# Clean test logs.
	rm -rfv testlogs

depends:
	# Install dependencies.
	ANSIBLE_COLLECTIONS_PATH="$$(pwd)/.ansible/collections" ansible-galaxy collection install -p "$$(pwd)/.ansible/collections" -r requirements.yml

test: $(TEST_DEPS)

test_init: mostlyclean depends
	install -d -m 0777 testlogs

test_init_podman_gentoo:
	# Podman Gentoo: Check if podman image is built, or build.
	bin/podman-gentoo-ansible-molecule "echo OK"

test_init_podman_debian:
	# Podman Debian: Check if podman image is built, or build.
	bin/podman-debian-ansible-molecule "echo OK"

test_role: test_init
	bin/podman-gentoo-ansible-molecule "cd roles/$(ROLE) && molecule test --all"
	bin/podman-debian-ansible-molecule "cd roles/$(ROLE) && molecule test --all"

test_files:
	# Verify all roles have a basic set of files.
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -d "%/defaults" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -d "%/tasks" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -d "%/meta" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -d "%/handlers" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -d "%/molecule/default" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -f "%/defaults/main.yml" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -f "%/tasks/main.yml" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -f "%/meta/main.yml" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -f "%/handlers/main.yml" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -f "%/molecule/default/molecule.yml" || { echo "ERROR: %" ; exit 1 ; }'
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'test -f "%/README.md" || { echo "ERROR: %" ; exit 1 ; }'

test_playbooks: test_init test_podman_gentoo_playbooks test_podman_debian_playbooks

test_podman_gentoo: test_init test_podman_gentoo_playbooks test_podman_gentoo_roles

test_podman_gentoo_playbooks: test_init test_init_podman_gentoo
	# Podman Gentoo: Running molecule tests for playbooks.
	bin/podman-gentoo-ansible-molecule "molecule test --all >/workdir/testlogs/molecule-gentoo-playbooks.log 2>&1" && echo -n "- Gentoo playbooks OK" || { cat testlogs/molecule-gentoo-playbooks.log ; echo "Gentoo playbooks ERROR" ; exit 1 ; }

test_podman_gentoo_roles: test_init test_init_podman_gentoo
	# Podman Gentoo: Running molecule tests for playbooks.
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'bin/podman-gentoo-ansible-molecule "cd % && molecule test --all >/workdir/testlogs/molecule-gentoo-$$(basename %).log 2>&1" && echo -n "- Gentoo role OK: % " || { cat testlogs/molecule-gentoo-$$(basename %).log ; echo "Gentoo role ERROR: %" ; exit 1 ; }'

test_podman_debian: test_init test_podman_debian_playbooks test_podman_debian_roles

test_podman_debian_playbooks: test_init test_init_podman_debian
	# Podman Debian: Running molecule tests for playbooks.
	bin/podman-debian-ansible-molecule "molecule test --all >/workdir/testlogs/molecule-debian-playbooks.log 2>&1" && echo -n "- Debian playbooks OK" || { cat testlogs/molecule-debian-playbooks.log ; echo "Debian playbooks ERROR" ; exit 1 ; }

test_podman_debian_roles: test_init test_init_podman_debian
	# Podman Debian: Running molecule tests for roles.
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'bin/podman-debian-ansible-molecule "cd % && molecule test --all >/workdir/testlogs/molecule-debian-$$(basename %).log 2>&1" && echo -n "- Debian role OK: % " || { cat testlogs/molecule-debian-$$(basename %).log ; echo "ERROR: %" ; exit 1 ; }'

test_local: test_init
	# Running molecule tests.
	molecule test --all >testlogs/molecule-local-playbooks.log 2>&1 && echo "Playbooks OK" || { cat testlogs/molecule-local-playbooks.log ; echo "Playbooks ERROR" ; exit 1 ; }
	find roles -maxdepth 1 -mindepth 1 -type d | xargs -P$(JOBS) -I % sh -c 'export TESTLOGS_DIR="$$(pwd)/testlogs" && export ANSIBLE_ROLES_PATH="$$(pwd)/roles" && export ANSIBLE_COLLECTIONS_PATH="$$(pwd)/.ansible/collections" && cd % && molecule test --all >"$${TESTLOGS_DIR}/molecule-local-$$(basename %).log" 2>&1 && echo "Role OK: %" || { cat $${TESTLOGS_DIR}/molecule-local-$$(basename %).log ; echo "Role ERROR: %" ; exit 1 ; }'
	# OK

lint: test_init
	# Running ansible-lint.
	@ansible-lint --version
	ANSIBLE_LINT_NODEPS=1 ANSIBLE_ROLES_PATH="$$(pwd)/roles" ANSIBLE_COLLECTIONS_PATH="$$(pwd)/.ansible/collections" ANSIBLE_INVENTORY=.ansible-lint-inventory ANSIBLE_VAULT_PASSWORD=x ansible-lint -qq --strict --exclude "$$(pwd)/.ansible/collections"
