#!/usr/bin/make -f

# contrary to the documentation, compat level 10 doesn't enable  bindnow yet
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# The build system doesn't use CPPFLAGS, pass them to CFLAGS to enable the
# missing (hardening) flags
DEB_CFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)
export DEB_CFLAGS_MAINT_APPEND

PREFIX = $(CURDIR)/debian/b43-asm/usr
export PREFIX

override_dh_auto_build:
	dh_auto_build --sourcedirectory=assembler \
		      --buildsystem=makefile
	dh_auto_build --sourcedirectory=disassembler \
		      --buildsystem=makefile

override_dh_auto_clean:
	rm -rf	.pc \
		b43-tools \
		foobar.fw \
		selftest \
		test.fw
	dh_auto_clean --sourcedirectory=assembler \
		      --buildsystem=makefile
	dh_auto_clean --sourcedirectory=disassembler \
		      --buildsystem=makefile

override_dh_auto_install: selftest
	dh_auto_install --sourcedirectory=assembler \
			--buildsystem=makefile
	dh_auto_install --sourcedirectory=disassembler \
			--buildsystem=makefile

selftest:
	@echo
	@echo SELFTEST:
	PATH=$(CURDIR)/assembler:$(PATH) b43-asm assembler/test.asm test.fw --cpp-args -DDEBUG=1 -- --ivalext .fw --psize
	PATH=$(CURDIR)/assembler:$(CURDIR)/disassembler:$(PATH) $(CURDIR)/assembler/selftest.sh test.fw
	@echo

%:
	dh ${@}

get-orig-source:
	@rm -rf b43-tools
	git clone https://git.bues.ch/git/b43-tools.git/
	@test -d b43-tools
	GIT_DATE=$$(GIT_DIR=b43-tools/.git/ git log -1 --pretty=format:%cD assembler/ disassembler/); \
		UTC_DATE=$$(date --utc --date="$$GIT_DATE" +%Y%m%d.%H%M); \
		mkdir b43-tools/b43-asm-0~$$UTC_DATE; \
		test -d b43-tools/b43-asm-0~$$UTC_DATE; \
		cp -r	b43-tools/assembler \
			b43-tools/disassembler \
				b43-tools/b43-asm-0~$$UTC_DATE/; \
		XZ_OPT=-6 tar \
			-C b43-tools/ \
			--posix \
			--numeric-owner \
			--owner=0 \
			--group=0 \
			-cJf ../b43-asm_0~$$UTC_DATE.orig.tar.xz \
				b43-asm-0~$$UTC_DATE/
	@rm -rf b43-tools
