#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

export PYBUILD_NAME=translate

p	= translate-toolkit
d	= $(CURDIR)/debian/$(p)
d2	= $(CURDIR)/debian/python-translate
d3	= $(CURDIR)/debian/python3-translate

export PYTHONNOUSERSITE=false

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

man:
	mkdir -p man
	for script in $(d2)/usr/bin/*; do \
	  case $$(basename $$script) in \
	  buildxpi*|build_tmdb|build_firefox*|get_moz_enUS*|junitmsgfmt|po2web2py|pocompendium|pomigrate2|popuretext|poreencode|posplit|pocount|poglossary|pocommentclean|pydiff|tmserver) \
	    ;; \
	  *) \
	    LC_ALL=C PYTHONPATH=. $$script --manpage \
	      > man/$$(basename $$script).1 \
	      || rm -f man/$(basename $$script).1; \
	    ;; \
	  esac; \
	done

docs:
	make -C docs html

override_dh_clean:
	dh_clean
	rm -rf man docs/_build run-tests .cache
	make -C docs clean

override_dh_install: man docs
	dh_install
	# delete unwanted cruft from /usr/lib
	for i in COPYING README.rst docs; do \
	  rm -rfv debian/*/usr/lib/python*/*-packages/translate/$$i \
	      debian/*/usr/lib/pyshared/translate/$$i; \
	done

	# provide a link to the langmodels shipped in libexttextcat-data
	for i in debian/*/usr/lib/python*/*-packages/translate/; do \
	  rm -rfv $$i/share/langmodels; \
	  ln -s /usr/share/libexttextcat $$i/share/langmodels; \
	done

	prename 's/\.sh$$//' debian/*/usr/bin/build_firefox.sh
	prename 's/\.py$$//' debian/*/usr/bin/*.py

	# run the test suite using py.test
	PYBUILD_SYSTEM=custom \
	  PYBUILD_TEST_ARGS='cp -a tests share {build_dir}; cd {build_dir}; PATH=$(CURDIR)/debian/{package}/usr/bin:$$PATH {interpreter} -m pytest -v -rxs' \
	  dh_auto_test --buildsystem=pybuild

	# only ship the Python 2 command line tools in the
	# translate-toolkit package.
	mkdir -p $(d)/usr/bin
	mv $(d2)/usr/bin/* $(d)/usr/bin/
	rm -rf $(d2)/usr/bin $(d3)/usr/bin

	# sub2po and po2sub need python-aeidon that no longer exists;
	# they can be brought back once the switch to Python 3 is complete.
	mkdir -p $(d)/usr/share/doc/$(p)/examples
	mv $(d)/usr/bin/po2sub $(d)/usr/bin/sub2po \
		$(d)/usr/share/doc/$(p)/examples
	cp man/po2sub.1 man/sub2po.1 \
		$(d)/usr/share/doc/$(p)/examples

override_dh_auto_test:
	# Tests must be run after install so that the entry_points scripts
	# have been created; override this and run tests as part of the install
	# step instead.

.PHONY: man docs
