#!/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

# This prevents setup.py from aborting because programs like qpdf,
# tesseract etc. are not installed.  This avoids us having to include
# those are build-depends.
#
# Note that this is a non-standard option implemented by OCRmyPDF's
# setup.py.
export PYBUILD_INSTALL_ARGS = --force

# 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 src/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_install:
	dh_install -Xinstallation
override_dh_sphinxdoc:
	dh_sphinxdoc -Xinstallation

# The test suite takes longer and longer to run, so we have disabled
# it at package build time and are relying on autopkgtest.  This also
# eases backporting ocrmypdf
override_dh_auto_test:

# # 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" \
		--locale="C.UTF-8" \
		> debian/ocrmypdf.1
