#
# 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
#

%include-2%
COMPONENT_FMRI =		system/management/ansible
COMPONENT_CLASSIFICATION =	System/Administration and Configuration

# This project is a collection of other projects (see tags.yaml), each with its
# own test requirements.  We do not support testing of that (yet).
TEST_STYLE = none

# This is a standalone application.  Since no other Python project depends on
# it we just provide single unversioned package.
SINGLE_PYTHON_VERSION = yes
%hook-no-license%
# https://github.com/ansible/ansible/pull/80385
LICENSE="GPL-3.0-only"
%hook-manifest%
# Do not generate dependencies for usr/bin/ansible-community on
# ansible_collections/__init__.py.  This file does not exist, but pkgdepend
# suggests for unknown reason it is needed.
printf '<transform file path=usr/bin/ansible-community$ -> add pkg.depend.bypass-generate .*/ansible_collections/__init__\\.py>\n' >> "$DISTRIBUTION.p5m"

#
# Some Ansible files are technically broken since they use Python shebang but
# they are not valid Python executable files.  Such files confuses pkgdepend so
# it fails.  Unfortunately, the default bypass transform in transforms/python
# is not enough to make pkgdepend happy, so we need to use a bit bigger hammer
# to completely skip pkgdepend for affected files.
#
# See also:
#	https://docs.ansible.com/ansible/latest/dev_guide/testing/sanity/shebang.html
#	https://github.com/vultr/ansible-collection-vultr/issues/81
#
cat <<-"EOF" >> "$DISTRIBUTION.p5m"

	# Completely skip pkgdepend for few invalid Python files
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/cloudscale_ch/cloud/plugins/modules/ -> add pkg.depend.bypass-generate .* >
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/community/sap_libs/plugins/modules/sap_pyrfc\.py$ -> add pkg.depend.bypass-generate .* >
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/containers/podman/plugins/modules/ -> add pkg.depend.bypass-generate .* >
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/ngine_io/vultr/plugins/modules/ -> add pkg.depend.bypass-generate .* >
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/ngine_io/exoscale/plugins/modules/ -> add pkg.depend.bypass-generate .* >
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/sensu/sensu_go/plugins/modules/ -> add pkg.depend.bypass-generate .* >
	<transform file path=usr/lib/python3\.\d+/vendor-packages/ansible_collections/vultr/cloud/ -> add pkg.depend.bypass-generate .* >

EOF

# This project is a collection of other projects (see tags.yaml), each with its
# own runtime requirements usually specified in separate requirements.txt file.
printf '\n# Automatically generated per collection dependencies and transforms\n' >> "$DISTRIBUTION.p5m"
cat "$SOURCE_DIR/tags.yaml" | egrep -v '^(#| )' | sed -e 's/:$//' | tr '.' '/' | while read d ; do
	# Get collection requirements
	DEPS=$(find "$SOURCE_DIR/ansible_collections/$d/" \
	    -name tests -prune \
	    -o \( -name requirements-test.txt -o -name requirements-dev.txt \) -prune \
	    -o \( -type f -name requirements*.txt -o -name ee-requirements.txt \) -exec cat {} \; -exec echo \; \
		| "$WS_TOP/tools/python-requires" - | grep -v '^ansible$' \
		| sed -e 's|^|/library/python/|' | LC_ALL=C sort -u)
	[[ -n "$DEPS" ]] || continue

	# Check requirements
	if pkg list -q -a $DEPS ; then
		for pkg in $DEPS ; do
			printf 'depend type=require fmri=%s-$(PYV)\n' "${pkg#/}"
		done
	else
		printf '# Missing dependencies for %s:' "$(printf '%s' "$d" | tr '/' '.')" >> "$DISTRIBUTION.p5m"
		for pkg in $DEPS ; do
			pkg list -q -a $pkg || printf ' %s' "${pkg##*/}" >> "$DISTRIBUTION.p5m"
		done
		printf '\n<transform file path=.*/vendor-packages/ansible_collections/%s/ -> drop>\n' "$d" >> "$DISTRIBUTION.p5m"
	fi
done | LC_ALL=C sort -u >> "$DISTRIBUTION.p5m"