#! /usr/bin/make -f

SHELL = /bin/bash

# all versions
PYVERS	:= $(shell pyversions -vs)
VER	:= $(shell /usr/bin/python -c 'import sys; print sys.version[:3]')
DEBVERS := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')

twversion := $(subst twisted-,,$(notdir $(CURDIR)))

include /usr/share/python/python.mk

ifeq (,$(findstring nodocs,$(DEB_BUILD_OPTIONS)))
  with_docs=yes
endif
with_docs=

build: build-stamp
build-arch: build-stamp
build-indep: build-stamp
build-indep: build-stamp
build-stamp: $(PYVERS:%=build-python%)
	touch $@
build-python%:
	python$* setup.py build
	python$*-dbg setup.py build
	touch $@

clean:
	rm -rf *-stamp build-python* build tmp
	rm -rf docs/_build build-doc
	rm -rf apidocs
	rm -rf $(addprefix debian/,$(packages)) debian/files debian/substvars
	rm -rf _trial_temp test.log
	rm -f twisted/plugins/dropin.cache
	find -name '__pycache__' -type d | xargs -r rm -rf
	find . -name "*.pyc" |xargs -r rm
	dh_clean

install: build-stamp install-prereq $(PYVERS:%=install-python%) install-nover

install-prereq: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

install-python%: install-prereq
	: # python-twisted-core
	python$* setup.py install --root=debian/python-twisted-core --install-layout=deb

	-find debian/python-twisted-core -name '*.py[co]' | xargs rm -f
	rm -rf debian/python-twisted-core$(call py_libdir,$*)/twisted/topfiles
	rm -rf debian/python-twisted-core$(call py_libdir,$*)/Twisted-$(twversion).egg-info

	python$* setup.py install --root=tmp --install-layout=deb
	cp -r tmp$(call py_libdir,$*)/Twisted-*.egg-info debian/python-twisted-core$(call py_libdir,$*)/

	find debian/python-twisted-core -name 'topfiles' -type d | xargs -r rm -rf

	: # python-twisted
	mkdir -p debian/python-twisted/$(call py_libdir,$*)

	: # python-twisted-bin
	dh_movefiles -ppython-twisted-bin \
	  --sourcedir=debian/python-twisted-core \
	    $(call py_libdir,$*)/twisted/python/_sendmsg.so \
	    $(call py_libdir,$*)/twisted/test/raiser.so

	: # python-twisted-bin-dbg
	python$*-dbg setup.py install --root=debian/python-twisted-bin-dbg --no-compile --install-layout=deb
	find debian/python-twisted-bin-dbg ! -type d ! -name '*_d.so' | xargs rm -f
	find debian/python-twisted-bin-dbg -depth -empty -exec rmdir {} \;

	: # Replace all '#!' calls to python with /usr/bin/python
	: # and make them executable
	for i in `find debian/python-twisted-* -mindepth 3 -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

install-nover:
	: # python man pages
	mkdir -p debian/python-twisted-core/usr/share/man/man1
	for i in \
	  docs/core/man/{manhole,pyhtmlizer,tap2deb,tap2rpm,trial,twistd}.1 \
	  docs/conch/man/{cftp,ckeygen,conch,tkconch}.1 \
	  docs/mail/man/mailmail.1; \
	do \
	  cp $$i debian/python-twisted-core/usr/share/man/man1/.; \
	done
	mv debian/python-twisted-core/usr/bin/cftp \
		debian/python-twisted-core/usr/bin/conchftp
	mv debian/python-twisted-core/usr/share/man/man1/cftp.1 \
		debian/python-twisted-core/usr/share/man/man1/conchftp.1
	sed -i '/^\.Nm /s/cftp/conchftp/;s/CFTP/CONCHFTP/' \
		debian/python-twisted-core/usr/share/man/man1/conchftp.1

ifeq ($(with_docs),yes)
	: # twisted-doc
	mkdir -p debian/twisted-doc/usr/share/doc/twisted-doc
	cp -a docs/core/* debian/twisted-doc/usr/share/doc/twisted-doc/
	-find debian/twisted-doc -type d -name man | xargs rm -rf
endif

binary-indep: build-indep install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i

	rm -rf debian/python-twisted-runner-dbg/usr/share/doc/python-twisted-runner-dbg
	ln -s python-twisted-runner \
	    debian/python-twisted-runner-dbg/usr/share/doc/python-twisted-runner-dbg

ifeq ($(with_docs),yes)
	cp -ua apidocs debian/twisted-doc/usr/share/doc/twisted-doc/
	# Replace links to Twisted website API doc by links to the local doc. We
	# need to compute path to the API which goes up enough in the folder
	# hierarchy to access the doc.
	for i in $$(cd debian/twisted-doc/usr/share/doc/twisted-doc/ && \
	  grep -rl "http://twistedmatrix.com/documents/$(twversion)/api/" .); do \
	  path="$$(echo $$i | sed "s:[^/]::g" | sed "s:/:../:g" | cut -c 4-)apidocs/"; \
	  sed -i "s:http\://twistedmatrix.com/documents/$(twversion)/api/:$$path:g" \
	  debian/twisted-doc/usr/share/doc/twisted-doc/$$i; \
	done
endif
	dh_installmenu -i
	dh_compress -i -X.py
	dh_fixperms -i
	dh_lintian -i
	dh_python2 -i -X hamcrest
	# Now that dh_python2 has generated the binary dependencies from the
	# egg-info, strip the unconditional requirements (everything before the
	# first [extra] section) from the shipped requires.txt. Upstream pins
	# zope.interface>=4.4.2, incremental>=16.10.1 etc.; resolute ships
	# compatible-but-older versions, and leaving the pins in makes
	# pkg_resources refuse to start twistd. The Depends are unaffected because
	# they were already emitted above; this matches Debian buster, whose
	# installed requires.txt has no unconditional block.
	sed -i '/^\[/,$$!d' debian/python-twisted-core/usr/lib/python2.7/dist-packages/Twisted-*.egg-info/requires.txt
	# post-process .substvars until #806316 is fixed and -X starts working
	sed -i 's/ python3*-hamcrest,//' debian/python-twisted-core.substvars
	dh_installdeb -i
	dh_gencontrol -ppython-twisted-core -ppython-twisted-mail \
	    -ppython-twisted-names -ppython-twisted-news -ppython-twisted-web \
	    -ppython-twisted-runner -ppython-twisted-runner-dbg \
            -ppython-twisted-words -ppython-twisted
	: # Add the epoch to python-twisted-conch, for upgrades
	dh_gencontrol -ppython-twisted-conch -- -v1:$(DEBVERS)
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_lintian -a
	dh_installdocs -a
	dh_installmenu -a

	dh_strip -ppython-twisted-bin --dbg-package=python-twisted-bin-dbg
	rm -rf debian/python-twisted-bin-dbg/usr/share/doc/python-twisted-bin-dbg
	ln -s python-twisted-bin \
	    debian/python-twisted-bin-dbg/usr/share/doc/python-twisted-bin-dbg

	dh_compress -a -X.py
	dh_fixperms -a
	dh_python2 -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install install-nover install-prereq
.NOTPARALLEL:

override_dh_auto_test:
