#
# 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% 01-gmake.patch
%patch% 02-posix-rm.patch

%include-2%
# https://github.com/encukou/py3c/issues/56
COMPONENT_ARCHIVE_URL =		\
	https://github.com/encukou/py3c/archive/refs/tags/v$(HUMAN_VERSION).tar.gz
%include-3%
# see $(SOURCE_DIR)/Makefile
# and https://github.com/encukou/py3c/issues/24#issuecomment-1447981119
COMPONENT_POST_INSTALL_ACTION += \
	$(MKDIR) $(PROTO_DIR)$(PKG_CONFIG_PATH.$(BITS)) ; \
	$(GSED) -e 's:@includedir@:$(shell $(PYTHON) -c "from sysconfig import get_paths; print(get_paths()[\"include\"])"):' \
		-e 's|^Cflags:.*$$|& -I$${includedir}/py3c|' \
		$(@D)/py3c.pc.in \
		> $(PROTO_DIR)$(PKG_CONFIG_PATH.$(BITS))/py3c.pc-$(PYTHON_VERSION) ;

# Normalize special PYV case
COMPONENT_TEST_TRANSFORMS += "-e 's/\(cpython-\)$(shell echo $(PYTHON_VERSION) | tr -d .)/\1\$$(PYV)/'"

# Make sure include files are included in right packages
GENERATE_EXTRA_CMD += | \
	$(GSED) -e 's|^.*usr/include/python\(3.[0-9]*\)[^0-9]|$$(PYTHON_\1_ONLY)&|'
# Replace Python version in pkgconfig file names
GENERATE_EXTRA_CMD += | \
	$(GSED) -e 's/\(py3c\.pc-\)3\.[0-9]*$$/\1$$(PYVER)/' | uniq
%hook-no-license%
cat "$SOURCE_DIR/LICENSE.MIT" > "$DISTRIBUTION.license"
%hook-manifest%
# Create mediated symlinks
printf '<transform file path=(usr/.*/pkgconfig/)(py3c\\.pc)-(3\\.\\d+)$ -> emit link path=%%<1>%%<2> target=%%<2>-%%<3> mediator=python mediator-version=%%<3> >\n' >> "$DISTRIBUTION-PYVER.p5m"