#
# 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-test-disable-failing.patch
%patch% 02-test-proxy.patch

%include-1%
# Force setup.py build style because we need to pass additional --openssl-dir
# and there seems to be no way how to do that with the default pyproject build
# style.
BUILD_STYLE = setup.py
%include-2%
# Install common files to /usr
PYTHON_DATA = $(USRDIR)
%include-3%
# Make sure we compile and link with proper openssl version
LDFLAGS += -L$(OPENSSL_LIBDIR)
LDFLAGS += -R$(OPENSSL_LIBDIR)
COMPONENT_BUILD_SETUP_PY_ARGS += --openssl-dir=$(OPENSSL_PREFIX)

# Rename pycurl documentation to contain version number
COMPONENT_POST_INSTALL_ACTION += \
	$(MV) $(PROTOUSRSHAREDOCDIR)/pycurl $(PROTOUSRSHAREDOCDIR)/pycurl-$(PYTHON_VERSION) ;

# Compile extra libraries for tests
COMPONENT_PRE_TEST_ACTION += PATH=$(PATH.gnu) make -C $(@D)/tests/fake-curl/libcurl ;

# https://github.com/pycurl/pycurl/issues/876
PYTEST_ADDOPTS += --deselect tests/pause_test.py::PauseTest::test_pause_via_call
PYTEST_ADDOPTS += --deselect tests/pause_test.py::PauseTest::test_pause_via_return

# Replace Python version in pycurl documentation directory name
GENERATE_EXTRA_CMD += | \
	$(GSED) -e 's/\(usr\/share\/doc\/pycurl-\)3\.[0-9]*/\1$$(PYVER)/' | uniq
%hook-manifest%
# ... and create mediated symlink for it.
printf '\nlink path=usr/share/doc/pycurl target=pycurl-$(PYVER) mediator=python mediator-version=$(PYVER)\n' >> "$DISTRIBUTION-PYVER.p5m"

# License
(
	cat "$SOURCE_DIR//README.rst" | gsed -e '0,/^License/d' -n -e '/^    /,${/^[^ ]/,$d ; s/^    // ; p}'
	printf '\n--- COPYING-LGPL ----------------------------------------------------------\n\n'
	cat "$SOURCE_DIR/COPYING-LGPL"
	printf '\n\n--- COPYING-MIT -----------------------------------------------------------\n\n'
	cat "$SOURCE_DIR/COPYING-MIT"
) > "$DISTRIBUTION.license"
sed -i -e '/^COMPONENT_LICENSE_FILE/d' Makefile
git add "$DISTRIBUTION.license"
LICENSE=
detect_license LICENSE "$DISTRIBUTION.license"
gsed -i -e 's/^\(COMPONENT_LICENSE =\t*\)[^\t]*$/\1'"$LICENSE"'/' Makefile
git add Makefile