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

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

%:
	dh $@ --with=python2,python3

override_dh_auto_clean:
	set -ex; for python in $(shell py3versions -r); do \
	    $$python setup.py clean || true; \
	done
	set -ex;for file in activate-global-python-argcomplete register-python-argcomplete activate-global-python-argcomplete3 register-python-argcomplete3 ; do \
	    rm -f debian/$$file.1 ; \
	done
	dh_auto_clean
	rm -rf build

override_dh_auto_build:
	set -ex; for python in $(shell py3versions -r); do \
	    $$python setup.py build; \
	done
	dh_auto_build

override_dh_auto_test:
	set -ex; for python in $(shell py3versions -r); do \
        $$python test/test.py; \
    done
	set -ex; for python in $(shell pyversions -r); do \
        $$python test/test.py; \
    done

override_dh_auto_install:
	set -ex; for python in $(shell py3versions -r); do \
	    $$python setup.py install --root=$(CURDIR)/debian/python3-argcomplete --install-layout=deb; \
	    for file in activate-global-python-argcomplete python-argcomplete-check-easy-install-script register-python-argcomplete ; do \
	        mv $(CURDIR)/debian/python3-argcomplete/usr/bin/$$file $(CURDIR)/debian/python3-argcomplete/usr/bin/$${file}3 ; \
	    done ; \
	done
	dh_auto_install --destdir=$(CURDIR)/debian/python-argcomplete

override_dh_install:
	dh_install -X.pyc --fail-missing

override_dh_installman:
	# we keep python-argcomplete-check-easy-install-script as a manual manpage (--help doesn't exists)
	VERSION=$$(./setup.py -V) ; \
	for file in activate-global-python-argcomplete register-python-argcomplete ; do \
		cp -f scripts/$$file debian/$$file ; \
		chmod a+x debian/$$file ; \
		PYTHONPATH=. help2man --version-string="$$VERSION" \
			--no-info --include=debian/help2man.include \
			--name "$$file - argcomplete utility script" \
			-o debian/$$file.1 \
			debian/$$file ; \
		rm -f debian/$$file; \
		# build the python3 version as well \
		cp -f scripts/$$file debian/$${file}3 ; \
		file=$${file}3; \
		chmod a+x debian/$$file ; \
		PYTHONPATH=. help2man --version-string="$$VERSION" \
			--no-info --include=debian/help2man.include \
			--name "$$file - argcomplete utility script" \
			-o debian/$$file.1 \
			debian/$$file ; \
		rm -f debian/$$file; \
	done ; \
	rm -f argcomplete/*.py[co]
	dh_installman

