#!/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_HOST_GNU_CPU	?= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)

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

DEBIAN_APP_NAME		:= $(shell echo $(DEBIAN_NAME) | sed -e 's/-.*//')
DEBIAN_APP_NAME_VER	:= $(shell echo $(DEBIAN_NAME) | sed -e 's/^[^-]*-\{0,1\}\([^-]*\)$$/\1/')
DEBIAN_APP_NAME_OTHER	:= abrowser

DEBIAN_NAME_OTHER	:= $(shell echo $(DEBIAN_NAME) | sed -e 's/$(DEBIAN_APP_NAME)/$(DEBIAN_APP_NAME_OTHER)/')

BRANDING_NAME_OTHER	:= awesome-browser
DEBIAN_META_NAME	:= $(shell grep '^Package: $(DEBIAN_APP_NAME)$$' $(CURDIR)/debian/control | sed -e 's/^Package: //')

DEBIAN_VERSION		:= $(shell dpkg-parsechangelog | sed -n 's/^Version: *\(.*\)$$/\1/ p')
DEBIAN_XUL_VER		:= $(shell MALLOC_OPTIONS=O xulrunner-1.9.2 --gre-version)
DEBIAN_XUL_DEV		:= /usr/lib/xulrunner-devel-$(DEBIAN_XUL_VER)
DEBIAN_XUL_DIR		:= usr/lib/xulrunner-$(DEBIAN_XUL_VER)
DEBIAN_FF3_VER		 = $(shell cat build-tree/mozilla/browser/config/version.txt)
DEBIAN_FF3_DIR		 = usr/lib/$(DEBIAN_APP_NAME)-$(DEBIAN_FF3_VER)
DEB_TAR_SRCDIR		:= mozilla

WANT_UNIT_TESTS		?= 1
DEB_MIN_SYSDEPS		?= 1

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

ifneq (,$(DEBIAN_META_NAME))
  DEBIAN_REAL_NAME = $(DEBIAN_META_NAME)
  DEBIAN_REAL_NAME_OTHER = $(DEBIAN_APP_NAME_OTHER)
else
  DEBIAN_REAL_NAME = $(DEBIAN_NAME)
  DEBIAN_REAL_NAME_OTHER = $(DEBIAN_NAME_OTHER)
endif

DISTRIB := $(shell lsb_release -i -s)
ifneq (,$(filter $(DISTRIB),Ubuntu))
  DISTRIB_VERSION_MAJOR := $(shell lsb_release -s -r | cut -d '.' -f 1)
  DISTRIB_VERSION_MINOR := $(shell lsb_release -s -r | cut -d '.' -f 2)
endif

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

EXTRA_SYSTEM_CONFIGURE_FLAGS = $(NULL)

DESKTOP  = $(DEBIAN_REAL_NAME_OTHER).desktop
ifneq (,$(findstring ~hg,$(DEBIAN_VERSION)))
	# codename branding for dailies
	BRANDING = --with-branding=browser/branding/unofficial
	DESKTOP_BRANDED  = $(DEBIAN_REAL_NAME)-namoroka.desktop
else
	# official branding for releases
	BRANDING = --enable-official-branding
	DESKTOP_BRANDED  = $(DEBIAN_REAL_NAME)-final.desktop
endif

# translation flags
MOZ_TRANSLATION_AUTO_XPI_OUTDIR := debian/lp-export-xpis/
MOZ_TRANSLATION_AUTO_XPI_SEARCHDIRS := debian/$(DEBIAN_REAL_NAME)
MOZ_TRANSLATION_AUTO_XPI_LOCALE := en-US

DEB_AUTO_UPDATE_AUTOCONF=2.13

include /usr/share/cdbs/1/rules/tarball.mk
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/$(DEBIAN_NAME).mk
include /usr/share/mozilla-devscripts/lp-locale-export.mk

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

CFLAGS = -g
CXXFLAGS = -g

LDFLAGS=$(shell echo $$LDFLAGS | sed -e 's/-Wl,-Bsymbolic-functions//')
export LDFLAGS

export DEB_BUILD_HARDENING=1

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
ifneq (armel, $(DEB_BUILD_ARCH))
	EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-optimize
else
	EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-optimize=-O2
endif
else
	EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-optimize
endif

# enable IPC and the crash reporter only on i386, amd64 and armel
ENABLE_IPC ?= 1
ifeq (,$(findstring i386,$(DEB_BUILD_ARCH)))
ifeq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
ifeq (,$(findstring armel,$(DEB_BUILD_ARCH)))
  ENABLE_IPC=0
endif
endif
endif

ifeq (1,$(ENABLE_IPC))
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-ipc
else
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-ipc
endif

# disable jit on sparc until bmo 502369 is fixable
ifneq (,$(findstring sparc,$(DEB_BUILD_ARCH)))
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-jit
endif

$(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

ifneq (1, $(DEB_MIN_SYSDEPS))
  USE_SYSTEM_CAIRO := $(shell pkg-config --exists 'cairo >= 1.8.8'; a=$$?; if test $$a != 1; then echo 1; fi)
endif
# 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

ifneq (1, $(DEB_MIN_SYSDEPS))
  USE_SYSTEM_SQLITE :=  $(shell pkg-config --exists 'sqlite3 >= 3.7.1'; a=$$?; if test $$a != 1; then echo 1; fi)
endif
ifeq (1,$(USE_SYSTEM_SQLITE))
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-system-sqlite
else
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-system-sqlite
endif

ifneq (1, $(DEB_MIN_SYSDEPS))
  USE_SYSTEM_NSPR := $(shell pkg-config --exists 'nspr >= 4.8.6'; a=$$?; if test $$a != 1; then echo 1; fi)
endif
ifeq (1,$(USE_SYSTEM_NSPR))
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --with-system-nspr
else
  EXTRA_SYSTEM_CONFIGURE_FLAGS += --without-system-nspr
endif

ifneq (1, $(DEB_MIN_SYSDEPS))
USE_SYSTEM_NSS := $(shell pkg-config --exists 'nss >= 3.12.8'; a=$$?; if test $$a != 1; then echo 1; fi)
endif
ifeq (1,$(USE_SYSTEM_NSS))
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --with-system-nss
else
   EXTRA_SYSTEM_CONFIGURE_FLAGS += --without-system-nss
endif

ifneq (1, $(DEB_MIN_SYSDEPS))
	system_jpeg_configure_flags    := --with-system-jpeg=/usr
	system_zlib_configure_flags    := --with-system-zlib=/usr
	system_myspell_configure_flags := --disable-system-myspell
	system_xul_configure_flags     := --with-libxul-sdk=$(DEBIAN_XUL_DEV) --with-system-libxul
else
	system_jpeg_configure_flags    := --without-system-jpeg
	system_zlib_configure_flags    := --without-system-zlib
	system_myspell_configure_flags := --enable-system-myspell
endif

# required for various features
BUILD_OFFICIAL = 1
export BUILD_OFFICIAL

DEB_CONFIGURE_USER_FLAGS= \
	$(EXTRA_SYSTEM_CONFIGURE_FLAGS) \
	--disable-debug \
	--with-user-appdir=.mozilla \
	$(system_jpeg_configure_flags) \
	$(system_zlib_configure_flags) \
	$(system_xul_configure_flags) \
	$(system_myspell_configure_flags) \
	--disable-crashreporter \
	--disable-composer \
	--disable-elf-dynstr-gc \
	--disable-gtktest \
	--disable-install-strip \
	--disable-installer \
	--disable-ldap \
	--disable-mailnews \
	--disable-profilesharing \
	--disable-strip \
	--disable-strip-libs \
	--disable-tests \
	--disable-mochitest \
	--disable-updater \
	--disable-xprint \
	--enable-application=browser \
	--enable-canvas \
	--enable-default-toolkit=cairo-gtk2 \
	--enable-gnomevfs \
	--enable-pango \
	--enable-postscript \
	--enable-svg \
	--enable-mathml \
	--enable-xft \
	--enable-xinerama \
	--enable-extensions=default,-reporter \
	--enable-safe-browsing \
	--enable-single-profile \
	--with-distribution-id=com.ubuntu \
	$(BRANDING)

UUDECODE = \
	debsearch.gif \
	$(NULL)

DEBIAN_EXECUTABLES = \
	firefox.sh \
	$(NULL)

subst_files = \
	debian/$(DEBIAN_REAL_NAME)-gnome-support.postinst \
	debian/$(DEBIAN_REAL_NAME).postinst \
	debian/$(DEBIAN_REAL_NAME).postrm \
	debian/$(DEBIAN_REAL_NAME).prerm \
	debian/$(DEBIAN_REAL_NAME).preinst \
	debian/firefox.sh \
	$(NULL)

%: %.in
	sed -e 's,@LIBDIR@,/$(DEBIAN_FF3_DIR),g' \
	    -e 's,@APPNAME@,$(DEBIAN_REAL_NAME),g' < $< > $@

debian/firefox.sh: debian/firefox.sh.in
	sed -e 's,@LIBDIR@,/$(DEBIAN_FF3_DIR),g' \
	    -e 's,@META_NAME@,$(DEBIAN_META_NAME),g' \
	    -e 's,@APPVER@,$(DEBIAN_APP_NAME_VER),g' < $< > $@

debian/migrator/ffox-beta-profile-migration-dialog: debian/migrator/main.c
	$(CC) $(CFLAGS) -o $@ $< $(shell pkg-config --cflags --libs gtk+-2.0)

BRANDING_MAKEFILES = ../../browser/branding/$(BRANDING_NAME_OTHER)/Makefile \
	../../browser/branding/$(BRANDING_NAME_OTHER)/locales/Makefile \
	../../browser/branding/$(BRANDING_NAME_OTHER)/content/Makefile \
	$(NULL)

ifeq (1, $(DEB_MIN_SYSDEPS))
post-patches:: debian/stamp-autotools-files-moz
debian/stamp-autotools-files-moz:
	# Number of processors: $(PROCESSORS) [$(shell grep 'model name' /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 $@
endif


common-build-arch::
	cd build-tree/mozilla/browser/locales/; \
		$(MAKE) SUBMAKEFILES="$(BRANDING_MAKEFILES)" $(BRANDING_MAKEFILES)
	cd build-tree/mozilla/browser/locales/; \
		$(MAKE) -C ../../browser/branding/$(BRANDING_NAME_OTHER)/content/; \
		$(MAKE) -C ../../browser/branding/$(BRANDING_NAME_OTHER)/locales/

common-post-build-arch::
	cd build-tree/mozilla/; \
		mkdir -p $(CURDIR)/debian/tmp-$(DEBIAN_REAL_NAME)-branding/; \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp-$(DEBIAN_REAL_NAME)-branding/
	cd build-tree/mozilla/browser/locales/; \
		$(MAKE) -C ../../browser/branding/$(BRANDING_NAME_OTHER)/; \
		$(MAKE) -C ../../browser/app/
	cd build-tree/mozilla/; \
		mkdir -p $(CURDIR)/debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/; \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/
	
binary-install/$(DEBIAN_REAL_NAME)::
	dh_installdirs $(DEBIAN_FF3_DIR)/distribution/
	dh_installdirs -p$(DEBIAN_REAL_NAME) /usr/lib/firefox-addons/searchplugins/en-US
	rm -rf debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/icons
	rm -rf debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/chrome/icons
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/classic.* \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/en-US.* \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/browser.* \
			$(DEBIAN_FF3_DIR)/chrome
ifeq (1, $(DEB_MIN_SYSDEPS))
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/pippki.* \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/toolkit.* \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/comm.* \
			$(DEBIAN_FF3_DIR)/chrome
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/*.so \
			$(DEBIAN_FF3_DIR)

	$(foreach i,$(wildcard debian/tmp/usr/lib/firefox-*/components/*.so), \
		dh_install -p$(DEBIAN_REAL_NAME) $(i);) true
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/firefox-*bin
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/defaults/autoconfig
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/defaults/pref
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/greprefs
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/res

else
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/components/libbrowsercomps.so
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/components/libbrowserdirprovider.so
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/defaults/preferences
endif
ifeq (1,$(ENABLE_IPC))
	dh_install -p$(DEBIAN_REAL_NAME) \
		debian/tmp/usr/lib/firefox-*/plugin-container
endif
	touch debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/.autoreg
	if [ -d debian/$(DEBIAN_REAL_NAME)/usr/lib/xulrunner-addons/extensions ] ; then \
	  touch $(foreach dir,$(wildcard debian/$(DEBIAN_REAL_NAME)/usr/lib/xulrunner-addons/extensions/*),$(dir)/chrome.manifest) ; \
	fi
	dh_install debian/firefox.sh $(DEBIAN_FF3_DIR)
	dh_installdocs -p$(DEBIAN_REAL_NAME) debian/firefox.cfg
	dh_install debian/$(DEBIAN_REAL_NAME)-restart-required.update-notifier $(DEBIAN_FF3_DIR)
	dh_install debian/migrator/ffox-beta-profile-migration-dialog $(DEBIAN_FF3_DIR)
	dh_link $(DEBIAN_FF3_DIR)/firefox.sh usr/bin/$(DEBIAN_REAL_NAME)
ifneq (,$(DEBIAN_META_NAME))
	dh_link $(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME) $(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME_OTHER)
	dh_link $(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME) $(DEBIAN_FF3_DIR)/$(DEBIAN_NAME_OTHER)
	dh_link $(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME) $(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME)-$(DEBIAN_APP_NAME_VER)
	dh_install debian/tmp/$(DEBIAN_FF3_DIR)/$(DEBIAN_META_NAME) $(DEBIAN_FF3_DIR)/
else
	dh_link $(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME)-$(DEBIAN_APP_NAME_VER) $(DEBIAN_FF3_DIR)/$(DEBIAN_NAME_OTHER)
	install -m 755 debian/tmp/$(DEBIAN_FF3_DIR)/$(DEBIAN_REAL_NAME) debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/$(DEBIAN_APP_NAME)-$(DEBIAN_APP_NAME_VER)
endif
	dh_link usr/lib/firefox-addons/extensions $(DEBIAN_FF3_DIR)/extensions
	dh_link usr/lib/firefox-addons/plugins $(DEBIAN_FF3_DIR)/plugins
	dh_link usr/lib/firefox-addons/searchplugins $(DEBIAN_FF3_DIR)/searchplugins
	dh_link etc/$(DEBIAN_REAL_NAME)/pref $(DEBIAN_FF3_DIR)/defaults/syspref
	dh_link etc/$(DEBIAN_REAL_NAME)/profile $(DEBIAN_FF3_DIR)/defaults/profile
	dh_link usr/share/myspell/dicts $(DEBIAN_FF3_DIR)/dictionaries
	rm -f debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/chrome/browser-branding*
	dh_install debian/distribution.ini $(DEBIAN_FF3_DIR)/distribution/
	dh_link usr/lib/firefox-addons/searchplugins $(DEBIAN_FF3_DIR)/distribution/searchplugins
	install -m 644 debian/tmp/$(DEBIAN_FF3_DIR)/searchplugins/* debian/$(DEBIAN_REAL_NAME)/usr/lib/firefox-addons/searchplugins/en-US/

binary-install/$(DEBIAN_REAL_NAME_OTHER)-branding::
	dh_install -p$(DEBIAN_REAL_NAME_OTHER)-branding \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/icons $(DEBIAN_FF3_DIR)
	dh_install -p$(DEBIAN_REAL_NAME_OTHER)-branding \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/icons $(DEBIAN_FF3_DIR)/chrome
	dh_install -p$(DEBIAN_REAL_NAME_OTHER)-branding \
		debian/tmp-$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/awesome-* $(DEBIAN_FF3_DIR)/chrome/
	dh_link -p$(DEBIAN_REAL_NAME_OTHER)-branding $(DEBIAN_FF3_DIR)/icons/mozicon128.png usr/share/pixmaps/$(DEBIAN_REAL_NAME_OTHER).png
	mkdir -p debian/$(DEBIAN_REAL_NAME_OTHER)-branding/usr/share/applications/
ifneq (,$(DEBIAN_META_NAME))
	cp -f debian/$(DESKTOP) \
		debian/$(DEBIAN_REAL_NAME_OTHER)-branding/usr/share/applications/$(DEBIAN_APP_NAME_OTHER).desktop
else
	cp -f debian/$(DESKTOP) \
		debian/$(DEBIAN_REAL_NAME_OTHER)-branding/usr/share/applications/$(DEBIAN_APP_NAME_OTHER)-$(DEBIAN_APP_NAME_VER).desktop
endif
ifeq (1, $(DEB_MIN_SYSDEPS))
	dh_installdirs -p$(DEBIAN_REAL_NAME_OTHER)-branding $(DEBIAN_FF3_DIR)/defaults/pref/
else
	dh_installdirs -p$(DEBIAN_REAL_NAME_OTHER)-branding $(DEBIAN_FF3_DIR)/defaults/preferences/
endif

binary-install/$(DEBIAN_REAL_NAME)-branding::
	rm -rf debian/$(DEBIAN_REAL_NAME)-branding/$(DEBIAN_FF3_DIR)/icons
	rm -rf debian/$(DEBIAN_REAL_NAME)-branding/$(DEBIAN_FF3_DIR)/chrome/icons
	dh_install -p$(DEBIAN_REAL_NAME)-branding \
		debian/tmp-$(DEBIAN_REAL_NAME)-branding/$(DEBIAN_FF3_DIR)/chrome/browser-branding* $(DEBIAN_FF3_DIR)/chrome
	dh_install -p$(DEBIAN_REAL_NAME)-branding \
		debian/tmp-$(DEBIAN_REAL_NAME)-branding/$(DEBIAN_FF3_DIR)/icons $(DEBIAN_FF3_DIR)
	dh_install -p$(DEBIAN_REAL_NAME)-branding \
		debian/tmp-$(DEBIAN_REAL_NAME)-branding/$(DEBIAN_FF3_DIR)/chrome/icons $(DEBIAN_FF3_DIR)/chrome
	dh_link -p$(DEBIAN_REAL_NAME)-branding $(DEBIAN_FF3_DIR)/icons/mozicon128.png usr/share/pixmaps/$(DEBIAN_REAL_NAME).png
	dh_link -p$(DEBIAN_REAL_NAME)-branding $(DEBIAN_FF3_DIR)/icons/mozicon128.png usr/share/pixmaps/$(DEBIAN_REAL_NAME)-3.0.png
	mkdir -p debian/$(DEBIAN_REAL_NAME)-branding/usr/share/applications/
ifneq (,$(DEBIAN_META_NAME))
	cp -f debian/$(DESKTOP_BRANDED) \
		debian/$(DEBIAN_REAL_NAME)-branding/usr/share/applications/$(DEBIAN_APP_NAME).desktop
else
	cp -f debian/$(DESKTOP_BRANDED) \
		debian/$(DEBIAN_REAL_NAME)-branding/usr/share/applications/$(DEBIAN_APP_NAME)-$(DEBIAN_APP_NAME_VER).desktop
endif

ifeq (1, $(DEB_MIN_SYSDEPS))
pref_target = debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/defaults/pref/ubuntu-useragent.js
else
pref_target = debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/defaults/preferences/ubuntu-useragent.js
endif

binary-install/$(DEBIAN_REAL_NAME):: $(pref_target)

$(pref_target): debian/ubuntu-useragent.js.tmpl
	sed \
		-e 's/@VENDOR@/'`lsb_release -i -s`'/g' \
		-e 's/@VENDOR_SUB@/'`lsb_release -r -s`'/g' \
		-e 's/@VENDOR_COMMENT@/'`lsb_release -c -s`'/g' \
		< $< > $@

ifeq (1, $(DEB_MIN_SYSDEPS))
abrowser_pref_target = debian/$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/defaults/pref/ubuntu-abrowser.js
else
abrowser_pref_target = debian/$(DEBIAN_REAL_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/defaults/preferences/ubuntu-abrowser.js
endif

binary-install/$(DEBIAN_REAL_NAME_OTHER)-branding:: $(abrowser_pref_target)

$(abrowser_pref_target): debian/ubuntu-abrowser.js.tmpl
	sed \
		-e 's/@APP_VERSION@/$(DEBIAN_FF3_VER)/' \
		< $< > $@

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

ifeq (1, $(DEB_MIN_SYSDEPS))
GNOME_SUPPORT_FILES = libmozgnome.so libnkgnomevfs.so
endif

binary-post-install/$(DEBIAN_REAL_NAME):: auto-locale-xpi.mk/translate compare
	sed \
		-e 's/@EM_VERSION@/$(EM_TRANSLATION_VERSION)/' \
		-e 's/@EM_FIREFOX_MIN_VERSION@/$(EM_TRANSLATION_MIN_VERSION)/' \
		-e 's/@EM_FIREFOX_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
	$(foreach file,$(GNOME_SUPPORT_FILES),rm -fv debian/$(DEBIAN_REAL_NAME)/usr/lib/firefox-*/components/$(file);) true

ifeq (1, $(DEB_MIN_SYSDEPS))
# we want the gnome dependencies not to be in the main package at shlibdeps runtime, hence we dont
# install them at binary-install/* stage, but copy them over _after_ the shlibdeps had been generated
binary-predeb/$(DEBIAN_REAL_NAME)::
	$(foreach file,$(GNOME_SUPPORT_FILES),cp -vf debian/tmp/usr/lib/firefox-*/components/$(file) debian/$(DEBIAN_REAL_NAME)/usr/lib/firefox-*/components/;) true

	$(foreach lib,libsoftokn3.so libfreebl3.so libnssdbm3.so, \
		LD_LIBRARY_PATH=debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR):$$LD_LIBRARY_PATH \
		build-tree/mozilla/dist/bin/shlibsign -v -i debian/$(DEBIAN_REAL_NAME)/$(DEBIAN_FF3_DIR)/$(lib);)

# ... and remove them from the -gnome-support package accordingly
binary-predeb/$(DEBIAN_REAL_NAME)-gnome-support::
	$(foreach file,$(GNOME_SUPPORT_FILES),rm -vf debian/$(DEBIAN_REAL_NAME)-gnome-support/usr/lib/firefox-*/components/$(file);) true
endif

pre-build:: $(subst_files) debian/migrator/ffox-beta-profile-migration-dialog
	set -e; for i in $(UUDECODE); do \
		uudecode -o debian/$$i debian/$$i.uu; \
		done ; \
		for i in $(DEBIAN_EXECUTABLES); do \
			chmod a+x debian/$$i; \
		done

	# KDE integration
	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

clean::
	set -e; for i in $(UUDECODE); do \
		rm -f debian/$$i; \
		done ; 
	rm -f debian/$(DEBIAN_REAL_NAME).desktop
	rm -f $(subst_files) debian/install.rdf
	rm -f debian/migrator/ffox-beta-profile-migration-dialog
	rm -f debian/migrator/*~
	rm -f debian/stamp-autotools-files-moz
	rm -rf debian/tmp-*
