#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
export DH_VERBOSE = 1

export PYBUILD_NAME=aiosmtpd
export PYBUILD_AFTER_INSTALL=mv '{destdir}/{install_dir}/examples' \
		'{destdir}/usr/share/doc/python3-aiosmtpd/examples_{version}' && \
		ln -sf '/usr/share/doc/python3-aiosmtpd/examples_{version}' \
		'{destdir}/usr/share/doc/python3-aiosmtpd/examples'
export PYBUILD_BEFORE_TEST=cp -a '{build_dir}/aiosmtpd/tests/certs/server.key' \
	'{build_dir}/aiosmtpd/tests/certs/server.crt' '/tmp/old' && \
	cp -f /tmp/new/server.key /tmp/new/server.crt \
	'{build_dir}/aiosmtpd/tests/certs'
export PYBUILD_AFTER_TEST=cp -fa '/tmp/old/server.key' '/tmp/old/server.crt' \
	'{build_dir}/aiosmtpd/tests/certs'

# main packaging script based on dh7 syntax
%:
	dh $@ --with python3,sphinxdoc --buildsystem=pybuild

override_dh_auto_install:
	dh_auto_install
	find debian/python3-aiosmtpd/usr/share/doc/python3-aiosmtpd -maxdepth 2 -type d -name "__pycache__" -exec rm -rf {} \;

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p /tmp/new
	mkdir -p /tmp/old
	openssl req -x509 -newkey rsa:4096 \
	-keyout '/tmp/new/server.key' \
	-out '/tmp/new/server.crt' \
	-days 365 -nodes -subj '/CN=localhost'
	dh_auto_test
endif

override_dh_installdocs:
	PYTHONPATH=. python3 -m sphinx -N -q -E -c ./ -b html . debian/compiled_doc/temp
	mv debian/compiled_doc/temp/aiosmtpd/docs debian/compiled_doc/html
	dh_installdocs

override_dh_installman:
	mkdir -p debian/compiled_doc/manpage
	rst2man aiosmtpd/docs/manpage.rst > debian/compiled_doc/manpage/aiosmtpd.man
	dh_installman

override_dh_python3:
	dh_python3
	mv debian/python3-aiosmtpd/usr/lib/python3/dist-packages/aiosmtpd/docs \
		debian/python3-aiosmtpd/usr/share/doc/python3-aiosmtpd/rst
