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

.NOTPARALLEL: test test_podman_debian test_podman_gentoo test_local

build: depends

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

test: depends test_files test_podman_gentoo test_podman_debian lint

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 ; }'
	# OK

test_podman_gentoo: depends
	# Run molecule tests (using Podman with Gentoo image).
	rm -rfv testlogs
	install -d -m 0777 testlogs
	bin/podman-gentoo-ansible-molecule "molecule test --all >/workdir/testlogs/molecule-gentoo-playbooks.log 2>&1" && echo "OK: %" || { cat testlogs/molecule-gentoo-playbooks.log ; echo "ERROR: %" ; exit 1 ; }
	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 "OK: %" || { cat testlogs/molecule-gentoo-$$(basename %).log ; echo "ERROR: %" ; exit 1 ; }'
	# OK

test_podman_debian: depends
	# Run molecule tests (using Podman with Debian image).
	rm -rfv testlogs
	install -d -m 0777 testlogs
	bin/podman-debian-ansible-molecule "molecule test --all >/workdir/testlogs/molecule-debian-playbooks.log 2>&1" && echo "OK: %" || { cat testlogs/molecule-debian-playbooks.log ; echo "ERROR: %" ; exit 1 ; }
	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 "OK: %" || { cat testlogs/molecule-debian-$$(basename %).log ; echo "ERROR: %" ; exit 1 ; }'
	# OK

test_local: depends
	# Run molecule tests.
	rm -rfv testlogs
	install -d -m 0777 testlogs
	molecule test --all >testlogs/molecule-local-playbooks.log 2>&1 && echo "OK: %" || { cat testlogs/molecule-local-playbooks.log ; echo "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 "OK: %" || { cat $${TESTLOGS_DIR}/molecule-local-$$(basename %).log ; echo "ERROR: %" ; exit 1 ; }'
	# OK

lint: depends
	# Run ansible-lint.
	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
	# OK
