#!/usr/bin/make -f

export DH_VERBOSE=1

export LANG=C.UTF-8
export LC_ALL=C.UTF-8

export DEB_CXXFLAGS_MAINT_APPEND += -std=gnu++14

%:
	dh $@ --with sphinxdoc

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build
	rm -rf tools/__pycache__
	rm -f inc/genhdr/microbitversion.h

override_dh_auto_build:
	# rename directories created from components tarballs (we note that
	# the underscore is not valid in the "component name" section of the
	# original source tarball filename, so must handle it here)
	mv yotta-targets yotta_targets
	mv yotta-modules yotta_modules
	# build the firmware
	make all
	# revert directories created from components tarballs
	mv yotta_targets yotta-targets
	mv yotta_modules yotta-modules
	# build documentation and remove sources
	PYTHONPATH=. http_proxy='127.0.0.1:9' https_proxy='127.0.0.1:9' python3 -m sphinx -N -bhtml docs/ build/html
	# fix duplicated images created by sphinx-build
	rm -f build/html/_images/speech-pitch1.png
	ln -sr build/html/_images/speech-pitch.png build/html/_images/speech-pitch1.png
	rm -f build/html/_images/scroll-hello1.gif
	ln -sr build/html/_images/scroll-hello.gif build/html/_images/scroll-hello1.gif
	rm -f build/html/_images/speech1.png
	ln -sr build/html/_images/speech.png build/html/_images/speech1.png

override_dh_fixperms:
	dh_fixperms
	find debian/firmware-microbit-micropython/usr/share/doc/firmware-microbit-micropython/examples \
			-name "*.py" -exec chmod -x '{}' \;

# all tests are executed on an attached micro:bit, so disable them
override_dh_auto_test:
