#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
RUSTFLAGS = -C link-args="$(LDFLAGS)"
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS RUSTFLAGS

rust_cpu = $(subst i586,i686,$(1))
DEB_HOST_RUST_TYPE := $(call rust_cpu,$(DEB_HOST_GNU_CPU))-unknown-$(DEB_HOST_GNU_SYSTEM)
DEB_TARGET_RUST_TYPE := $(call rust_cpu,$(DEB_TARGET_GNU_CPU))-unknown-$(DEB_TARGET_GNU_SYSTEM)

# Cargo looks for config in and writes cache to $CARGO_HOME/
export CARGO_HOME = $(CURDIR)/.cargohome
# Ask cargo to be verbose when building
export VERBOSE = 1

DEB_DESTDIR := $(CURDIR)/debian/tmp
VENDORDIR := $(CURDIR)/vendor
INDEXDIR := $(CURDIR)/vendor/index
DEPSDIR := $(CURDIR)/deps

%:
	dh $@ --with bash-completion

override_dh_auto_configure:
	cp -a $(CURDIR)/Cargo.lock $(CURDIR)/.Cargo.lock.orig
	# Prepare a fake registry by packing all deps
	./debian/cargo-vendor-pack.py

override_dh_auto_build:
ifneq ($(filter stage0,$(DEB_BUILD_PROFILES)),)
	# Bootstrap cargo stage0
	./debian/bootstrap.py \
		--no-clean \
		--no-clone \
		--no-git \
		--no-download \
		--crate-index $(INDEXDIR)/ \
		--cargo-root $(CURDIR)/ \
		--target-dir $(DEPSDIR)/ \
		--host=$(DEB_HOST_RUST_TYPE) \
		--target=$(DEB_TARGET_RUST_TYPE)
	# Workaround for https://github.com/rust-lang/cargo/issues/1423
	mv $(DEPSDIR) $(CURDIR)/.deps
	ln -s `find $(CURDIR)/.deps -name 'cargo-*' -type f -executable` $(CURDIR)/cargo-stage0
else
	ln -s `which cargo` $(CURDIR)/cargo-stage0
	# Workaround for https://github.com/rust-lang/cargo/issues/1423
	mv $(DEPSDIR) $(CURDIR)/.deps
endif
	# Configure to build cargo using the just-built stage0
	./configure \
		--prefix=/usr \
		--disable-debug \
		--enable-optimize \
		--local-rust-root=/usr \
		--local-cargo=$(CURDIR)/cargo-stage0
	# Build final cargo binary and docs
	$(MAKE)
ifeq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
	$(MAKE) doc
	cd target/doc/ && rm -f jquery.js && ln -s /usr/share/javascript/jquery/jquery.js
endif

	# Restore from workarounds
	mv $(CURDIR)/.deps $(DEPSDIR)

override_dh_auto_clean:
	-mv $(CURDIR)/.Cargo.lock.orig $(CURDIR)/Cargo.lock
	-mv $(CURDIR)/.deps $(DEPSDIR)
	dh_auto_clean
	-$(RM) -r $(CURDIR)/deps/*.rlib \
			$(CURDIR)/deps/build_script* \
			$(CURDIR)/deps/cargo* \
			$(CURDIR)/deps/*.o \
			$(CURDIR)/target/ \
			$(CURDIR)/.cargo/ \
			$(CURDIR)/config.mk \
			$(CURDIR)/config.stamp \
			$(CURDIR)/Makefile \
			$(CURDIR)/cargo-stage0 \
			$(CARGO_HOME) \
			$(VENDORDIR)

override_dh_auto_install:
	# We pick stuff directly from target/

override_dh_auto_test:
	# we don't run tests at the moment due to too many deps

