#!/usr/bin/make -f

include $(CURDIR)/debian/build/mozvars.mk
include $(CURDIR)/debian/config/branch.mk

MOZ_PKG_BASENAME	:= thunderbird
MOZ_APP			:= mail
MOZ_VENDOR		:=
MOZ_MOZDIR		:= mozilla
MOZ_DEFAULT_APP_BASENAME := Thunderbird

MOZ_BRANDING		:= $(CHANNEL)
ifneq (,$(filter release beta, $(MOZ_BRANDING)))
MOZ_BRANDING		:= official
endif
ifeq (1,$(MOZ_BUILD_UNOFFICIAL))
ifneq (,$(filter official aurora, $(MOZ_BRANDING)))
MOZ_BRANDING		:= nightly
endif
endif
ifeq (official,$(MOZ_BRANDING))
MOZ_BRANDING_DIR	:= other-licenses/branding/thunderbird
MOZ_BRANDING_OPTION	:= --enable-official-branding
else
MOZ_BRANDING_DIR	:= $(MOZ_APP)/branding/$(MOZ_BRANDING)
MOZ_BRANDING_OPTION	:= --with-branding=$(MOZ_BRANDING_DIR)
endif

MOZ_SEARCHPLUGIN_DIR	= $(MOZ_LIBDIR)/distribution/searchplugins

ifeq (,$(filter lucid maverick natty, $(DISTRIB_CODENAME)))
MOZ_ENABLE_UNITY_INTEGRATION = 1
ifneq (,$(filter oneiric, $(DISTRIB_CODENAME)))
PKG_GS_EXTRA_ARGS	:= "-Vunity:Depends=libindicate5, libdbusmenu-gtk4, libunity6, libebook1.2-12, libedataserver1.2-15"
else
ifneq (,$(filter precise, $(DISTRIB_CODENAME)))
PKG_GS_EXTRA_ARGS	:= "-Vunity:Depends=libindicate5, libdbusmenu-gtk4, libunity9, libebook-1.2-12, libedataserver-1.2-15"
else
PKG_GS_EXTRA_ARGS	:= "-Vunity:Depends=libmessaging-menu0, libunity9, libebook-1.2-14, libedataserver-1.2-17"
endif
endif
endif

# Make sure thunderbird-couchdb is removed on upgrade to 12.10
# See https://launchpad.net/bugs/1040839
ifeq (,$(filter lucid maverick natty oneiric precise, $(DISTRIB_CODENAME)))
TB_COUCHDB_BREAKS	:= thunderbird-couchdb
endif

ifeq (thunderbird,$(MOZ_PKG_NAME))
# We need to add an epoch to the version of the thunderbird-locale- packages. Don't do this for thunderbird-trunk-locale- though
LOCALE_PACKAGES := $(shell cat $(CURDIR)/debian/control | grep "^Package:[[:space:]]*$(MOZ_PKG_NAME)-locale\-" | sed -n -e 's/^Package\:[[:space:]]*\([^[:space:]]*\)/\1/ p')
DEB_VERSION	:= $(shell dpkg-parsechangelog | sed -n -e '/^Version:/s/.*: //p')
$(foreach locale_package, $(LOCALE_PACKAGES), $(eval PKG_$(locale_package)_EXTRA_ARGS = -v1:$(DEB_VERSION)))

PKG_APP_REPLACES_ARGS	:= mozilla-thunderbird, thunderbird-gnome-support (<= 3.0.4+nobinonly-0ubuntu3)
PKG_APP_CONFLICTS_ARGS	:= mozilla-thunderbird
PKG_APP_BREAKS_ARGS	:= thunderbird-gnome-support (<= 3.0.4+nobinonly-0ubuntu3), $(TB_COUCHDB_BREAKS)
else
PKG_APP_BREAKS_ARGS	:= $(TB_COUCHDB_BREAKS)
endif

PKG_SUPPORT_SUGGESTS	:= $(MOZ_PKG_NAME)-gnome-support

ifeq (1,$(MOZ_ENABLE_UNITY_INTEGRATION))
MOZ_DEFINES += -DMOZ_ENABLE_UNITY_INTEGRATION
endif

MOZ_PKGNAME_SUBST_FILES = \
	debian/$(MOZ_PKG_BASENAME).sh \
	debian/apport/blacklist \
	debian/apport/native-origins \
	debian/apport/source_$(MOZ_PKG_NAME).py \
	$(NULL)

include $(CURDIR)/debian/build/mozbuild.mk

make-messagingmenu-addon: debian/messagingmenu/messagingmenu@mozilla.com.xpi
debian/messagingmenu/messagingmenu@mozilla.com.xpi:
	cd $(CURDIR)/debian/messagingmenu; \
		sh build.sh || exit 1; \
		mv $(CURDIR)/debian/messagingmenu/messagingmenu.xpi $(CURDIR)/debian/messagingmenu/messagingmenu@mozilla.com.xpi

make-eds-addon: debian/eds/edsintegration@mozilla.com.xpi
debian/eds/edsintegration@mozilla.com.xpi:
	cd $(CURDIR)/debian/eds; \
		LIBXUL_SDK=$(CURDIR)/$(MOZ_DISTDIR) sh build.sh || exit 1; \
		mv $(CURDIR)/debian/eds/edsintegration.xpi $(CURDIR)/debian/eds/edsintegration@mozilla.com.xpi

common-build-arch:: make-messagingmenu-addon make-eds-addon

binary-install/$(MOZ_PKG_NAME)::
	convert -resize 32x32 debian/$(MOZ_PKG_NAME)/usr/share/pixmaps/$(MOZ_APP_NAME).png debian/$(MOZ_PKG_NAME)/usr/share/pixmaps/$(MOZ_APP_NAME).xpm
ifeq (1, $(MOZ_ENABLE_UNITY_INTEGRATION))
	echo "/usr/share/applications/$(MOZ_APP_NAME).desktop" > debian/$(MOZ_PKG_NAME)/usr/share/indicators/messages/applications/$(MOZ_APP_NAME)
endif

pre-build::
	$(shell /usr/bin/docbook-to-man $(CURDIR)/debian/thunderbird.sgml > $(CURDIR)/debian/$(MOZ_APP_NAME).1)

ifneq ($(MOZ_APP_BASENAME),$(MOZ_DEFAULT_APP_BASENAME))
	@echo "Changing Name in application.ini to $(MOZ_APP_BASENAME)"
	@cp $(DEB_SRCDIR)/mail/app/application.ini $(DEB_SRCDIR)/mail/app/application.ini.bak; \
		sed -i 's/^\(Name\=\).*/\1$(MOZ_APP_BASENAME)/' $(DEB_SRCDIR)/mail/app/application.ini
	@echo "Setting MOZ_APP_UA_NAME to $(MOZ_DEFAULT_APP_BASENAME)"
	@cp $(DEB_SRCDIR)/mail/confvars.sh $(DEB_SRCDIR)/mail/confvars.sh.bak; \
		echo "MOZ_APP_UA_NAME=$(MOZ_DEFAULT_APP_BASENAME)" >> $(DEB_SRCDIR)/mail/confvars.sh
endif
ifneq ($(MOZ_APP_NAME),$(MOZ_DEFAULT_APP_NAME))
	@echo "Changing MOZ_APP_NAME to $(MOZ_APP_NAME)"
	@if [ ! -e $(DEB_SRCDIR)/mail/confvars.sh.bak ] ; \
	then \
		cp $(DEB_SRCDIR)/mail/confvars.sh $(DEB_SRCDIR)/mail/confvars.sh.bak ; \
	fi ; \
	sed -i 's/^\(MOZ_APP_NAME\=\).*/\1$(MOZ_APP_NAME)/' $(DEB_SRCDIR)/mail/confvars.sh
	@$(foreach file,mail/app/profile/all-thunderbird.js $(MOZ_BRANDING_DIR)/thunderbird-branding.js, \
		echo "Replacing instances of \"%APP%\" with \"thunderbird\" in $(file)"; \
		sed -ri 's/%APP%/thunderbird/g' $(DEB_SRCDIR)/$(file);)
endif

clean::
	rm -f debian/messagingmenu/*.xpi
	rm -f debian/eds/*.xpi
	rm -f debian/eds/components/*.xpt
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/mail/app/application.ini)
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/mail/confvars.sh)
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/mail/app/profile/all-thunderbird.js)
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/thunderbird-branding.js)
	rm -f debian/$(MOZ_APP_NAME).1
