#!/usr/bin/make -f


CARGO ?= cargo
RONN ?= ronn

OUTDIR_EMULATOR = $(CURDIR)/debian/pir-8-emu/
OUTDIR_BINUTILS = $(CURDIR)/debian/pir-8-emu-binutils/
OUTDIR_DEVEL    = $(CURDIR)/debian/pir-8-emu-devel/
OUTDIR_ISA      = $(CURDIR)/debian/pir-8-emu-isa/

# https://wiki.debian.org/Multiarch/Tuples#Supporting_interfaces
TRIPLE = $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)


override_dh_auto_build:
	mkdir -p completions
	$(RONN) --organization="Lounge<C++>" $(wildcard man/*.md)	
	$(MAKE)
	RUSTFLAGS=-g $(CARGO) build --release
	cd completions && ../target/release/pir-8-build-completions

override_dh_auto_install:
	mkdir -p $(foreach l,$(OUTDIR_EMULATOR) $(OUTDIR_BINUTILS),$(l)usr/bin $(l)usr/share/zsh/vendor-functions $(l)usr/share/fish/vendor_completions.d) $(OUTDIR_EMULATOR)usr/share/pixmaps $(OUTDIR_EMULATOR)usr/share/applications $(OUTDIR_DEVEL)usr/lib/$(TRIPLE)/pir-8-emu/handler-examples/ $(OUTDIR_DEVEL)usr $(foreach l,$(OUTDIR_BINUTILS) $(OUTDIR_DEVEL) $(OUTDIR_ISA),$(l)usr/share/pir-8-emu)
	@
	cp assets/pir-8-emu.ico debian/pir-8-emu.png $(OUTDIR_EMULATOR)usr/share/pixmaps/
	cp debian/pir-8-emu.desktop $(OUTDIR_EMULATOR)usr/share/applications/
	cp target/release/pir-8-emu $(OUTDIR_EMULATOR)usr/bin/
	@
	cp $(foreach l,as disasm,target/release/pir-8-$(l)) $(OUTDIR_BINUTILS)usr/bin/
	@
	cp -r include/          $(OUTDIR_DEVEL)usr/
	cp $(wildcard out/*.so) $(OUTDIR_DEVEL)usr/lib/$(TRIPLE)/pir-8-emu/handler-examples/
	@
	cp -r test-data/        $(OUTDIR_BINUTILS)usr/share/pir-8-emu/examples
	cp -r handler-examples/ $(OUTDIR_DEVEL)usr/share/pir-8-emu/
	cp -r ext/pir-8/        $(OUTDIR_ISA)usr/share/pir-8-emu/
	rm                      $(OUTDIR_ISA)usr/share/pir-8-emu/pir-8/LICENSE
	@
	dh_bash-completion
	# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=921236#17
	cp completions/_pir-8-emu                         $(OUTDIR_EMULATOR)usr/share/zsh/vendor-functions/
	cp $(foreach l,as disasm,completions/_pir-8-$(l)) $(OUTDIR_BINUTILS)usr/share/zsh/vendor-functions/
	# # https://fishshell.com/docs/current/#where-to-put-completions
	cp completions/pir-8-emu.fish                         $(OUTDIR_EMULATOR)usr/share/fish/vendor_completions.d/
	cp $(foreach l,as disasm,completions/pir-8-$(l).fish) $(OUTDIR_BINUTILS)usr/share/fish/vendor_completions.d/

# changelog.pir-8:     git log --no-decorate --no-show-signature
# changelog.pir-8-emu: for tag in $(git tag -l | tac); do git tag -ln1000 "$tag"; echo; echo; done | awk '!/[[:space:]]+\([[:alnum:]]{40}\)$/' | sed -re 's;\([[:alnum:]]{40}\);+;g' -e 's;\(#;(https://github.com/LoungeCPP/pir-8-emu/issues/;g' -e 's;\(@;(https://github.com/;g' | head -c -2
override_dh_installchangelogs:
	dh_installchangelogs -Npir-8-emu-isa debian/changelog.pir-8-emu
	dh_installchangelogs -ppir-8-emu-isa debian/changelog.pir-8

override_dh_strip:
	# These binaries do not use BLT, but cargo still links them against it; dpkg-shlibdeps adds libbearlibterminal to Depends: and complains:
	# > dpkg-shlibdeps: warning: package could avoid a useless dependency if debian/pir-8-emu-binutils/usr/bin/pir-8-disasm debian/pir-8-emu-binutils/usr/bin/pir-8-as were not linked against libBearLibTerminal.so.0.15.7 (they use none of the library's symbols)
	patchelf --remove-needed $(shell ldd $(OUTDIR_BINUTILS)usr/bin/pir-8-as     | awk '/BearLibTerminal/ {print $$1}') $(OUTDIR_BINUTILS)usr/bin/pir-8-as
	patchelf --remove-needed $(shell ldd $(OUTDIR_BINUTILS)usr/bin/pir-8-disasm | awk '/BearLibTerminal/ {print $$1}') $(OUTDIR_BINUTILS)usr/bin/pir-8-disasm
	dh_strip

%:
	dh $@
