#!/usr/bin/make -f

# Print all logging info to stdout so it
# shows up in build logs.
export MESON_PRINT_TEST_OUTPUT=1

export QT_SELECT=qt5

# We're not in 1963 anymore, Toto!
export LC_ALL=C.UTF-8

export PYBUILD_NAME=meson

export DEBPYTHON3_DEFAULT=3.5
export DEBPYTHON3_SUPPORTED=3.5

export CC=gcc-4.8
export CXX=g++-4.8

%:
	dh $@

# Based on pybuild, in the dh-python package
override_dh_auto_clean:
	HOME=$(CURDIR)/debian/home python3.5 ./setup.py clean

# Based on pybuild, in the dh-python package
override_dh_auto_configure:
	mkdir -p debian/home
	HOME=$(CURDIR)/debian/home python3.5 ./setup.py config

# Based on pybuild, in the dh-python package
override_dh_auto_build:
	HOME=$(CURDIR)/debian/home python3.5 ./setup.py build

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	mkdir -p debian/path
	ln -snf /usr/bin/python3.5 debian/path/python3
	PATH=$(CURDIR)/debian/path:$(PATH) python3.5 ./run_tests.py
endif

# Based on pybuild, in the dh-python package
override_dh_auto_install:
	find debian/build -name '*.egg-info' -print0 | xargs -r -0 rm -vfr
	HOME=$(CURDIR)/debian/home python3.5 ./setup.py install \
		--force \
		--root debian/meson \
		--prefix /usr \
		--install-layout deb \
		--install-lib /usr/share/meson \
		--install-scripts /usr/share/meson \
		$(NULL)

override_dh_clean:
	dh_clean
	rm -f meson-test-run.txt meson-test-run.xml
	rm -rf build
	rm -rf debian/home
	rm -rf debian/path
	rm -rf __pycache__
	rm -rf mesonbuild/__pycache__
	rm -rf mesonbuild/*/__pycache__

override_dh_install:
# Helper script to autogenerate cross files.
	dh_install
	mkdir -p $$(pwd)/debian/meson/usr/share/meson
	cp debian/debcrossgen $$(pwd)/debian/meson/usr/share/meson
	rm -rf $$(pwd)/debian/meson/usr/lib/python*/dist-packages/mesonbuild/__pycache__
	rm -rf $$(pwd)/debian/meson/usr/lib/python*/dist-packages/mesonbuild/*/__pycache__
	# dh_python3 in precise didn't support --shebang
	sed -e '1s,#!.*,#!/usr/bin/python3.5,' < meson.py > debian/meson/usr/share/meson/meson
	chmod 0755 debian/meson/usr/share/meson/meson
	mkdir -p $$(pwd)/debian/meson/usr/bin
	ln -fns ../share/meson/meson debian/meson/usr/bin/meson
