#!/usr/bin/make -f

export PYBUILD_NAME := hy

# have to set HOME because rply wants to write into ~/.cache/rply
export HOME := $(CURDIR)/fake-home

# TODO figure out why the tests in python3 fail via pybuild, but pass after install
export PYBUILD_DISABLE_python3 := test

BUILD_DATE=$(shell dpkg-parsechangelog -S Date | LC_ALL=C date -u '+%B %d, %Y' -f -)

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

override_dh_auto_build:
	dh_auto_build
	make -C docs \
		SPHINXOPTS='-D today="$(BUILD_DATE)"' \
		man
	# TODO html docs (and install them in hy-doc or something)

override_dh_auto_test:
	# tests use "README.md" from the original source for IO testing
	dh_auto_test -- \
		--before-test 'cp {dir}/README.md {build_dir}' \
		--test-args ' \
			--exclude test_botsbuildbots \
			--ignore-files test_bin.py \
		' \
		--after-test 'rm {build_dir}/README.md'

override_dh_auto_install:
	dh_auto_install
# make sure all python-hy binaries have a "2" suffix
# (skip binaries that already have a "2" suffix)
	for bin in debian/python-hy/usr/bin/*; do \
		[ "$${bin%2}" = "$${bin}" ] || continue; \
		if [ -e "$${bin}2" ]; then \
			rm -v "$${bin}"; \
		else \
			mv "$${bin}" "$${bin}2"; \
		fi; \
	done
# make sure all python3-hy binaries have a "3" suffix
# (skip binaries that already have a "3" suffix)
	for bin in debian/python3-hy/usr/bin/*; do \
		[ "$${bin%3}" = "$${bin}" ] || continue; \
		if [ -e "$${bin}3" ]; then \
			rm -v "$${bin}"; \
		else \
			mv "$${bin}" "$${bin}3"; \
		fi; \
	done

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_auto_clean:
	dh_auto_clean
	# sphinx
	rm -rf docs/_build
	# clean out our HOME-hack leftovers
	rm -rf fake-home
	# egg-info! (not shipped at all in upstream tarballs)
	rm -rf hy.egg-info
