#!/usr/bin/make -f

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH		?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

DEB_MOZ_APPLICATION	:= $(shell dpkg-parsechangelog | sed -n 's/^Source: *\(.*\)$$/\1/ p')
DEB_MOZ_VERSION		:= $(shell echo $(DEB_MOZ_APPLICATION) | sed -e 's/.*-//')

DEBIAN_VERSION		:= $(shell dpkg-parsechangelog | sed -n 's/^Version: *\(.*\)$$/\1/ p')
DEBIAN_REV_CODE		:= $(shell echo $(DEBIAN_VERSION) | sed 's/^.*-\([0-9]*\)[^-]*$$/0\1/ ; s/^.*\(..\)$$/\1/')
DEBIAN_XUL_VER		 = $(shell tail -1 mozilla/config/milestone.txt)
DEBIAN_XUL_DIR		 = /usr/lib/xulrunner-$(DEBIAN_XUL_VER)
DEBIAN_XUL_INCLUDE_DIR	 = /usr/include/xulrunner-$(DEBIAN_XUL_VER)
DEB_SRCDIR		:= mozilla

MOZCLIENT_PROJECTNAME   := $(DEB_MOZ_APPLICATION)
MOZCLIENT_PROJECTDIR    := $(CURDIR)/debian/mozclient

# minVersion and maxVersion used in install.rdf
EM_TRANSLATION_VERSION		= $(DEBIAN_XUL_VER)
EM_TRANSLATION_MIN_VERSION	= 1.9
EM_TRANSLATION_MAX_VERSION	= $(DEBIAN_XUL_VER)

EXTRA_SYSTEM_CONFIGURE_FLAGS = $(NULL)

WANT_UNIT_TESTS ?= 1
$(info WANT_UNIT_TESTS=$(WANT_UNIT_TESTS))
ifeq (1,$(WANT_UNIT_TESTS))
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-tests --enable-mochitest
else
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-tests --disable-mochitest
endif

USE_SYSTEM_CAIRO := $(shell pkg-config --exists 'cairo >= 1.8.8' && echo 1)
# for old cairo versions we cannot use system cairo
ifeq (1,$(USE_SYSTEM_CAIRO))
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-system-cairo
else
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-system-cairo
endif

USE_SYSTEM_SQLITE :=  $(shell pkg-config --exists 'sqlite3 >= 3.6.16.1' && echo 1)
ifeq (1,$(USE_SYSTEM_SQLITE))
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-system-sqlite
else
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-system-sqlite
endif

USE_SYSTEM_NSPR := $(shell pkg-config --exists 'nspr >= 4.8.0' && echo 1)
ifeq (1,$(USE_SYSTEM_NSPR))
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --with-system-nspr
endif

USE_SYSTEM_NSS := $(shell pkg-config --exists 'nss >= 3.12.3' && echo 1)
ifeq (1,$(USE_SYSTEM_NSS))
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --with-system-nss
endif

USE_SYSTEM_HUNSPELL := $(shell pkg-config --exists 'hunspell >= 1.2' && echo 1)
ifeq (1,$(USE_SYSTEM_HUNSPELL))
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-system-hunspell
else
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-system-hunspell
endif

# translation flags

MOZ_TRANSLATION_AUTO_XPI_OUTDIR		:= debian/lp-export-xpis/
MOZ_TRANSLATION_AUTO_XPI_SEARCHDIRS	:= debian/$(DEB_MOZ_APPLICATION)
MOZ_TRANSLATION_AUTO_XPI_LOCALE		:= en-US

# enable below to disable symbolic-functions linkage
# LDFLAGS=$(shell echo $$LDFLAGS | sed -e 's/-Wl,-Bsymbolic-functions//')
# export LDFLAGS

DEB_AUTO_UPDATE_AUTOCONF=2.13

# workaround cdbs wrongly unapplying patches before cleaning up
clean:: 
	[ ! -f $(DEB_SRCDIR)/Makefile ] || $(MAKE) -C $(DEB_SRCDIR) distclean

ifneq (,$(wildcard *.tar.bz2)$(DEBIAN_MOZCLIENT_EMBEDDED))
   DEB_TAR_SRCDIR := mozilla
   MOZCLIENT_EMBEDDED = 1
   DEBIAN_XUL_VER = $(shell tail -1 build-tree/mozilla/config/milestone.txt)
   include /usr/share/cdbs/1/rules/tarball.mk
endif
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/patchsys-quilt.mk
include /usr/share/cdbs/1/class/autotools.mk
include $(CURDIR)/debian/mozclient/$(DEB_MOZ_APPLICATION).mk
include /usr/share/mozilla-devscripts/lp-locale-export.mk

PROCESSORS := $(shell grep -ic ^processor /proc/cpuinfo)
ifeq (0,$(PROCESSORS))
PROCESSORS := 1
endif
ifneq (1,$(PROCESSORS))
DEB_MAKE_EXTRA_ARGS += -j$(PROCESSORS)
endif

ifeq (1,$(WANT_UNIT_TESTS))
# IGNORE_TEST_FAILURES	 = --ignore-errors
DEB_MAKE_CHECK_TARGET	 = $(IGNORE_TEST_FAILURES) check || exit 0
endif

DEB_MAKE_CLEAN_TARGET	 = $(NULL)
# We don't want build-tree/mozilla/README to be shipped as a doc
DEB_INSTALL_DOCS_ALL	:= $(NULL)
MOZ_APPLICATION		 = xulrunner

# List all extensions we want
DEB_MOZ_EXTENSIONS=default

CFLAGS   = -g
CXXFLAGS = -g

BUILD_OFFICIAL = 1
export BUILD_OFFICIAL

DEB_CONFIGURE_USER_FLAGS= \
	$(EXTRA_SYSTEM_CONFIGURE_FLAGS) \
	--enable-application=$(MOZ_APPLICATION) \
	--enable-extensions=$(DEB_MOZ_EXTENSIONS) \
	--with-default-mozilla-five-home=$(DEBIAN_XUL_DIR) \
	--enable-safe-browsing \
	--enable-startup-notification \
	--with-user-appdir=.mozilla \
	--without-system-jpeg \
	--with-system-zlib=/usr \
	--with-system-bz2=/usr \
	--disable-javaxpcom \
	--disable-crashreporter \
	--disable-elf-dynstr-gc \
	--disable-installer \
	--disable-strip \
	--disable-strip-libs \
	--disable-install-strip \
	--disable-updater \
	--enable-optimize \
	--enable-libnotify \
	--with-distribution-id=com.ubuntu \
	$(NULL)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEB_CONFIGURE_USER_FLAGS += \
		--disable-optimize \
		--enable-optimize="-O0 -g" \
		$(NULL)
endif

NOT_EXECUTABLE = \
	*.xml \
	*.js  \
	*.png \
	*.xpm \
	*.h \
	*.idl \
	*.txt \
	LICENSE \
	$(NULL)

ifeq (1,$(USE_SYSTEM_NSS))
binary-install/$(DEB_MOZ_APPLICATION)::
	dh_link -p$(DEB_MOZ_APPLICATION) usr/lib/nss/libnssckbi.so $(DEBIAN_XUL_DIR)/libnssckbi.so
endif

binary-install/$(DEB_MOZ_APPLICATION)::
	touch debian/xulrunner-$(DEB_MOZ_VERSION)/$(DEBIAN_XUL_DIR)/.autoreg
	dh_link -p$(DEB_MOZ_APPLICATION) usr/lib/xulrunner-addons/extensions  $(DEBIAN_XUL_DIR)/extensions
	dh_link -p$(DEB_MOZ_APPLICATION) usr/lib/xulrunner-addons/plugins  $(DEBIAN_XUL_DIR)/plugins
	dh_link -p$(DEB_MOZ_APPLICATION) $(DEBIAN_XUL_DIR)/xulrunner /usr/bin/$(DEB_MOZ_APPLICATION)
	dh_link -p$(DEB_MOZ_APPLICATION) $(DEBIAN_XUL_DIR)/xpcshell /usr/bin/xpcshell-$(DEB_MOZ_VERSION)
ifneq (1,$(USE_SYSTEM_SQLITE))
	dh_link -p$(DEB_MOZ_APPLICATION) $(DEBIAN_XUL_DIR)/libsqlite3.so $(DEBIAN_XUL_DIR)/libsqlite3.so.0
endif
	dh_install -p$(DEB_MOZ_APPLICATION) debian/system-greprefs.js /etc/xulrunner-$(DEB_MOZ_VERSION)/ 
	dh_link -p$(DEB_MOZ_APPLICATION) /etc/xulrunner-$(DEB_MOZ_VERSION)/system-greprefs.js $(DEBIAN_XUL_DIR)/greprefs/system-greprefs.js

LOCALE_VERSION = $(shell echo $(DEBIAN_VERSION) | sed -e 's/~hg.*/pre/' | tr -d '~')

binary-post-install/$(DEB_MOZ_APPLICATION):: auto-locale-xpi.mk/translate fix-permissions test-results
	dh_install -p$(DEB_MOZ_APPLICATION)-dev debian/build-system.tar.gz /usr/lib/xulrunner-devel-$(DEBIAN_XUL_VER)/sdk/
	dh_install -p$(DEB_MOZ_APPLICATION)-testsuite debian/test-results.tar.gz /usr/lib/xulrunner-devel-$(DEBIAN_XUL_VER)/
	sed \
		-e 's/@EM_VERSION@/$(EM_TRANSLATION_VERSION)/' \
		-e 's/@EM_TOOLKIT_MIN_VERSION@/$(EM_TRANSLATION_MIN_VERSION)/' \
		-e 's/@EM_TOOLKIT_MAX_VERSION@/$(EM_TRANSLATION_MAX_VERSION)/' \
		< debian/translation-support/install.rdf.in > debian/install.rdf
	cd debian && zip lp-export-xpis/en-US.xpi install.rdf
ifeq (,$(findstring $(EM_TRANSLATION_VERSION), $(LOCALE_VERSION)))
	$(warning EM_TRANSLATION_VERSION=$(EM_TRANSLATION_VERSION) LOCALE_VERSION=$(LOCALE_VERSION). You need to explicitly adjust the translation version and min/max bounds.)
endif

binary-predeb/$(DEB_MOZ_APPLICATION):: compare

binary-install/$(DEB_MOZ_APPLICATION)-dev::
ifeq (1,$(USE_SYSTEM_NSPR))
	dh_link -p$(DEB_MOZ_APPLICATION)-dev /usr/bin/nspr-config $(DEBIAN_XUL_DIR)/nspr-config
	dh_link -p$(DEB_MOZ_APPLICATION)-dev /usr/include/nspr $(DEBIAN_XUL_INCLUDE_DIR)/nspr
else
	dh_install -p$(DEB_MOZ_APPLICATION)-dev debian/tmp/$(DEBIAN_XUL_DIR)/nspr-config
endif

TEST_RESULTS = \
	tools/test-harness \
	_leaktest \
	_profile \
	_tests \
	$(NULL)

test-results: debian/test-results-stamp
debian/test-results-stamp:
	( cd $(DEB_BUILDDIR) ; tar zcvfh $(CURDIR)/debian/test-results.tar.gz $(TEST_RESULTS) )
	touch $@

FIND_EXP = $(foreach ext,$(NOT_EXECUTABLE),-name '$(ext)' -o)
FIND_NOT_EXECUTABLE = $(wordlist 1, $(shell expr $(words $(FIND_EXP)) - 1), $(FIND_EXP))

post-patches:: debian/stamp-autotools-files-moz
debian/stamp-autotools-files-moz:
	# Number of processors: $(PROCESSORS) [$(shell grep -E '^(model name|Processor)' /proc/cpuinfo | head -1)]
	if [ "Z$(DEB_AUTO_UPDATE_AUTOCONF)" != Z ] || [ ! -e $(DEB_SRCDIR)/js/src/configure ]; \
	then cd $(DEB_SRCDIR)/js/src && `which autoconf$(DEB_AUTO_UPDATE_AUTOCONF) || which autoconf`; fi
	touch $@

subst_files = \
	debian/$(DEB_MOZ_APPLICATION).preinst \
	debian/$(DEB_MOZ_APPLICATION).postinst \
	debian/$(DEB_MOZ_APPLICATION).prerm \
	debian/$(DEB_MOZ_APPLICATION).postrm \
	$(NULL)

# debian/<pkg>.install.<arch> files to be generated from debian/<pkg>.install.<arch>.pp
# install_files = \
# 	$(patsubst %.pp,%,$(foreach file,$(DEB_ALL_PACKAGES),$(wildcard debian/$(file).install.$(DEB_BUILD_ARCH).pp))) \
# 	$(NULL)

post-patches:: $(subst_files) $(install_files) debian/build-system.tar.gz

debian/build-system.tar.gz:
	# Create a build-system for xulrunner applications
	sh debian/create-build-system.sh

fix-permissions:
	# Fix permissions
	cd debian/tmp ; \
	find . -type f \( $(FIND_NOT_EXECUTABLE) \) -perm -u=x -exec chmod -c 644 {} \;

%: %.pp
	# Pre-processing $@
	perl $(DEB_BUILDDIR)/config/preprocessor.pl $< > $@

%: %.in
	sed -e 's,@XULDIR@,$(DEBIAN_XUL_DIR),g' \
	    -e 's,@XULVER@,$(DEBIAN_XUL_VER),g' \
	    -e 's,@XULBRANCH@,$(DEB_MOZ_VERSION),g' \
	< $< > $@

clean::
	rm -f $(subst_files) $(install_files) debian/install.rdf
	rm -f debian/test-results.tar.gz debian/test-results-stamp
	rm -f debian/build-system.tar.gz
	rm -f debian/stamp-autotools-files-moz
