#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2023 Marcel Telka
#

%patch% 02-selinux.patch
%patch% 03-test-disable-plugins.patch
%patch% 05-wcwidth.patch
%patch% 06-dev-null.patch
%patch% 07-test_importlib_resources.patch

%include-2%
# ansible-core >= 2.14.0 requires Python >= 3.9
UNSUPPORTED_PYTHON_VERSIONS =
PYTHON_VERSIONS := $(filter-out $(UNSUPPORTED_PYTHON_VERSIONS), $(PYTHON_VERSIONS))

# ansible-core use custom testing (see below)
TEST_STYLE = custom
%include-3%
# Make sure the unversioned generic package depends on unsupported versioned package(s) too.
$(BUILD_DIR)/mkgeneric-python:	PYTHON_VERSIONS := $(UNSUPPORTED_PYTHON_VERSIONS) $(PYTHON_VERSIONS)

# custom testing
# https://docs.ansible.com/ansible/latest/dev_guide/testing_running_locally.html#testing-ansible-core
COMPONENT_TEST_CMD = bin/ansible-test
COMPONENT_TEST_ARGS =
COMPONENT_TEST_ARGS += units
COMPONENT_TEST_ARGS += --python $(PYTHON_VERSION)
COMPONENT_TEST_ARGS += --python-interpreter $(PYTHON)
COMPONENT_TEST_ARGS += --local
COMPONENT_TEST_ARGS += --color no
COMPONENT_TEST_ARGS += --verbose
COMPONENT_TEST_TARGETS =

# This project does not support tox so we need to provide test requirements
# manually.
TEST_REQUIREMENTS += test/lib/ansible_test/_data/requirements/units.txt

# testing fails without HOME environment variable
COMPONENT_TEST_ENV += HOME=/tmp

# The ansible-test is running pytest several times in a row with pytest-xdist
# plugin enabled.  This is hard to normalize properly, so we just pick most
# important lines from the test log and normalize them.
COMPONENT_TEST_TRANSFORMS = \
	"-n -e '/^\[gw/p' \
	| $(COMPONENT_TEST_TRANSFORMER) \
		-e 's/^\[gw.\] \[...%\] //' \
		-e 's/\([^ ]\{1,\}\) \(.*\)$$/\2 \1/' \
		-e 's/\(test_distribution_version\[stdin\)[0-9]*/\1/' \
		-e 's|$(@D)|\$$(@D)|g' \
	| $(SORT) \
	| $(COMPONENT_TEST_TRANSFORMER) -e ''"
%hook-no-license%
# https://github.com/ansible/ansible/pull/80385
LICENSE="GPL-3.0-only"
%hook-manifest%
# To make sure ansible-core binaries are available when the python mediator is
# set to an usupported Python version we need fake versioned ansible-core
# package(s).  We do not use SINGLE_PYTHON_VERSION for ansible-core because
# ansible depends on ansible-core and with SINGLE_PYTHON_VERSION here the
# switch of ansible/ansible-core pair to newer Python version would be a bit
# harder.
UNSUPPORTED_PYTHON_VERSIONS=$($GMAKE print-value-UNSUPPORTED_PYTHON_VERSIONS)
PYTHON_VERSION=$($GMAKE print-value-PYTHON_VERSION)
for ver in $UNSUPPORTED_PYTHON_VERSIONS ; do
	v=${ver//.}
	sed -e '/path=usr\/lib/,$d' \
		-e 's/^file \(path=usr\/bin\/\)\(.*\)-\$(PYVER)$/link mediator=python mediator-version='"$ver"' \1\2 target=\2-'"$PYTHON_VERSION"'/' \
		-e 's/\$(PYV)/'"$v"'/' < "$DISTRIBUTION-PYVER.p5m" > "$DISTRIBUTION-$v.p5m"
	printf "\ndepend type=require fmri=library/python/ansible-core-${PYTHON_VERSION//.}\n" >> "$DISTRIBUTION-$v.p5m"
	git add "$DISTRIBUTION-$v.p5m"
done