#!/usr/bin/make -f
# -*- makefile -*-

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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# When using sudo pbuilder, this will cause mk/install.mk to run sudo,
# but we don't need sudo as a build-dep for the package if we unexport
# the SUDO_USER variable.
unexport SUDO_USER

# Debhelper clears MAKEFLAGS, so we have to do this again for any
# target where we call $(MAKE) directly.  Boo.
DEB_PARALLEL_JOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
DEB_MAKE_PARALLEL = $(if $(DEB_PARALLEL_JOBS),-j$(DEB_PARALLEL_JOBS))


# $DEB_VERSION_UPSTREAM is the full upstream version (eg. 1.0.0~alpha)

# This is original/unmangled upstream version (eg. 1.0.0-alpha)
UPSTREAM_RUST_VERSION := $(subst ~,-,$(DEB_VERSION_UPSTREAM))

# Main (major.minor) upstream version (eg. 1.0)
MAIN_RUST_VERSION := $(shell echo $(DEB_VERSION_UPSTREAM) | egrep -o '^[^\.]+\.[^\.]+')

# Upstream version suffix, if any (eg. alpha)
UPSTREAM_RUST_SUFFIX := $(shell echo $(DEB_VERSION_UPSTREAM) | egrep -o '[^\~]+$$')

# Private dir for rust .so and .rlib
RUST_PREFIX := usr/lib/$(DEB_HOST_MULTIARCH)/rust/$(MAIN_RUST_VERSION)

# Release type (one of beta, stable or nightly)
RELEASE_CHANNEL := beta

DEB_DESTDIR := $(CURDIR)/debian/tmp
RUST_TMP_PREFIX := $(DEB_DESTDIR)/$(RUST_PREFIX)

# These are the normal build flags
COMMON_FLAGS = \
               --disable-manage-submodules \
               --release-channel=$(RELEASE_CHANNEL) \
               --mandir=/usr/share/man \
               --enable-rpath \
               --prefix=/$(RUST_PREFIX)

SYSTEM_LLVM = --llvm-root=/usr/lib/llvm-3.6


ifeq (,$(findstring dlstage0,$(DEB_BUILD_PROFILES)))
# Without these options, a pre-built stage0 will be downloaded from
# rust-lang.org at build time.
#	DEB_CONFIGURE_EXTRA_FLAGS += --enable-local-rust --local-rust-root=/usr
endif

BUILD_DOCS = 1
ifneq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
  DEB_CONFIGURE_EXTRA_FLAGS += --disable-docs
  BUILD_DOCS =
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  DEB_CONFIGURE_EXTRA_FLAGS += --disable-optimize --disable-optimize-cxx --disable-optimize-llvm
endif


%:
	dh $@ --parallel

# Note: SHELL is not set by dash, but the configure script wants to use it
override_dh_auto_configure:
	SHELL=/bin/sh \
	    ./configure \
		    $(COMMON_FLAGS) \
		    $(DEB_CONFIGURE_EXTRA_FLAGS) \
		    $(SYSTEM_LLVM)


override_dh_auto_clean:
	[ ! -f Makefile ] ||  $(MAKE) $(DEB_MAKE_PARALLEL) clean-all
	-$(RM) Makefile

override_dh_auto_build-arch:
	dh_auto_build -- all VERBOSE=1

override_dh_auto_build-indep:
	$(if $(BUILD_DOCS),dh_auto_build -- docs)

LD_TMP1 := $(RUST_TMP_PREFIX)/lib
# TODO improve this stuff to make it dynamic
LD_TMP_i386 := $(LD_TMP1)/rustlib/i686-unknown-linux-gnu/lib
LD_TMP_AMD64 := $(LD_TMP1)/rustlib/x86_64-unknown-linux-gnu/lib
RUST_TMP_LD_PATH := $(LD_TMP1):$(LD_TMP_i386):$(LD_TMP_AMD64)

override_dh_shlibdeps:
	LD_LIBRARY_PATH=$(RUST_TMP_LD_PATH):$(LD_LIBRARY_PATH) \
		dh_shlibdeps -l$(RUST_TMP_LD_PATH)

override_dh_auto_install:
	dh_auto_install --destdir=$(DEB_DESTDIR)
	dh_link $(RUST_PREFIX)/bin/rustc usr/bin/rustc-$(MAIN_RUST_VERSION)
	dh_link usr/bin/rustc-$(MAIN_RUST_VERSION) usr/bin/rustc
	dh_link $(RUST_PREFIX)/bin/rustdoc usr/bin/rustdoc-$(MAIN_RUST_VERSION)
	dh_link usr/bin/rustdoc-$(MAIN_RUST_VERSION) usr/bin/rustdoc
	dh_link $(RUST_PREFIX)/bin/rust-gdb usr/bin/rust-gdb-$(MAIN_RUST_VERSION)
	dh_link usr/bin/rust-gdb-$(MAIN_RUST_VERSION) usr/bin/rust-gdb

	# Replace duplicated compile-time/run-time dylibs with symlinks
	for f in $(RUST_TMP_PREFIX)/lib/rustlib/*/lib/lib*.so; do \
		name=$${f##*/}; \
		if [ -f $(RUST_TMP_PREFIX)/lib/$$name ]; then \
			ln -sf ../../../$$name $$f; \
		fi; \
	done

override_dh_install:
	# Brute force to remove privacy-breach-logo lintian warning.
	# We could have updated the upstream sources but it would complexify
	# the rebase
ifneq (,$(BUILD_DOCS))
	sed -i '/rel="shortcut icon" href="http:\/\/www.rust-lang.org\/favicon.ico"/d' `find $(DEB_DESTDIR) -iname '*.html'`
	rm -f `find $(DEB_DESTDIR) -iname '*.html' -empty` $(DEB_DESTDIR)/usr/share/doc/rust-doc/html/.lock
	ln -sf /usr/share/javascript/jquery/jquery.js $(DEB_DESTDIR)/usr/lib/*/rust/*/share/doc/rust/html/jquery.js
	ln -sf /usr/share/javascript/jquery/jquery.js $(DEB_DESTDIR)/usr/lib/*/rust/*/share/doc/rust/html/style/jquery.js
	ln -sf /usr/share/javascript/jquery/jquery.js $(DEB_DESTDIR)/usr/lib/*/rust/*/share/doc/rust/html/book/jquery.js
endif
	dh_install

override_dh_installchangelogs:
	dh_installchangelogs RELEASES.md

override_dh_installdocs:
	dh_installdocs -X.tex -X.aux -X.log -X.out -X.toc

override_dh_auto_test:
	# NB: not parallel - there is some race that leads to failures like:
	# note: cc: error: x86_64-unknown-linux-gnu/test/run-pass/generic-default-type-params-cross-crate.stage2-x86_64-unknown-linux-gnulibaux/default_type_params_xc.o: No such file or directory
ifeq (, $(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) check-notidy
endif
