#!/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)

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

DEBIAN_APP_NAME		:= $(shell echo $(DEBIAN_NAME) | sed -e 's/-.*//')
DEBIAN_APP_NAME_VER	:= $(shell echo $(DEBIAN_NAME) | sed -e 's/.*-//g')
DEBIAN_APP_NAME_OTHER	:= abrowser

DEBIAN_NAME_OTHER	:= $(DEBIAN_APP_NAME_OTHER)-$(DEBIAN_APP_NAME_VER)
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: *//p')
PACKAGE_VERSION		:= $(shell dpkg-parsechangelog | sed -n 's/^Version: .*-\(.*\)$$/\1/p')
DEBIAN_XUL_VER		:= $(shell MALLOC_OPTIONS=O xulrunner-1.9.1 --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 $(DEB_BUILDDIR)/browser/config/version.txt)
DEBIAN_FF3_DIR		 = usr/lib/$(DEBIAN_APP_NAME)-$(DEBIAN_FF3_VER)
DEB_TAR_SRCDIR		:= mozilla
DEB_BUILDDIR		= build-tree/mozilla

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

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

EXTRA_SYSTEM_CONFIGURE_FLAGS = $(NULL)

FORCE_OFFICIAL_BRANDING = 1

DESKTOP  = $(DEBIAN_NAME_OTHER).desktop
ifneq (,$(FORCE_OFFICIAL_BRANDING))
	# official branding for final and final RCs builds
	BRANDING = --enable-official-branding
	DESKTOP_BRANDED  = $(DEBIAN_NAME)-final.desktop
else
# Change branding based on Version
ifneq (,$(findstring ~b,$(DEBIAN_VERSION)))
	# codename branding for beta
	BRANDING = --with-branding=browser/branding/unofficial
	DESKTOP_BRANDED  = $(DEBIAN_NAME)-shiretoko.desktop
else
ifneq (,$(findstring ~hg,$(DEBIAN_VERSION)))
	# minefield branding for snapshots
	BRANDING = $(NULL)
	DESKTOP_BRANDED  = $(DEBIAN_NAME)-minefield.desktop
else
	# codename branding for milestones
	BRANDING = --with-branding=browser/branding/unofficial
	DESKTOP_BRANDED  = $(DEBIAN_NAME)-shiretoko.desktop
endif
endif
endif

# translation flags
MOZ_TRANSLATION_AUTO_XPI_OUTDIR := debian/lp-export-xpis/
MOZ_TRANSLATION_AUTO_XPI_SEARCHDIRS := debian/$(DEBIAN_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 $(DEB_BUILDDIR)/README to be shipped as a doc
DEB_INSTALL_DOCS_ALL := $(NULL)

CFLAGS = -g -mtune=pentium -fomit-frame-pointer
CXXFLAGS = $(CFLAGS)

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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-optimize
endif

USE_SYSTEM_CAIRO := $(shell pkg-config --exists 'cairo >= 1.5.8'; a=$$?; if test $$a != 1; then echo 1; fi)
# 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.0'; a=$$?; if test $$a != 1; then echo 1; fi)
ifeq (1,$(USE_SYSTEM_SQLITE))
	EXTRA_SYSTEM_CONFIGURE_FLAGS += --enable-system-sqlite
else
	EXTRA_SYSTEM_CONFIGURE_FLAGS += --disable-system-sqlite
endif

# required for various features such as safe browsing
BUILD_OFFICIAL = 1
export BUILD_OFFICIAL

DEB_CONFIGURE_USER_FLAGS= \
	$(EXTRA_SYSTEM_CONFIGURE_FLAGS) \
	--disable-debug \
	--with-user-appdir=.mozilla \
	--with-system-jpeg=/usr \
	--with-system-zlib=/usr \
	--with-libxul-sdk=$(DEBIAN_XUL_DEV) \
	--with-system-libxul \
	--with-system-nspr \
	--with-system-nss \
	--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-optimize \
	--enable-pango \
	--enable-postscript \
	--enable-svg \
	--enable-mathml \
	--enable-xft \
	--enable-xinerama \
	--enable-extensions=default,-reporter \
	--enable-safe-browsing \
	--enable-single-profile \
	--enable-system-myspell \
	--with-distribution-id=com.jolicloud \
	$(BRANDING)

UUDECODE = \
	debsearch.gif \
	$(NULL)

DEBIAN_EXECUTABLES = \
	firefox.sh \
	$(NULL)

subst_files = \
	debian/$(DEBIAN_NAME)-gnome-support.postinst \
	debian/$(DEBIAN_NAME).postinst \
	debian/distribution.ini \
	debian/firefox.sh \
	$(NULL)

%: %.in
	sed -e 's,@LIBDIR@,/$(DEBIAN_FF3_DIR),g' \
	    -e 's,@APPNAME@,$(DEBIAN_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/distribution.ini: debian/distribution.ini.in
	sed -e 's,@PKG_VERSION@,$(PACKAGE_VERSION),g' \
		-e 's,@DEB_DISTRIB@,$(DEBIAN_DISTRIBUTION),g' < $< > $@

debian/migrator/ffox-35-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)

common-post-build-arch::
	cd $(DEB_BUILDDIR); \
		mkdir -p $(CURDIR)/debian/tmp-$(DEBIAN_NAME)-branding/; \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp-$(DEBIAN_NAME)-branding/
	cd $(DEB_BUILDDIR); \
		mkdir -p $(CURDIR)/debian/tmp-$(DEBIAN_NAME_OTHER)-branding/; \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp-$(DEBIAN_NAME_OTHER)-branding/
	
binary-install/$(DEBIAN_NAME)::
	dh_installdirs $(DEBIAN_FF3_DIR)/distribution/
	dh_installdirs -p$(DEBIAN_NAME) /usr/lib/firefox-addons/searchplugins/en-US
	rm -rf debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/icons
	rm -rf debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/chrome/icons
	dh_install -p$(DEBIAN_NAME) \
		debian/tmp-$(DEBIAN_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/classic.* \
		debian/tmp-$(DEBIAN_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/en-US.* \
		debian/tmp-$(DEBIAN_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/chrome/browser.* \
			$(DEBIAN_FF3_DIR)/chrome
	touch debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/.autoreg
	if [ -d debian/$(DEBIAN_NAME)/usr/lib/xulrunner-addons/extensions ] ; then \
	  touch $(foreach dir,$(wildcard debian/$(DEBIAN_NAME)/usr/lib/xulrunner-addons/extensions/*),$(dir)/chrome.manifest) ; \
	fi
	dh_install debian/firefox.sh $(DEBIAN_FF3_DIR)
	dh_installdocs -p$(DEBIAN_NAME) debian/firefox.cfg
	dh_install debian/$(DEBIAN_NAME)-restart-required.update-notifier $(DEBIAN_FF3_DIR)
	dh_install debian/migrator/ffox-35-beta-profile-migration-dialog $(DEBIAN_FF3_DIR)
	dh_link $(DEBIAN_FF3_DIR)/firefox.sh usr/bin/$(DEBIAN_NAME)
ifneq (,$(DEBIAN_META_NAME))
	dh_link usr/bin/$(DEBIAN_NAME) usr/bin/$(DEBIAN_APP_NAME)
	dh_link usr/bin/$(DEBIAN_NAME) usr/bin/$(DEBIAN_APP_NAME_OTHER)
	#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 -v 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)
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 etc/$(DEBIAN_NAME)/pref $(DEBIAN_FF3_DIR)/defaults/syspref
	dh_link etc/$(DEBIAN_NAME)/profile $(DEBIAN_FF3_DIR)/defaults/profile
	dh_link usr/share/myspell/dicts $(DEBIAN_FF3_DIR)/dictionaries
	rm -f debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/chrome/browser-branding*
	dh_install debian/distribution.ini $(DEBIAN_FF3_DIR)/distribution/
	dh_link -p$(DEBIAN_NAME) usr/lib/firefox-addons/searchplugins $(DEBIAN_FF3_DIR)/distribution/searchplugins
	dh_link -p$(DEBIAN_NAME) usr/lib/firefox-addons/searchplugins usr/lib/firefox-addons/searchplugins/common
	install -m 644 debian/tmp/$(DEBIAN_FF3_DIR)/searchplugins/* debian/$(DEBIAN_NAME)/usr/lib/firefox-addons/searchplugins/en-US/

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

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

binary-install/$(DEBIAN_NAME):: debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/defaults/preferences/ubuntu-useragent.js

debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/defaults/preferences/ubuntu-useragent.js: 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' \
		< $< > $@

binary-install/$(DEBIAN_NAME_OTHER)-branding:: debian/$(DEBIAN_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/defaults/preferences/ubuntu-abrowser.js

debian/$(DEBIAN_NAME_OTHER)-branding/$(DEBIAN_FF3_DIR)/defaults/preferences/ubuntu-abrowser.js: 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 '~')

binary-post-install/$(DEBIAN_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
	sed -i \
		-e 's/^MaxVersion=.*/MaxVersion=1.9.1.*/' \
		-e 's/^MinVersion=.*/MinVersion=1.9.1/' \
		debian/$(DEBIAN_NAME)/$(DEBIAN_FF3_DIR)/application.ini

pre-build:: $(subst_files) debian/migrator/ffox-35-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

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