#! /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]')
PY3VERS	:= $(shell py3versions -vs)
VER3	:= $(shell /usr/bin/python3 -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-doc
build-stamp: $(PYVERS:%=build-python%) $(PY3VERS:%=build3-python%)
	touch $@
build-python%:
	python$* setup.py build
	python$*-dbg setup.py build
	touch $@

build3-python%:
	python$* setup.py build
	python$*-dbg setup.py build
	touch $@

build-doc:
	$(MAKE) -C docs html
	touch $@

clean:
	rm -rf *-stamp build-python* build tmp
	rm -rf docs/_build build-doc
	rm -rf build3-python*
	rm -rf apidocs py3
	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%) $(PY3VERS:%=install3-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

install3-python%: install-prereq
	: # python3-twisted $*
	python$* setup.py install \
		--root=$(CURDIR)/debian/python3-twisted --install-layout=deb

	find debian/python3-twisted -name '__pycache__' | xargs -r rm -rf

	: # python3-twisted-bin
	dh_movefiles -ppython3-twisted-bin \
	  --sourcedir=debian/python3-twisted \
	    $(call py_libdir,$*)/twisted/test/raiser.cpython-$(subst .,,$*)m-$(DEB_HOST_MULTIARCH).so
#	    $(call py_libdir,$*)/twisted/python/sendmsg.cpython-$(subst .,,$*)m-$(DEB_HOST_MULTIARCH).so

	: # Replace all '#!' calls to python with /usr/bin/python3
	: # and make them executable
	for i in `find debian/python3-twisted debian/python3-twisted-* -mindepth 3 -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python3\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

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

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

	: # python3 man pages
	for i in debian/python3-twisted/usr/bin/*; do \
	  mv $$i $${i}3; \
	done
	mkdir -p debian/python3-twisted/usr/share/man/man1
	for i in docs/core/man/{trial,twistd}.1; do \
	  i2=$$(basename $$i .1)3.1; \
	  cp -p $$i debian/python3-twisted/usr/share/man/man1/$$i2; \
	done

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 build-doc install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i
	cp -a docs/_build/html debian/twisted-doc/usr/share/doc/twisted-doc/.
	dh_sphinxdoc -ptwisted-doc

	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
	dh_python3 -i -X hamcrest
	# post-process .substvars until #806316 is fixed and -X starts working
	sed -i 's/ python3*-hamcrest,//' debian/python-twisted-core.substvars debian/python3-twisted.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 -ptwisted-doc -ppython-twisted \
	    -ppython3-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_strip -ppython3-twisted-bin --dbg-package=python3-twisted-bin-dbg
	rm -rf debian/python3-twisted-bin-dbg/usr/share/doc/python3-twisted-bin-dbg
	ln -s python3-twisted-bin \
	    debian/python3-twisted-bin-dbg/usr/share/doc/python3-twisted-bin-dbg

	dh_compress -a -X.py
	dh_fixperms -a
	dh_python2 -a
	dh_python3 -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:
