#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk

# For cross-compiling; perhaps we should add this to dh-cargo or debcargo
export PKG_CONFIG = $(DEB_HOST_GNU_TYPE)-pkg-config
export PKG_CONFIG_ALLOW_CROSS = 1
RUSTFLAGS += -C linker=$(DEB_HOST_GNU_TYPE)-gcc

# Pass on dpkg-buildflags stuff
RUSTFLAGS += $(foreach flag,$(LDFLAGS),-C link-arg=$(flag))
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS RUSTFLAGS

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

%:
	dh $@ --with bash-completion

override_dh_auto_configure:
	cp -a $(CURDIR)/Cargo.lock $(CURDIR)/.Cargo.lock.orig
ifneq ($(filter pkg.cargo.mkstage0,$(DEB_BUILD_PROFILES)),)
	# NOTE: this very likely doesn't work any more, see bootstrap.py for details
	# Instead, you can try to bootstrap by setting PATH to a binary cargo
	# downloaded from upstream; or by cross-compiling, see "build-cross" below
	# Preserved in case someone wants to resurrect it later:
	# Bootstrap cargo stage0
	./debian/bootstrap.py \
		--no-clean \
		--no-clone \
		--no-git \
		--no-download \
		--crate-index $(CURDIR)/vendor/index / \
		--cargo-root $(CURDIR)/ \
		--target-dir $(CURDIR)/deps \
		--host=$(DEB_HOST_RUST_TYPE) \
		--target=$(DEB_TARGET_RUST_TYPE)
	# Workaround for https://github.com/rust-lang/cargo/issues/1423
	ln -s `find $(CURDIR)/deps -name 'cargo-*' -type f -executable` $(CURDIR)/cargo-stage0
else
	ln -s `which cargo` $(CURDIR)/cargo-stage0
endif
	# Configure to build cargo using the just-built stage0
	./configure \
		--prefix=/usr \
		--disable-debug \
		--enable-optimize \
		--local-rust-root=/usr \
		--release-channel=stable \
		--cargo=$(CURDIR)/cargo-stage0 \
		--build=$(DEB_BUILD_RUST_TYPE) \
		--host=$(DEB_HOST_RUST_TYPE) \
		--target=$(DEB_TARGET_RUST_TYPE)

override_dh_auto_build-arch:
	RUST_BACKTRACE=1 $(MAKE)

override_dh_auto_build-indep:
ifeq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
	$(MAKE) doc
	cd target/doc/ && rm -f jquery.js && ln -s /usr/share/javascript/jquery/jquery.js
endif

override_dh_auto_test:
	RUST_BACKTRACE=1 CFG_DISABLE_CROSS_TESTS=1 dh_auto_test

override_dh_auto_install:
	# We pick stuff directly from target/

override_dh_auto_clean:
	-mv $(CURDIR)/.Cargo.lock.orig $(CURDIR)/Cargo.lock
	dh_auto_clean
	-$(RM) -r $(CURDIR)/target/ \
			$(CURDIR)/.cargo \
			$(CURDIR)/config.mk \
			$(CURDIR)/config.stamp \
			$(CURDIR)/Makefile \
			$(CURDIR)/cargo-stage0

CROSS_SBUILD = DEB_BUILD_OPTIONS=nocheck sbuild --profiles=nocheck \
	  --build-failed-commands '%SBUILD_SHELL' \
	  --add-depends=libc-dev:$(DEB_HOST_ARCH) \
	  $(EXTRA_SBUILD_FLAGS) \
	  --host=$(DEB_HOST_ARCH) \
	  --no-arch-all

crossbuild:
	$(CROSS_SBUILD) .

crossbuild-experimental:
	$(CROSS_SBUILD) \
	  --extra-repository='deb http://httpredir.debian.org/debian experimental main' \
	  --build-dep-resolver=aspcud \
	  .
