#!/usr/bin/make -f

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

MOZ_PKG_BASENAME	:= abrowser
MOZ_APP			:= browser
MOZ_VENDOR		:= Mozilla
MOZ_MOZDIR		:=

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		:= unofficial
endif
endif
MOZ_BRANDING_DIR 	:= $(MOZ_APP)/branding/$(MOZ_BRANDING)
ifeq (official,$(BRANDING))
MOZ_BRANDING_OPTION	:= --enable-official-branding
else
MOZ_BRANDING_OPTION	:= --with-branding=$(MOZ_BRANDING_DIR)
endif

ifneq (,$(filter oneiric, $(DISTRIB_CODENAME)))
MOZ_SEARCHPLUGIN_DIR	= $(MOZ_ADDONDIR)/distribution/searchplugins
else
MOZ_SEARCHPLUGIN_DIR	= $(MOZ_LIBDIR)/distribution/searchplugins
endif

ifeq (abrowser, $(MOZ_APP_NAME))
ifneq (,$(filter lucid, $(DISTRIB_CODENAME)))
PLUGIN_BREAKS := flashplugin-installer (<= 11.2.202.233ubuntu0.10.04.1), adobe-flashplugin (<= 11.1.102.63-0lucid1)
PLUGIN_CONFLICTS := gcu-plugin (<= 0.10.12-2ubuntu1), gecko-mediaplayer (<= 0.9.9.2-1ubuntu0.10.04.1), \
		    mozilla-gtk-vnc (<= 0.3.10-2ubuntu2.1), mozilla-opensc  (<= 0.11.12-1ubuntu3.2), \
		    mozilla-plugin-pcmanx (<= 0.3.9-2ubuntu2), mozplugger (<= 1.13.3-1ubuntu1), \
		    xine-plugin (<= 1.0.2-1ubuntu2), mozilla-virt-viewer (<= 0.0.3-6ubuntu7.xul191.1)
else
ifneq (,$(filter maverick, $(DISTRIB_CODENAME)))
PLUGIN_BREAKS := flashplugin-installer (<= 11.2.202.228ubuntu0.10.10.1), adobe-flashplugin (<= 11.1.102.63-0maverick1)
PLUGIN_CONFLICTS := gcu-plugin (<= 0.12.3-1), gecko-mediaplayer (<= 0.9.9.2-1ubuntu0.10.10.1), \
		    mozilla-gtk-vnc (<= 0.4.1-3ubuntu2), mozilla-opensc (<= 0.11.13-1ubuntu2.1), \
		    mozilla-plugin-pcmanx (<= 0.3.9-2ubuntu2), mozplugger (<= 1.14.1-2~exp3ubuntu1), \
		    xine-plugin (<= 1.0.2-2ubuntu1)
else
ifneq (,$(filter natty, $(DISTRIB_CODENAME)))
PLUGIN_BREAKS := flashplugin-installer (<= 11.2.202.233ubuntu0.11.04.2), adobe-flashplugin (<= 11.1.102.63-0natty1)
PLUGIN_CONFLICTS := gcu-plugin (<=  0.12.7-1ubuntu1)
else
ifneq (,$(filter oneiric, $(DISTRIB_CODENAME)))
PLUGIN_BREAKS := flashplugin-installer (<= 11.2.202.233ubuntu0.11.10.3), adobe-flashplugin (<= 11.1.102.63-0oneiric1)
PLUGIN_CONFLICTS := gcu-plugin (<= 0.12.8-1ubuntu3)
else
ifneq (,$(filter precise, $(DISTRIB_CODENAME)))
PLUGIN_BREAKS := flashplugin-installer (<= 11.1.102.63ubuntu1), adobe-flashplugin (<= 11.1.102.63-0precise1)
PLUGIN_CONFLICTS := gcu-plugin (<= 0.12.10-1ubuntu1)
endif
endif
endif
endif
endif
endif

ifeq (abrowser, $(MOZ_PKG_NAME))
ifeq (,$(filter lucid maverick natty oneiric precise, $(DISTRIB_CODENAME)))
PKG_APP_REPLACES_ARGS	:= kubuntu-abrowser-installer
else
ifeq (,$(filter lucid, $(DISTRIB_CODENAME)))
PKG_APP_REPLACES_ARGS	:= abrowser, abrowser-branding, abrowser-branding, kubuntu-abrowser-installer
PKG_APP_BREAKS_ARGS	:= abrowser (<= 4.0~b11+build3+nobinonly-0ubuntu1), abrowser-branding (<= 4.0~b11+build3+nobinonly-0ubuntu1), \
			   abrowser-branding (<= 4.0~b11+build3+nobinonly-0ubuntu1), $(PLUGIN_BREAKS)
PKG_APP_CONFLICTS_ARGS	:= $(PLUGIN_CONFLICTS)
else
PKG_APP_PROVIDES_ARGS	:= abrowser-3.6, abrowser-3.5, abrowser-3.0, abrowser-2, abrowser-2-dom-inspector, abrowser-2-libthai
PKG_APP_CONFLICTS_ARGS	:= abrowser-3.6 (<< 3.6~hg20100117r33523), abrowser-3.5 (<< 3.6~hg20100117r33523), \
			   abrowser-3.0 (<< 3.6~hg20100117r33523), abrowser-3.6-gnome-support (<< 3.6~hg20100117r33523), \
			   abrowser-2 (<< 3), abrowser-2-libthai (<< 3), abrowser-2-dom-inspector (<< 3), \
			   abrowser (<= 4.0~b11+build3+nobinonly-0ubuntu1), abrowser-branding (<= 4.0~b11+build3+nobinonly-0ubuntu1), \
			   abrowser-branding (<= 4.0~b11+build3+nobinonly-0ubuntu1), $(PLUGIN_CONFLICTS)
PKG_APP_REPLACES_ARGS	:= abrowser-3.6, abrowser-3.5, abrowser-3.0, abrowser-3.6-gnome-support, abrowser-2, abrowser-2-libthai, \
			   abrowser-2-dom-inspector, abrowser, abrowser-branding, abrowser-branding, kubuntu-abrowser-installer
PKG_APP_BREAKS_ARGS	:= $(PLUGIN_BREAKS)
PKG_DEV_PROVIDES_ARGS	:= abrowser-3.6-dev, abrowser-3.5-dev, abrowser-3.0-dev, abrowser-2-dev
PKG_DEV_REPLACES_ARGS	:= abrowser-3.6-dev, abrowser-3.5-dev, abrowser-3.0-dev, abrowser-2-dev
PKG_DEV_CONFLICTS_ARGS	:= abrowser-3.6-dev (<< 3.6~hg20100117r33523+nobinonly), abrowser-3.5-dev (<< 3.6~hg20100117r33523), \
			   abrowser-3.0-dev (<< 3.6~hg20100117r33523), abrowser-2-dev (<< 3)
PKG_GS_PROVIDES_ARGS	:= abrowser-3.6-gnome-support, abrowser-3.5-gnome-support, abrowser-3.0-gnome-support, abrowser-2
PKG_GS_CONFLICTS_ARGS	:= abrowser-3.6-gnome-support (<< 3.6~hg20100117r33523+nobinonly), \
			   abrowser-3.5-gnome-support (<< 3.6~hg20100117r33523), abrowser-3.0-gnome-support (<< 3.6~hg20100117r33523), \
			   abrowser-2 (<< 3)
PKG_GS_REPLACES_ARGS	:= abrowser-3.6-gnome-support, abrowser-3.5-gnome-support, abrowser-3.0-gnome-support, abrowser-2-gnome-support
PKG_DBG_CONFLICTS_ARGS	:= abrowser-3.6-dbg (<< 3.6~hg20100117r33523+nobinonly), abrowser-3.5-dbg (<< 3.6~hg20100117r33523), abrowser-2-dbg (<< 3)
PKG_DBG_REPLACES_ARGS	:= abrowser-3.6-dbg, abrowser-3.5-dbg, abrowser-2-dbg
PKG_DBG_PROVIDES_ARGS	:= abrowser-3.6-dbg, abrowser-3.5-dbg, abrowser-2-dbg
endif
endif
endif

ifneq (,$(filter lucid, $(DISTRIB_CODENAME)))
PKG_SUPPORT_SUGGESTS	:= $(MOZ_PKG_NAME)-gnome-support, kmozillahelper (>= 0.6)
else
ifneq (,$(filter lucid maverick natty oneiric, $(DISTRIB_CODENAME)))
PKG_SUPPORT_SUGGESTS	:= $(MOZ_PKG_NAME)-gnome-support | abrowser-kde-support
else
PKG_SUPPORT_SUGGESTS	:= $(MOZ_PKG_NAME)-gnome-support
endif
endif

MOZ_PKGNAME_SUBST_FILES = \
	debian/usr.bin.$(MOZ_PKG_NAME) \
	debian/README.Debian \
	debian/$(MOZ_PKG_NAME)-locale.preinst \
	debian/$(MOZ_PKG_BASENAME).sh \
	debian/apport/blacklist \
	debian/apport/native-origins \
	debian/apport/source_$(MOZ_PKG_NAME).py \
	$(NULL)

MOZ_APPNAME_SUBST_FILES = \
	debian/$(MOZ_APP_NAME).1 \
	debian/$(MOZ_APP_NAME)-restart-required.update-notifier \
	$(NULL)

MOZ_PKGCONFIG_FILES = \
	debian/pkgconfig/mozilla-plugin.pc \
	debian/pkgconfig/libxul.pc \
	debian/pkgconfig/mozilla-nspr.pc \
	$(NULL)

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

debian/usr.bin.abrowser.in:
	if [ '$(DISTRIB_VERSION_MAJOR)$(DISTRIB_VERSION_MINOR)' -ge '1204' ]; then \
		cp $(CURDIR)/debian/usr.bin.abrowser.apparmor.12.04 $(CURDIR)/debian/usr.bin.abrowser.in ; \
	elif [ '$(DISTRIB_VERSION_MAJOR)$(DISTRIB_VERSION_MINOR)' -ge '1104' ]; then \
		cp $(CURDIR)/debian/usr.bin.abrowser.apparmor.11.04 $(CURDIR)/debian/usr.bin.abrowser.in ; \
	elif [ '$(DISTRIB_VERSION_MAJOR)$(DISTRIB_VERSION_MINOR)' -ge '1010' ]; then \
		cp $(CURDIR)/debian/usr.bin.abrowser.apparmor.10.10 $(CURDIR)/debian/usr.bin.abrowser.in ; \
	elif [ "$(DISTRIB_VERSION_MAJOR)" -ge "10" ]; then \
		cp $(CURDIR)/debian/usr.bin.abrowser.apparmor.10.04 $(CURDIR)/debian/usr.bin.abrowser.in ; \
	else \
		cp $(CURDIR)/debian/usr.bin.abrowser.apparmor.9.10 $(CURDIR)/debian/usr.bin.abrowser.in ; \
	fi

common-binary-fixup-arch::
	@while read line ; \
	do \
		line=`echo $$line | sed 's/#.*//' | sed '/^$$/d'` ; \
		if [ ! -z "$$line" ] ; \
		then \
		pkgname=`echo $$line | sed 's/\([^:]*\):*\([^:]*\)/\2/'` ; \
			cp $(CURDIR)/debian/$(MOZ_PKG_NAME)-locale.preinst $(CURDIR)/debian/$(MOZ_PKG_NAME)-locale-$${pkgname}.preinst ; \
		fi \
	done < $(CURDIR)/debian/config/locales.shipped

LOCALE_PACKAGES := $(shell cat $(CURDIR)/debian/control | grep "^Package:[[:space:]]*$(MOZ_PKG_NAME)-locale\-" | sed -n -e 's/^Package\:[[:space:]]*\([^[:space:]]*\)/\1/ p')

#common-binary-post-install-arch:: install-webapprt-langpack-links

install-webapprt-langpack-links: $(foreach target, $(LOCALE_PACKAGES), install-webapprt-langpack-link-$(target))
install-webapprt-langpack-link-%: XPIS = $(notdir $(wildcard debian/$*/$(MOZ_ADDONDIR)/extensions/*.xpi))
install-webapprt-langpack-link-%:
	$(foreach xpi,$(XPIS),dh_link -p$* $(MOZ_ADDONDIR)/extensions/$(xpi) $(MOZ_LIBDIR)/webapprt/extensions/$(xpi);)

pre-build::
	@cp $(DEB_SRCDIR)/toolkit/content/widgets/dialog.xml $(DEB_SRCDIR)/toolkit/content/widgets/dialog-kde.xml
	@cp $(DEB_SRCDIR)/toolkit/content/widgets/preferences.xml $(DEB_SRCDIR)/toolkit/content/widgets/preferences-kde.xml
	@cp $(DEB_SRCDIR)/browser/base/content/browser.xul $(DEB_SRCDIR)/browser/base/content/browser-kde.xul

ifneq ($(MOZ_APP_BASENAME),$(MOZ_DEFAULT_APP_BASENAME))
	@echo "Setting MOZ_APP_UA_NAME to $(MOZ_DEFAULT_APP_BASENAME)"
	@cp $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/configure.sh $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/configure.sh.bak; \
		echo "MOZ_APP_UA_NAME=$(MOZ_DEFAULT_APP_BASENAME)" >> $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/configure.sh

	@echo "Setting UAName to $(MOZ_DEFAULT_APP_BASENAME)"
	@cp $(DEB_SRCDIR)/webapprt/application.ini.in $(DEB_SRCDIR)/webapprt/application.ini.in.bak; \
		sed -ri 's/@MOZ_APP_BASENAME@/$(MOZ_DEFAULT_APP_BASENAME)/g' $(DEB_SRCDIR)/webapprt/application.ini.in
endif
ifneq ($(MOZ_APP_NAME),$(MOZ_DEFAULT_APP_NAME))
	@echo "Replacing instances of \"%APP%\" with \"abrowser\" in abrowser-branding.js"
	@cp $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/pref/abrowser-branding.js $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/pref/abrowser-branding.js.bak; \
		sed -ri 's/%APP%/abrowser/g' $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/pref/abrowser-branding.js
endif

clean::
	rm -f debian/$(MOZ_PKG_NAME)-locale-*.preinst
	rm -f debian/usr.bin.abrowser.in
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/configure.sh)
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/$(MOZ_BRANDING_DIR)/pref/abrowser-branding.js)
	$(call RESTORE_BACKUP, $(DEB_SRCDIR)/webapprt/application.ini.in)
	rm -f $(DEB_SRCDIR)/toolkit/content/widgets/dialog-kde.xml
	rm -f $(DEB_SRCDIR)/toolkit/content/widgets/preferences-kde.xml
	rm -f $(DEB_SRCDIR)/browser/base/content/browser-kde.xul
