#!/usr/bin/make -f
# -*- makefile -*-

# 3.11
pybasever  := 3.11
# 311 / -internal
pyshortver := -internal

version				:= 21.3.1
_prefix				:= /opt/alt/python$(pyshortver)
_sysconfdir			:= ${_prefix}/etc
_bindir				:= ${_prefix}/bin
_libdir				:= ${_prefix}/lib
_datadir			:= ${_prefix}/share
pylibdir			:= ${_libdir}/python$(pybasever)
srcname				:= pip
__alt_pythonXY		:= ${_bindir}/python$(pybasever)
buildroot			:= ${CURDIR}/debian/tmp
alt_pythonXY_sitelib	:= $(shell $(__alt_pythonXY) -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(True))")
python_wheelname	:= ${srcname}-${version}-py3-none-any.whl
python_wheeldir		:= ${_datadir}/python-wheels
python_record		:= ${alt_pythonXY_sitelib}/${srcname}-${version}.dist-info/RECORD
alt_bashcompdir		:= $(_datadir)/bash-completion/completions

export PYBUILD_DISABLE_python2=1
export PYTHONWARNINGS=d
export ALTNAME=alt-python$(pyshortver)-${srcname}
export PYTHONPATH="${_bindir}:${_libdir}:${alt_pythonXY_sitelib}:${python_wheeldir}"
export bashcompdir=${_sysconfdir}/bash_completion.d
export CFLAGS="-I/opt/alt/python$(pyshortver)/include"

%:
	dh $@

# no versioned easy_install scripts
export SETUPTOOLS_DISABLE_VERSIONED_EASY_INSTALL_SCRIPT = yes

# versioned easy_install scripts
export XPY3VERSIONS = 3.5 3.6 3.7 $(pybasever)

override_dh_auto_build:
	patch -p1 < debian/patches/remove-existing-dist-only-if-path-conflicts.patch
	patch -p1 < debian/patches/debian-dummy-certifi.patch
	patch -p1 < debian/patches/nowarn-pip._internal.main.patch
	patch -p1 < debian/patches/no-version-warning.patch

	# this goes together with patch4
	rm src/pip/_vendor/certifi/*.pem

	# Do not use furo as HTML theme in docs
	# furo is not available in Fedora
	sed -i '/html_theme = "furo"/d' docs/html/conf.py

	# towncrier extension for Sphinx is not yet available in Fedora
	sed -i '/"sphinxcontrib.towncrier",/d' docs/html/conf.py

	# tests expect wheels in here
	ln -s $(python_wheeldir) tests/data/common_wheels

	# Remove shebang from files in bundled chardet
	grep -lr "^#\!/usr/bin/env python" src/pip/_vendor/chardet/ | xargs sed -i "1d"

	# Remove windows executable binaries
	rm -v src/pip/_vendor/distlib/*.exe
	sed -i '/\.exe/d' setup.py

	${__alt_pythonXY} setup.py bdist_wheel

override_dh_auto_install:
	${__alt_pythonXY} dist/${python_wheelname}/pip install \
		-I dist/${python_wheelname} \
		--root ${buildroot} \
		--no-deps \
		--disable-pip-version-check \
		--progress-bar off \
		--verbose \
		--ignore-installed \
		--no-warn-script-location \
		--no-index \
		--no-cache-dir \
		--find-links dist \
		'pip==$(version)'

	# before we ln -s anything, we apply Source10 patch to all pips:
	for PIP in ${buildroot}${_bindir}/pip*; do \
	  patch -p1 --no-backup-if-mismatch $${PIP} < debian/patches/pip-allow-different-versions.patch; \
	done

	mkdir -p ${buildroot}${alt_bashcompdir}
	PYTHONPATH=${buildroot}${alt_pythonXY_sitelib} \
		${buildroot}${_bindir}/pip completion --bash \
	    > ${buildroot}${alt_bashcompdir}/pip3

	# Make bash completion apply to all the 5 symlinks we install
	sed -i -e "s/^\\(complete.*\\) pip\$$/\\1 pip pip{,-}{3,$(pybasever)}}/" \
	    -e s/_pip_completion/_pip3_completion/ \
	    ${buildroot}${alt_bashcompdir}/pip3


	# Provide symlinks to executables to comply with Fedora guidelines for Python
	ln -s ./pip$(pybasever) ${buildroot}${_bindir}/pip-$(pybasever)
	ln -s ./pip-$(pybasever) ${buildroot}${_bindir}/pip-3


	# Make sure the INSTALLER is not pip, otherwise Patch2 won't work
	# TODO Maybe we should make all our python packages have this?
	#if [ -d ${buildroot}${pylibdir}/site-packages ]; then \
	#	mv ${buildroot}${pylibdir}/site-packages ${buildroot}${alt_pythonXY_sitelib}; \
	#fi
	echo apt > ${buildroot}${alt_pythonXY_sitelib}/pip-${version}.dist-info/INSTALLER

	mkdir -p ${buildroot}${python_wheeldir}
	install -p dist/${python_wheelname} -t ${buildroot}${python_wheeldir}
ifeq ($(pyshortver),-internal)
	# ensurepip (patched to use _WHEEL_PKG_DIR) scans for both pip and setuptools;
	# copy setuptools from _bundled so venv creation works without a separate
	# alt-python$(pyshortver)-setuptools-wheel package.
	install -p $(wildcard ${pylibdir}/ensurepip/_bundled/setuptools-*.whl) \
		-t ${buildroot}${python_wheeldir}
endif

	dh_movefiles -palt-python$(pyshortver)-pip \
		.${_bindir}/* \
		.${alt_pythonXY_sitelib}/* \
		.${alt_bashcompdir}/*

ifeq ($(pyshortver),-internal)
	dh_movefiles -palt-python$(pyshortver)-pip-wheel \
		.${python_wheeldir}/${python_wheelname} \
		.${python_wheeldir}/setuptools-*.whl
else
	dh_movefiles -palt-python$(pyshortver)-pip-wheel \
		.${python_wheeldir}/${python_wheelname}
endif

override_dh_auto_clean:
override_dh_auto_test:

override_dh_builddeb:
	dh_builddeb
	if [ $(shell find $(buildroot) -type f | wc -l) -gt 0 ]; then \
		echo "Installed but unpackaged:"; \
		find $(buildroot) -type f -exec echo "{}" \; | sed -e 's#$(buildroot)##g'; \
		exit 1; \
	fi
