#!/usr/bin/make -f

#export DH_VERBOSE=1

export PYBUILD_DIR=_build/python
export PYBUILD_SYSTEM=distutils

LO_EXT_DIR := debian/tmp/usr/lib/libreoffice/share/extensions/grammalecte

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


# We cannot rely on auto_clean because PYBUILD_DIR is dynamically
# created, and thus we cannot rely on pybuild calling setup.py clean
# on the first run. Manually do the cleaning instead.
override_dh_auto_clean:
	rm -rf __pycache__/ \
		_build/ \
		grammalecte/ \
		graphspell/__pycache__/ \
		graphspell/_dictionaries/ \
		.pybuild

override_dh_auto_configure:
	python3 make.py fr

	mkdir $(PYBUILD_DIR)
	mv _build/Grammalecte-fr-*.zip $(PYBUILD_DIR)
	(cd $(PYBUILD_DIR) && unzip Grammalecte-fr-*.zip)

	dh_auto_configure

override_dh_auto_install:
	mkdir -p $(LO_EXT_DIR)
	unzip -d $(LO_EXT_DIR) _build/Grammalecte-fr-*.oxt

	rm $(LO_EXT_DIR)/pythonpath/grammalecte/WARNING.txt

	dh_auto_install

	# This move cannot be exported to dh_install using dh_exec, because
	# wildcards are not supported and Python version number may change.
	mkdir -p debian/tmp/usr/share/doc/python3-grammalecte
	cp debian/tmp/usr/lib/$(shell py3versions -d)/dist-packages/grammalecte/README* \
		debian/tmp/usr/share/doc/python3-grammalecte

	rm debian/tmp/usr/lib/python3.*/dist-packages/grammalecte/README*
	rm debian/tmp/usr/lib/python3.*/dist-packages/grammalecte/WARNING.txt
