#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

# needed as we have more than one binary package
export PYBUILD_DESTDIR=debian/ocrmypdf

# setuptools-scm's heuristics fail to determine the package version
# which causes the build to fail, so just override it
export SETUPTOOLS_SCM_PRETEND_VERSION = ${DEB_VERSION_UPSTREAM}

# deal with unicode encoding/decoding errors throughout the package
# build, and especially the test suite.  This isn't needed in
# d/tests/test-suite because autopkgtest sets LANG=C.UTF-8 (see
# locale(7) for the difference between LC_ALL and LANG; using LC_ALL
# here makes the package build more robust because LC_ALL overrides
# all other locale variables, whereas LANG does not)
export LC_ALL=C.UTF-8

%:
	dh $@ --with python3,sphinxdoc --buildsystem=pybuild

override_dh_installchangelogs:
	dh_installchangelogs docs/release_notes.rst

# upstream's doc build assumes that ocrmypdf is already installed, and
# tries to find its version with pkg_resources.  Here we make our own
# copy and patch in the version string, and build the docs based on
# that.  Better than a quilt patch which would have to be updated
# every upstream release
override_dh_auto_build:
	mkdir -p debian/.debhelper
	cp -R ocrmypdf debian/.debhelper
	sed -i debian/.debhelper/ocrmypdf/__init__.py -e \
		"s|^__version__ =.*|__version__ = \"$(DEB_VERSION_UPSTREAM)\"|"
# disabled pending updates to debhelper for 'nodoc' / compat 11
#ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=debian/.debhelper sphinx-build docs html
#endif
	dh_auto_build -O--buildsystem=pybuild

# don't install installation instructions
override_dh_installdocs:
	dh_installdocs -Xinstallation.html
override_dh_sphinxdoc:
	dh_sphinxdoc -Xinstallation.html

# this override is needed because the pybuild build system does not
# yet support running py.test tests with pytest-runner (pytest-runner
# is necessary to run the test suite in advance of ocrmypdf being
# installed)
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	python3 setup.py test
endif

# requires ocrmypdf already installed!
.PHONY: gen-man-page
gen-man-page:
	help2man ocrmypdf --no-info \
		-n "add an OCR text layer to PDF files" \
		> debian/ocrmypdf.1
