#!/usr/bin/make -f

DH_VERBOSE = 1

# export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# DPKG_EXPORT_BUILDFLAGS = 1

# Remove because they're for normal builds, not "cross" builds, or what ever
# WASM is considered to be. Sort-of a cross-compile, but it isn't self hosting,
# so I'm not sure what to call it.
# include /usr/share/dpkg/default.mk
# include /usr/share/rustc/architecture.mk

# finds package info and puts it in vars like DEB_VERSION_UPSTREAM
include /usr/share/dpkg/pkg-info.mk
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
CARGO_REGISTRY=$(CURDIR)/debian/cargo_registry
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

WEBROOT=/usr/share/boids/
export WEBROOT

%:
	dh $@

execute_after_dh_auto_clean:
# 	$(CARGO) clean
	rm -rf $(CARGO_HOME)
	rm -rf $(CARGO_REGISTRY)
	rm -f debian/cargo-checksum.json

override_dh_auto_configure:
	$(CARGO) prepare-debian $(CARGO_REGISTRY) --link-from-system
	touch debian/cargo-checksum.json
	# patch cargo_home config to use lld with wasm, otherwise the build fails
	echo "\n[target.wasm32-unknown-unknown]" >> debian/cargo_home/config.toml
	cat debian/cargo_home/config.toml | sed "s/linker=[^']\+/linker=rust-lld/" | grep "^rustflags = " >> debian/cargo_home/config.toml
	dh_auto_configure

override_dh_auto_test:
# No tests. Can't execute a WASM binary
	echo "Skipping tests, can't run WASM files"

override_dh_auto_install:
	dh_auto_install WEBROOT=$(WEBROOT)
