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

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

# checking the release type, if $(RELEASE)=UNRELEASED we won't build
# the thunderbird-dbg package
include /usr/share/dpkg/pkg-info.mk
RELEASE := $(DEB_DISTRIBUTION)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

ICU_DATA_FILE := $(shell basename config/external/icu/data/icud*.dat)
ICU_BUILD := $(shell if ! grep -qs -e "--with-system-icu" debian/mozconfig.default; then echo YES || NO; fi)
ICU_BUILD_DIR := $(CURDIR)/obj-thunderbird/obj-icu
ICU_BUILD_CONFIGURE := --with-data-packaging=archive --enable-static --disable-layoutex --disable-shared --disable-extras --disable-icuio --disable-layout --disable-tests --disable-samples --disable-strict
ICU_BUILD_CPPFLAGS := "-DU_NO_DEFAULT_INCLUDE_UTF_HEADERS=1 -DUCONFIG_NO_LEGACY_CONVERSION -DUCONFIG_NO_TRANSLITERATION -DUCONFIG_NO_REGULAR_EXPRESSIONS -DUCONFIG_NO_BREAK_ITERATION -DU_CHARSET_IS_UTF8"

THUNDERBIRD_VERSION=$(shell tail -1 comm/mail/config/version.txt)
DEB_AUTO_UPDATE_AUTOCONF=2.13

# build directory used for building the thunderbird-l10n files
THUNDERBIRD_L10N_BUILDDIR=debian/thunderbird-l10n-build

# Iceowl/Lightning version, note this is different from the TB version!
LIGHTNING_VERSION := $(shell python $(CURDIR)/comm/calendar/lightning/build/makeversion.py ${THUNDERBIRD_VERSION})

# ID of the l10n language packs from upstream, need to match the application.id
# in thunderbird-l10n/$LANG/manifest.json
export MOZ_LANGPACK_ID = $(shell grep MOZ_LANGPACK_EID comm/mail/locales/Makefile.in | cut -f2 -d @)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

# currently needed as workaround for GCC-6 built
# see https://bugzilla.mozilla.org/show_bug.cgi?id=1251576
# and https://bugzilla.mozilla.org/show_bug.cgi?id=1245783#c31
CFLAGS += -fno-delete-null-pointer-checks -fno-schedule-insns2 -fno-lifetime-dse
CXXFLAGS += -fno-delete-null-pointer-checks -fno-schedule-insns2 -fno-lifetime-dse

# special CFLAGS for various platforms
ifeq ($(DEB_BUILD_ARCH),armel)
	CFLAGS += -D__ARM_PCS
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,--no-keep-memory -Wl,--reduce-memory-overheads

# Add execution time and memory usage stats in the logs
LDFLAGS += -Wl,--stats

export MOZ_BUILD_DATE := $(SOURCE_DATE_EPOCH)
export BUILD_DATE := $(shell dpkg-parsechangelog --show-field=Date)
export MOZCONFIG=$(shell pwd)/mozconfig.thunderbird
export MOZILLA_OFFICIAL=1
export DEB_BUILD_GNU_TYPE
export DEB_HOST_GNU_TYPE
export DEB_BUILD_OPTIONS
# Some Debian build tools clear out some variables
export SHELL=/bin/bash

%:
	dh $@

build:
	dh $@

override_dh_auto_clean:
	dh_auto_clean --builddirectory=obj-thunderbird
	# needed for thunderbird-l10n
	rm -rf $(THUNDERBIRD_L10N_BUILDDIR)

override_dh_auto_configure:
	# run autoconf for all configure files
	if [ "Z$(DEB_AUTO_UPDATE_AUTOCONF)" != Z ] || [ ! -e $(CURDIR)/configure ]; then \
		cd $(CURDIR) && `which autoconf$(DEB_AUTO_UPDATE_AUTOCONF) || which autoconf`; fi
	if [ "Z$(DEB_AUTO_UPDATE_AUTOCONF)" != Z ] || [ ! -e $(CURDIR)/js/src/configure ]; then \
		cd $(CURDIR)/js/src && `which autoconf$(DEB_AUTO_UPDATE_AUTOCONF) || which autoconf`; fi
	# copy the mozconfig files
	cp debian/mozconfig.* .
	echo 'mk_add_options MOZ_OBJDIR=$(CURDIR)/obj-thunderbird' >> mozconfig.thunderbird
	echo 'ac_add_options --prefix=$(CURDIR)/debian/tmp/usr' >> mozconfig.default
	@echo "rebuild ICU data file needed: $(ICU_BUILD)"
ifeq ($(ICU_BUILD),YES)
	####################################################################
	# (re)build upstream icudt*.dat due disabled usage of system files #
	####################################################################
	rm -f $(CURDIR)/config/external/icu/data/$(ICU_DATA_FILE)
	mkdir -p $(ICU_BUILD_DIR)
	cd $(ICU_BUILD_DIR) ;\
		$(CURDIR)/intl/icu/source/configure $(ICU_BUILD_CONFIGURE) CPPFLAGS=$(ICU_BUILD_CPPFLAGS) ;\
		$(MAKE) VERBOSE=1 -j$(NUMJOBS)
	@ICU_DATA_FILE_CREATED=$$(basename $(ICU_BUILD_DIR)/data/out/icud*.dat) ;\
	test -f $(ICU_BUILD_DIR)/data/out/$$ICU_DATA_FILE_CREATED && cp $(ICU_BUILD_DIR)/data/out/$$ICU_DATA_FILE_CREATED $(CURDIR)/config/external/icu/data ;\
	echo "created new ICU_DATA_FILE in $(CURDIR)/config/external/icu/data/$$ICU_DATA_FILE_CREATED"
endif
	# configure the various build settings
	# thunderbird, calendar-google-provider, lightning
	DIST= ./mach -v configure

override_dh_auto_build:
	# building the stuff
	./mach -v build
	# build thunderbird-l10n
	mkdir -p $(THUNDERBIRD_L10N_BUILDDIR)
	cd $(CURDIR)/thunderbird-l10n ;\
	for XPI in *; do \
		locale=`basename $${XPI}` ;\
		USED_LANGPACK_ID=`grep langpack- $${XPI}/manifest.json | tr '"' ' ' | awk '{print $$3}' | cut -f2-3 -d @` ;\
		if [ "$${USED_LANGPACK_ID}" != "$${MOZ_LANGPACK_ID}" ]; then \
			echo "MOZ_LANGPACK_ID mismatch! '$${USED_LANGPACK_ID}' != '$${MOZ_LANGPACK_ID}'" ;\
			echo "thunderbird-l10n l10n source '$${XPI}' uses a different MOZ_LANGPACK_ID than defined in $(CURDIR)/comm/mail/locales/Makefile.in!";\
			exit 1 ;\
		else \
			xpi-pack $${XPI} ../$(THUNDERBIRD_L10N_BUILDDIR)/langpack-$${locale}@$${MOZ_LANGPACK_ID}.xpi ;\
		fi \
	done

override_dh_auto_install:
ifneq (,$(filter mips,$(DEB_BUILD_ARCH)))
	sed -i '/"javascript.options.\(baselinejit\|ion\)"/s/true/false/' obj-thunderbird/dist/bin/greprefs.js
endif
	# install thunderbird into temp install folder
	./mach -v install
	# install, symlinking thunderbird-l10n packages
	cd $(CURDIR)/thunderbird-l10n ;\
	for LANG in *; do \
		locale=`basename $${LANG}` ;\
		lowercase_locale=`echo $${locale} | tr 'A-Z' 'a-z'` ;\
		echo "preparing and working on 'thunderbird-l10n-$${lowercase_locale}  (langpack-$${locale}@$${MOZ_LANGPACK_ID}.xpi)" ;\
		mkdir -p $(CURDIR)/debian/thunderbird-l10n-$${lowercase_locale}/usr/lib/thunderbird/extensions/ ;\
		install -D -m 644 ../$(THUNDERBIRD_L10N_BUILDDIR)/langpack-$${locale}@$${MOZ_LANGPACK_ID}.xpi $(CURDIR)/debian/thunderbird-l10n-$${lowercase_locale}/usr/share/thunderbird/extensions/langpack-$${locale}@$${MOZ_LANGPACK_ID}.xpi ;\
		ln -sf /usr/share/thunderbird/extensions/langpack-$${locale}@$${MOZ_LANGPACK_ID}.xpi \
			$(CURDIR)/debian/thunderbird-l10n-$${lowercase_locale}/usr/lib/thunderbird/extensions/langpack-$${locale}@$${MOZ_LANGPACK_ID}.xpi ;\
	done
	# install lightning-l10n
	cd $(CURDIR)/lightning-l10n ;\
	for LANG in *; do \
		locale=`basename $${LANG}` ;\
		lowercase_locale=`echo $${locale} | tr 'A-Z' 'a-z'` ;\
		echo "working on 'lightning-l10n-$${lowercase_locale}'" ;\
		mkdir -p $(CURDIR)/debian/lightning-l10n-$${lowercase_locale}/usr/share/lightning ;\
		cp -a $${locale}/* $(CURDIR)/debian/lightning-l10n-$${lowercase_locale}/usr/share/lightning ;\
	done
	dh_bash-completion

override_dh_install-arch:
	# in case of a pre-builded and installed icudt*.dat file pick it up
	if [ -f debian/tmp/usr/lib/thunderbird/$(ICU_DATA_FILE) ]; then \
		if ! grep -qs $(ICU_DATA_FILE) debian/thunderbird.install ; then \
			echo "debian/tmp/usr/lib/thunderbird/$(ICU_DATA_FILE)" >> debian/thunderbird.install ;\
		fi \
	fi
	dh_install
	# install apparmor profile
	cp debian/apparmor/usr.bin.thunderbird debian/thunderbird/etc/apparmor.d/usr.bin.thunderbird
	dh_apparmor --profile-name=usr.bin.thunderbird -pthunderbird

override_dh_install-indep:
	dh_install
	# prepare calendar-google-provider
	mkdir -p debian/calendar-google-provider/usr/share/xul-ext/calendar-google-provider/
	GDATA_PROVIDER=`find -type f -name "gdata-provider*.xpi"` &&\
		unzip -d debian/calendar-google-provider/usr/share/xul-ext/calendar-google-provider/ $(CURDIR)/$$GDATA_PROVIDER
	find debian/calendar-google-provider/usr/share/xul-ext/calendar-google-provider -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	ID=`grep "em:id" $(CURDIR)/debian/calendar-google-provider/usr/share/xul-ext/calendar-google-provider/install.rdf | sed -e s"/<em:id>"// -e s",</em:id>",, -e 's/^[ ]*//' | head -n1` ;\
	mkdir -p mkdir -p $(CURDIR)/debian/calendar-google-provider/usr/lib/thunderbird/extensions/ ;\
	ln -sf /usr/share/xul-ext/calendar-google-provider $(CURDIR)/debian/calendar-google-provider/usr/lib/thunderbird/extensions/$$ID
	# sometimes there are temporary build files in calendar-google-provider
	@echo "    --> searching for temporary build files in 'calendar-google-provider' ..."
	@for i in `find debian/calendar-google-provider/ -name ".mkdir.done*"`; do echo remove $$i && rm $$i; done
	# prepare lightning
	mkdir -p debian/lightning/usr/share/lightning
	LIGHTNING=`find -type f -name "lightning*.xpi"` &&\
		unzip -d debian/lightning/usr/share/lightning $(CURDIR)/$$LIGHTNING
	# remove executable rights on *.js, *.png, *.xul files
	find debian/lightning*/ -type f \( -name "*.dtd" -o -name "*.js" -o -name "*.png" -o -name "*.properties" -o -name "*.xul" \) -exec chmod 644 {} \;
	# sometimes there are temporary build files in lightning
	@echo "    --> searching for temporary build files in 'lightning' ..."
	@for i in `find debian/lightning/ -name ".mkdir.done*"`; do echo remove $$i && rm $$i; done
	find debian/lightning/usr/share/lightning -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	for LANG in lightning-l10n/*; do \
		locale=`basename $${LANG}`; \
		echo "locale calendar $${locale} chrome/calendar-$${locale}/locale/$${locale}/calendar/" >> debian/lightning/usr/share/lightning/chrome.manifest ;\
		echo "locale lightning $${locale} chrome/lightning-$${locale}/locale/$${locale}/lightning/" >> debian/lightning/usr/share/lightning/chrome.manifest ;\
	done

override_dh_strip:
	if [ "$(RELEASE)" != "UNRELEASED" ]; then \
		dh_strip --dbg-package=thunderbird-dbg ;\
	else \
		dh_strip --no-automatic-dbgsym ;\
	fi

override_dh_shlibdeps:
	dh_shlibdeps -a -l $(CURDIR)/debian/tmp/usr/lib/thunderbird

override_dh_builddeb:
	# just build all packages if there is no 'UNRELEASED' within the changelog
	# found, otherwise skip the thunderbird-dbg package, it isn't there
	if [ "$(RELEASE)" != "UNRELEASED" ]; then \
		dh_builddeb ;\
	else \
		dh_builddeb -pthunderbird ;\
		dh_builddeb -picedove ;\
		dh_builddeb -picedove-dev ;\
		dh_builddeb -plightning ;\
		dh_builddeb -piceowl-extension ;\
		dh_builddeb -pcalendar-google-provider ;\
		for package in `grep -e \
			'Package: thunderbird-l10n\|Package: lightning-l10n\|Package: icedove-l10n\|Package: iceowl-l10n' debian/control | awk '{print $$2;}'`; do \
			dh_builddeb -p$$package ;\
		done ;\
		sed -i '/thunderbird-dbg/d' debian/files ;\
		sed -i '/icedove-dbg/d' debian/files ;\
	fi

override_dh_lintian:
	# adding a lintian override about a non spelling issues that lintian is
	# false positive detecting
	for lintian_overrides in `grep 'Package: ' debian/control | awk '{print $$2;}'`; do \
		echo "$$lintian_overrides: spelling-error-in-copyright Synopsys Synopsis" >> debian/$$lintian_overrides.lintian-overrides ;\
	done
	dh_lintian

.PHONY: build
