#!/usr/bin/make -f
TESTDIR = $(shell dh_testdir || echo no)
ifeq (,$(TESTDIR))
include debian/make.mk
# Use dpkg-buildflags to get hardening flags, exclude non-hardening flags,
# and disable read-only relocations.
dpkg_buildflags = $(and $(1),$(shell DEB_BUILD_MAINT_OPTIONS=hardening=-relro DEB_CFLAGS_MAINT_STRIP="$(shell DEB_BUILD_MAINT_OPTIONS=hardening=-all dpkg-buildflags --get $(1))" dpkg-buildflags --get $(1)))
$(call lazy,CFLAGS,$$(call dpkg_buildflags,CFLAGS))
$(call lazy,CPPFLAGS,$$(call dpkg_buildflags,CPPFLAGS))
$(call lazy,LDFLAGS,$$(call dpkg_buildflags,LDFLAGS))

$(call lazy,DEB_HOST_GNU_TYPE,$$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE))
$(call lazy,DEB_BUILD_GNU_TYPE,$$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE))
$(call lazy,DEB_BUILD_ARCH,$$(shell dpkg-architecture -qDEB_BUILD_ARCH))

PYTHON := python -B

PRODUCT := browser
PRODUCT_NAME := firefox
include debian/upstream.mk

ifdef BACKPORT
LESS_SYSTEM_LIBS = 1
endif

ifeq ($(DEBIAN_SOURCE),$(PRODUCT_NAME))
MOZILLA_OFFICIAL := 1
endif

LDFLAGS :=
# ld crashes for some files because of --as-needed :(
ifeq (,$(filter ia64,$(DEB_BUILD_ARCH)))
LDFLAGS += -Wl,--as-needed
endif

# Reduce memory usage of the linker at the expense of processing time
# This should help on lower-end architectures like arm and mips, which
# spend an immense amount of time swapping.
LDFLAGS += -Wl,--reduce-memory-overheads
# Somehow, this breaks the build on ia64.
ifeq (,$(filter ia64,$(DEB_BUILD_ARCH)))
LDFLAGS += -Wl,--no-keep-memory
endif
# Also add execution time and memory usage stats in the logs
LDFLAGS += -Wl,--stats

AUTOCONF_DIRS := build/autoconf js/src/build/autoconf

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_FLAGS += --disable-optimize
endif

ifeq ($(DEB_BUILD_ARCH),ppc64)
	CFLAGS += -mminimal-toc
endif

ifneq (,$(filter kfreebsd-% hurd-%,$(DEB_BUILD_ARCH)))
	CONFIGURE_FLAGS += --disable-necko-wifi
endif

ifneq (,$(filter armel,$(DEB_BUILD_ARCH)))
	CFLAGS += -D__ARM_PCS
endif

ifneq (,$(filter ia64,$(DEB_BUILD_ARCH)))
	CONFIGURE_FLAGS += --enable-optimize=-O2
endif

ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_FLAGS += --enable-debug
endif
ifeq (,$(filter debug noopt,$(DEB_BUILD_OPTIONS)))
	GENSYMBOLS_FLAGS := -c4
endif

ifeq (default,$(origin CC))
CC := gcc
endif
ifeq (default,$(origin CXX))
CXX := g++
endif
CXXFLAGS = $(CFLAGS)

EXPORTS := CC CXX CFLAGS CXXFLAGS CPPFLAGS LDFLAGS MOZILLA_OFFICIAL
$(call lazy,CONFIGURE_ENV,$$(foreach export,$(EXPORTS),$$(export)="$$($$(export))"))

GENERATED_FILES += debian/dh/dh_xulrunner.1

debian/dh/dh_xulrunner.1: debian/dh/dh_xulrunner
	pod2man -c Debhelper -r $(GRE_VERSION) $^ > $@

build-xulrunner/dist/bin/npapi_getinfo: debian/dh/npapi_getinfo.c
	$(CC) -o $@ $< -Ibuild-xulrunner/dist/include -ldl -lpthread -Wl,-rpath,\$$ORIGIN

IN_FILES := $(wildcard debian/*.in debian/dh/*.in)
preprocessed_filename = $(subst JS_SO_VERSION,$(JS_SO_VERSION),$(subst GRE_VERSION,$(GRE_VERSION),$(1:.in=)))
define preprocess
$(call preprocessed_filename,$(1)): $(1)
PREPROCESSED_FILES += $(call preprocessed_filename,$(1))
endef
$(foreach f,$(IN_FILES),$(eval $(call preprocess, $(f))))

GENERATED_FILES += $(PREPROCESSED_FILES)
debian/control: debian/rules debian/changelog

DICT_DIR := /usr/share/hunspell

$(PREPROCESSED_FILES): VARS = GRE_VERSION JS_SO_VERSION DICT_DIR LESS_SYSTEM_LIBS
$(PREPROCESSED_FILES):
	$(PYTHON) config/Preprocessor.py --marker % -Fsubstitution $(foreach var,$(VARS),-D$(var)="$($(var))" )$< > $@

%/Makefile: DIR=$(firstword $(subst /, ,$@))
%/Makefile:
	$(DIR)/config.status -n -v --file=$@

%/package-manifest: %/Makefile
	$(MAKE) -C $*

override_dh_auto_configure:

stamps/prepare-configure:: stamps/dummy
	for dir in $(AUTOCONF_DIRS); do \
		for file in config.guess config.sub; do \
			sed -i '2!b;/^#/ i\exec "/usr/share/misc/'$$file'" "$$@"' $$dir/$$file; \
		done; \
	done
	chmod 755 configure js/src/configure

stamps/configure-xulrunner:: stamps/prepare-configure debian/xulrunner.mozconfig
	$(if $(wildcard build-xulrunner),,mkdir build-xulrunner)
	cd build-xulrunner && \
	MOZCONFIG=$(CURDIR)/debian/xulrunner.mozconfig \
	ASFLAGS="-g" \
	$(CONFIGURE_ENV) \
	../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(CONFIGURE_FLAGS) \
		--with-default-mozilla-five-home=/usr/lib/xulrunner-$(GRE_VERSION)

stamps/configure-check-xulrunner:: stamps/configure-xulrunner
# Ensure --{with,enable}-system options properly set expected variables
# according to the definitions in the mozconfig file.
	@awk -F' *# *| *$$' ' \
	BEGIN { "find $(CURDIR)/build-xulrunner \\( -name autoconf.mk -o -name emptyvars.mk \\) -printf \"%p \"" | getline confs } \
	$$1 ~ /system/ { \
		if (! $$2) { \
			print FILENAME ": Missing variable for",$$1; \
			error=1; \
		} else { \
			split($$2,var,"="); \
			cmd = "grep -l " var[1] " " confs; \
			cmd | getline dir; \
			sub(/\/[^\/]*$$/, "", dir); \
			cmd = "$(MAKE) -C " dir " --no-print-directory echo-variable-" var[1]; \
			cmd | getline value; \
			if (value != var[2]) { print $$1, "triggered", var[1] "=" value,"instead of",$$2; error=1 } \
		} \
	} \
	END { if (error) { exit 1 }}' debian/xulrunner.mozconfig

stamps/build-xulrunner:: stamps/configure-check-xulrunner
	dh_auto_build --parallel --builddirectory=build-xulrunner -- \
		_LEAKTEST_FILES=leaktest.py \
		JS_READLINE=1

stamps/build-xulrunner-extra:: stamps/build-xulrunner build-xulrunner/debian/extra-stuff/Makefile
	$(MAKE) -C build-xulrunner/debian/extra-stuff


override_dh_auto_build: stamps/build-xulrunner-extra

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#include debian/test.mk
endif

override_dh_auto_clean::
	rm -f MPL
	rm -f $(GENERATED_FILES)
	rm -rf stamps
	debian/rules debian/control TESTDIR=

	dh_auto_clean --builddirectory=build-xulrunner
	find . -name "*.pyc" -o -name "*.pyo" | xargs --no-run-if-empty rm -f

	for dir in $(AUTOCONF_DIRS); do \
		for file in config.guess config.sub; do \
			sed -i '2!b;/^exec "/ d' $$dir/$$file; \
		done; \
	done

override_dh_auto_install: stamps/install-xulrunner

stamps/install-xulrunner:: build-xulrunner/debian/installer/package-manifest
	dh_auto_install --sourcedirectory=build-xulrunner/xulrunner/installer -- \
		MOZ_APP_BASE_VERSION=$(GRE_VERSION) \
		MOZ_PKG_MANIFEST=$(CURDIR)/$< \
		MOZ_NONLOCALIZED_PKG_LIST="xpcom xulrunner debian" \
		MOZ_LOCALIZED_PKG_LIST=en-US \
		SIGN_NSS=

	mv debian/tmp/usr/bin/xulrunner debian/tmp/usr/bin/xulrunner-$(GRE_VERSION)

	# Fixup permissions
	find debian/tmp/usr/share debian/tmp/usr/include -type f -print0 | xargs -0 chmod 644

	# Replace some installed files by symlinks
	for d in lib bin; do \
		cd "$(CURDIR)/debian/tmp/usr/lib/xulrunner-devel-$(GRE_VERSION)/sdk/$$d"; \
		for f in *; do \
			if [ -f "$(CURDIR)/debian/tmp/usr/lib/xulrunner-$(GRE_VERSION)/$$f" ]; then \
				rm -f $$f ; ln -s /usr/lib/xulrunner-$(GRE_VERSION)/$$f $$f; \
			fi; \
		done; \
	done

	dh_auto_install --sourcedirectory=build-xulrunner/js/src -- prefix=/usr MODULE=mozjs FORCE_STATIC_LIB= _MOZBUILD_EXTERNAL_VARIABLES= REAL_LIBRARY=
	cp -r debian/tmp/usr/include/xulrunner-$(GRE_VERSION)/js debian/tmp/usr/include/mozjs
	mv debian/tmp/usr/lib/pkgconfig/mozjs.pc debian/tmp/usr/lib/pkgconfig/mozilla-js.pc
	rm debian/tmp/usr/bin/js-config
	find debian/tmp/usr/include/mozjs -type f -printf %P\\n | while read f; do \
		file=$$(echo debian/tmp/usr/include/xulrunner-$(GRE_VERSION)/$$f); \
		rm $$file; \
		ln -s /usr/include/mozjs/$$f $$file; \
	done
ifndef LESS_SYSTEM_LIBS
	for f in /usr/include/nspr/* /usr/include/nss/*; do \
		ln -s $$f debian/tmp/usr/include/xulrunner-$(GRE_VERSION)/$$(basename $$f); \
	done
endif
	for h in $$(grep -l -r EXPORTS build-xulrunner/media | grep Makefile$ | \
		while read f; do $(MAKE) --no-print-directory -C $$(dirname $$f) echo-variable-EXPORTS echo-variable-EXPORTS_NAMESPACES; done); do \
		rm -rf debian/tmp/usr/include/xulrunner-$(GRE_VERSION)/$$h; \
	done

	mv debian/tmp/usr/bin/js debian/tmp/usr/bin/smjs

	sed -i '/^Cflags/ s,$${includedir},/usr/include/mozjs,' debian/tmp/usr/lib/pkgconfig/mozilla-js.pc

MPL-1.1: gfx/cairo/cairo/COPYING-MPL-1.1
	cp -f $< $@

MPL-2.0: b2g/LICENSE
	cp -f $< $@

override_dh_installdocs: MPL-1.1 MPL-2.0
	dh_installdocs -A $^

stamps/dh_install:: debian/dh/dh_xulrunner debian/noinstall build-xulrunner/dist/bin/npapi_getinfo
	awk '{print "debian/tmp/" $$1 }' < debian/noinstall | xargs rm -r
	dh_install --fail-missing

CMP_AWK := '$$0 !~ /^\t/ { txt = txt ? txt "\n" $$0 : $$0 } END { if (txt != expected) { print "Expected:\n" expected "\nGot:\n" txt; exit 1 }}'

stamps/check-dh_xulrunner-glue:: stamps/dh_install debian/dh/dh_xulrunner
	# Make sure dh_xulrunner is able to spot the current XPCOM standalone glue.
	perl debian/dh/dh_xulrunner_test -pxulrunner-$(GRE_VERSION) | awk -v expected='addsubstvar("xulrunner-$(GRE_VERSION)", "shlibs:Depends", "xulrunner-$(GRE_VERSION)");' $(CMP_AWK)

override_dh_install: stamps/check-dh_xulrunner-glue

override_dh_strip:
	dh_strip -a -Nlibmozjs$(JS_SO_VERSION) -Nspidermonkey-bin --dbg-package=xulrunner-$(GRE_VERSION)-dbg
	dh_strip -plibmozjs$(JS_SO_VERSION) --dbg-package=libmozjs$(JS_SO_VERSION)-dbg
	dh_strip -pspidermonkey-bin

override_dh_shlibdeps:
	dh_shlibdeps -a -l$(CURDIR)/debian/tmp/usr/lib/xulrunner-$(GRE_VERSION) -Xlibdbusservice
	LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/xulrunner-$(GRE_VERSION) dpkg-shlibdeps -Tdebian/xulrunner-$(GRE_VERSION).substvars -dDepends -pgnome $(foreach lib,dbusservice,debian/xulrunner-$(GRE_VERSION)/usr/lib/xulrunner-$(GRE_VERSION)/components/lib$(lib).so)

override_dh_builddeb:
	dh_builddeb -- -Zxz

install binary binary-arch binary-indep: $(GENERATED_FILES)

binary binary-arch binary-indep build clean install:
	dh $@

.PHONY: build clean binary-indep binary-arch binary install

.NOTPARALLEL:

$(shell awk -F:: '$$1 ~ /^stamps\// && !/%/ { print $$1 }' debian/rules) stamps/dummy::
	@mkdir -p stamps
	$(if $(wildcard $@),,touch $@)
endif
