#!/usr/bin/make -f

DEBIAN_NAME		:= $(shell dpkg-parsechangelog | sed -n 's/^Source: *\(.*\)$$/\1/ p')
DEBIAN_VERSION		:= $(shell dpkg-parsechangelog | sed -n 's/^Version: *\(.*\)$$/\1/ p')
DEBIAN_UPSTREAM_VERSION	:= $(shell echo $(DEBIAN_VERSION) | sed 's/^\(.*\)-[^-]*$$/\1/')
DEBIAN_REVISION		:= $(shell echo $(DEBIAN_VERSION) | sed 's/^.*r\([^-]*\)-.*/\1/')
DEBIAN_DIST		:= $(shell lsb_release -ds | tr -d '()' | sed -e 's/\#/ /g')
DEBIAN_DIST_NAME	:= $(shell lsb_release -si | sed -e 's/\#/ /g')
DEBIAN_DIST_VERSION	:= $(shell lsb_release -sr | sed -e 's/\#/ /g')

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_BITS)

# needed for easy job control, e.g, kill %1
SHELL := /bin/bash

SRC_DIR        := $(CURDIR)
LIB_DIR        := usr/lib/$(DEBIAN_NAME)
FFMPEG_DIR     := third_party/ffmpeg
FFMPEG_SRC_DIR := $(SRC_DIR)/$(FFMPEG_DIR)
NINJA          := ninja
BUILD_TYPE     := final

# Whitelist LP provided new langs only in release builds, PPAs ship them all
WANT_ONLY_WHITELISTED_NEW_LANGS ?= 0

ifneq (,$(findstring Ubuntu,$(DEBIAN_DIST)))
DISTRIBUTION=UBUNTU
UBUNTU_DIST=$(DEBIAN_DIST)
endif

ifneq (,$(findstring Debian,$(DEBIAN_DIST)))
DISTRIBUTION=DEBIAN
endif

WANT_OZONE       := 0

common_defines :=
browser_defines :=
ffmpeg_std_defines :=
ffmpeg_extra_defines :=

# enable verbose build messages
export DH_VERBOSE=1

common_defines += \
				enable_google_now=false \
				enable_hangout_services_extension=true \
				enable_mdns=true \
				enable_nacl=false \
				enable_vr=false \
				enable_wayland_server=false \
				enable_webrtc=true \
				enable_widevine=true \
				fieldtrial_testing_like_official_build=true \
				is_component_ffmpeg=true \
				is_debug=false \
				is_desktop_linux=true \
				linux_use_bundled_binutils=false \
				remove_webcore_debug_symbols=true \
				symbol_level=0 \
				treat_warnings_as_errors=false \
				use_allocator="none" \
				use_alsa=true \
				use_aura=true \
				use_bundled_fontconfig=false \
				use_cups=true \
				use_custom_libcxx=false \
				use_dbus=true \
				use_gio=true \
				use_glib=true \
				use_gold=true \
				use_gtk3=true \
				use_libpci=true \
				use_pulseaudio=true \
				use_sysroot=false \
				use_system_harfbuzz=false \
				use_system_libjpeg=false \
				use_vaapi=true \
				rtc_enable_protobuf=false \
				is_clang=true \
				clang_base_path="/usr" \
				clang_use_chrome_plugins=false \
				use_lld=false \
				is_cfi=false \
				use_thin_lto=false \
				fatal_linker_warnings=false \
				target_os="linux" \
				current_os="linux" \
				optimize_webui=false

# FFmpeg-mt
# Ignore the version built as part of chromium. Build it separately twice, once with
# the Chrome branding to gain the extra codecs, and once without branding.
# The ffmpeg_branding variable controls which codecs are built inside the sumo lib.
# By default, ffmpeg_branding equals to "Chromium" and only builds the ogg/vorbis/theora codecs.
# When set to "Chrome", it also builds aac/ac3/mpeg4audio/h264/mov/mp3
ffmpeg_std_defines   += proprietary_codecs=false
ffmpeg_extra_defines += proprietary_codecs=true ffmpeg_branding="Chrome"

## Debian got this name wrong. DEB_HOST is not the hosting machine of
## compilation, but the destination of the resulting compiled programs.
## DEB_HOST = target. DEB_BUILD = hosting machine. Someone forgot what
## "to host" means, and thought of "host" as "computery thing".

ifeq (armel,$(DEB_HOST_ARCH))
common_defines += target_cpu="arm"
common_defines += arm_use_neon=false
common_defines += v8_use_arm_eabi_hardfloat=false
common_defines += arm_float_abi="soft" arm_use_thumb=false
else ifeq (armhf,$(DEB_HOST_ARCH))
common_defines += target_cpu="arm"
common_defines += arm_use_neon=false
common_defines += arm_float_abi="hard" arm_use_thumb=true
else ifeq (arm64,$(DEB_HOST_ARCH))
common_defines += target_cpu="arm64"
else ifeq (i386,$(DEB_HOST_ARCH))
common_defines += target_cpu="x86"
else ifeq (amd64,$(DEB_HOST_ARCH))
common_defines += target_cpu="x64"
else ifeq (mipsel,$(DEB_HOST_ARCH))
common_defines += target_cpu="mipsel"
else ifeq ($(DEB_HOST_ARCH),)
else
$(warning unanticipated arch $(DEB_HOST_ARCH))
common_defines += target_arch="$(DEB_HOST_ARCH)"
endif

ifeq (1,$(WANT_OZONE))
common_defines += use_ozone=true use_xkbcommon=true
endif

subst_files = \
	debian/$(DEBIAN_NAME).sh \
	$(NULL)

DEB_DH_SHLIBDEPS_ARGS_chromium-codecs-ffmpeg := -ldebian/iridium-codecs-ffmpeg/$(LIB_DIR)
DEB_DH_SHLIBDEPS_ARGS_chromium-codecs-ffmpeg-extra := -ldebian/iridium-codecs-ffmpeg-extra/$(LIB_DIR)

# enable compile-time dependency on gnome-keyring
# TODO: do this?

# Build chrome and chrome_sandbox, and chromedriver
BUILD_TARGETS += \
	pdf \
	chrome \
	chromedriver \
	clear_key_cdm \
	$(NULL)

# Needed to preserve the suid and make the sandbox happy
DEB_FIXPERMS_EXCLUDE := chrome-sandbox

ifneq (,$(GOOGLEAPI_APIKEY_$(DISTRIBUTION)))
common_defines += \
	google_api_key="$(GOOGLEAPI_APIKEY_$(DISTRIBUTION))" \
	google_default_client_id="$(GOOGLEAPI_CLIENTID_$(DISTRIBUTION))" \
	google_default_client_secret="$(GOOGLEAPI_CLIENTSECRET_$(DISTRIBUTION))"
else
$(warning Google API info is not set in build variables GOOGLEAPI_APIKEY_$(DISTRIBUTION) GOOGLEAPI_CLIENTID_$(DISTRIBUTION) GOOGLEAPI_CLIENTSECRET_$(DISTRIBUTION))
endif

ifneq (,$(filter 14.04%,$(UBUNTU_DIST)))
ifeq (i386,$(DEB_HOST_ARCH_CPU))
common_defines += use_pic=false
endif
endif

### Official interfaces.

# Debian Policy §4.9
%:
	@set -eux
	dh $@ --sourcedirectory=$(SRC_DIR) --builddirectory=$(SRC_DIR)/out/$(BUILD_TYPE)

# There's a build directory in the source tree that may or may not be interpreted as a target.
# As a result, make will do nothing. As a workaround, define an explicit target.
build:
	@set -eux
	dh $@ --sourcedirectory=$(SRC_DIR) --builddirectory=$(SRC_DIR)/out/$(BUILD_TYPE)

# BZR build-deb rule
get-packaged-orig-source: URL=https://downloads.iridiumbrowser.de/source/iridium-browser-$(ORIG_VERSION).tar.xz
get-packaged-orig-source: SUMURL=https://downloads.iridiumbrowser.de/source/iridium-browser-$(ORIG_VERSION).tar.xz.hashes
get-packaged-orig-source: DEST_FILENAME=iridium-browser_$(ORIG_VERSION).orig.tar.xz
get-packaged-orig-source:
	@set -eux
	wget --continue --progress=dot:mega $(URL)
	diff -u <(wget -O - -q $(SUMURL); echo) <(for crypto in md5 sha1 sha224 sha256 sha384 sha512; do echo -n "$$crypto  "; $${crypto}sum 'iridium-$(ORIG_VERSION).tar.xz'; done)
	mv "iridium-$(ORIG_VERSION).tar.xz" "$(DEST_FILENAME)"

# Debian Policy §4.9
get-orig-source: get-orig-source-dev

get-orig-source-dev:
	@set -eux
	ORIG_VERSION=$(shell wget -qO- "https://omahaproxy.appspot.com/all?os=linux&channel=dev" | grep linux | while IFS=, read col1 col2 col3 col4; do echo $$col3; done) debian/rules get-packaged-orig-source

get-orig-source-beta:
	@set -eux
	ORIG_VERSION=$(shell wget -qO- "https://omahaproxy.appspot.com/all?os=linux&channel=beta" | grep linux | while IFS=, read col1 col2 col3 col4; do echo $$col3; done) debian/rules get-packaged-orig-source



### Below here, rules that support those above required ones. "override_dh_"*
### are of course DH customizations

override_dh_auto_configure: $(SRC_DIR)/gn
	$(SRC_DIR)/gn gen --args='$(common_defines) $(browser_defines) $(ffmpeg_extra_defines)' $(SRC_DIR)/out/$(BUILD_TYPE)
	#$(SRC_DIR)/gn gen --args='$(common_defines) $(ffmpeg_extra_defines)' $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-extra
	#$(SRC_DIR)/gn gen --args='$(common_defines) $(ffmpeg_std_defines)' $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-std

override_dh_auto_build:
	$(NINJA) -v -k 1 -C $(SRC_DIR)/out/$(BUILD_TYPE) $(BUILD_TARGETS)
	#$(NINJA) -v -C $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-extra third_party/ffmpeg
	#$(NINJA) -v -C $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-std third_party/ffmpeg

override_dh_builddeb:
	dh_builddeb -- -Zxz

override_dh_shlibdeps:
	dh_shlibdeps -l$(CURDIR)/debian/iridium-browser/usr/lib/iridium-browser/ 2>&1 | grep -v "dpkg-shlibdeps: warning: can't extract name and version from library name" || true

override_dh_install-arch: TRASH := .deps gen obj obj.host obj.target \*.lock build.\* .landmines mksnapshot\* protoc pyproto re2c resources yasm \*.TOC product_logo_\*png gen\* lib/\* lib libvpx_obj_int_extract .ninja\* chrome-wrapper \*.breakpad.\* java_mojo dump_syms browser_test_resources ar_sample_test_driver unittests app_streaming/dev locales remoting_locales v8_build_config.json v8_context_snapshot_generator args.gn gn \*.ninja angledata brotli character_data_generator \*.pak.info flatc \*.service libEGL.so libGLESv2.so libVkLayer_\* libwidevinecdm\*
override_dh_install-arch: debian/iridium-browser.sh
	@set -eux
	# Two stages: Install out of source tree. Copy to packaging.
	mkdir -p debian/tmp/$(LIB_DIR)
	(cd $(SRC_DIR)/out/$(BUILD_TYPE) && tar -cf - $(foreach excl,$(TRASH),--exclude=$(excl) ) --dereference .;) | (cd debian/tmp/$(LIB_DIR) && tar -xvf -;)
	(cd $(SRC_DIR)/out/$(BUILD_TYPE) && tar -cf - $$(find *locales -type f -name en-US.pak) --dereference;) | (cd debian/tmp/$(LIB_DIR) && tar xvf -;)
	#rm debian/tmp/$(LIB_DIR)/libffmpeg.so
	
	# Icons
	dh_installdirs --package=$(DEBIAN_NAME) usr/share/pixmaps
	cp -a $(SRC_DIR)/chrome/app/theme/chromium/product_logo_48.png debian/$(DEBIAN_NAME)/usr/share/pixmaps/$(DEBIAN_NAME).png
	for size in 22 24 48 64 128 256 ; do \
		if test -f "chrome/app/theme/chromium/product_logo_$$size.png"; then \
			dh_installdirs --package=iridium-browser usr/share/icons/hicolor/$${size}x$${size}/apps ; \
			cp -a chrome/app/theme/chromium/product_logo_$$size.png  \
				debian/iridium-browser/usr/share/icons/hicolor/$${size}x$${size}/apps/iridium-browser.png ; \
		else \
			echo "There is no size $${size} icon. We expected one."; \
		fi; \
	done
	mkdir -p debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/scalable/apps
	cp -a debian/iridium-browser.svg debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/scalable/apps
	
	##### Copy installed to package ####
	(cd debian/tmp/$(LIB_DIR); tar cf - --exclude=chromedriver .;) | (cd debian/iridium-browser/$(LIB_DIR); tar xvf -;)
	
	# Preferences
	install -T -m 644 debian/iridium-browser.default debian/iridium-browser/etc/iridium-browser/default
	
	# Rename the binary from chrome to $(DEBIAN_NAME), this is required
	# as chromium dereferences all symlinks before starting its children
	# making the Gnome System Monitor confused with icons
	mv debian/iridium-browser/$(LIB_DIR)/chrome debian/iridium-browser/$(LIB_DIR)/iridium-browser
	
	# Launcher script
	install -T -m 755 debian/iridium-browser.sh debian/iridium-browser/usr/bin/iridium-browser
	
	# Generate the manpage and install it
	mkdir -p debian/iridium-browser/usr/share/man/man1
	sed \
	  -e "s:/@@PACKAGE@@:/iridium:g" \
	  -e "s:@@PACKAGE@@:iridium-browser:g" \
	  -e "s:@@MENUNAME@@:Iridium:g" \
	  chrome/app/resources/manpage.1.in \
	  > debian/iridium-browser/usr/share/man/man1/iridium-browser.1
	gzip -9n debian/iridium-browser/usr/share/man/man1/iridium-browser.1
	
	#dh_installdirs --package=iridium-codecs-ffmpeg usr/lib/iridium-browser
	#cp $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-std/libffmpeg.so debian/iridium-codecs-ffmpeg/usr/lib/iridium-browser/
	
	#dh_installdirs --package=iridium-codecs-ffmpeg-extra usr/lib/iridium-browser
	#cp $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-extra/libffmpeg.so debian/iridium-codecs-ffmpeg-extra/usr/lib/iridium-browser/
	
	dh_install -a

override_dh_install-indep:
	@set -eux
	dh_install -i
	(cd $(SRC_DIR)/out/$(BUILD_TYPE) && tar -cf - $$(find *locales -type f ! -name en-US.pak) --dereference;) | (cd debian/iridium-browser-l10n/$(LIB_DIR) && tar xvf -;)
	install --directory debian/iridium-browser/etc/iridium-browser/customizations
	install --owner=root --mode=0644 --no-target-directory debian/iridium-browser-customization-example debian/iridium-browser/etc/iridium-browser/customizations/00-example

override_dh_auto_test:
	# No tests to run

override_dh_strip:
	dh_strip --no-automatic-dbgsym

override_dh_clean:
	@set -eux
	rm -rf debian/*-stamp debian/stamp-* debian/tmp-*
	rm -f $(subst_files)
	rm -rf $(SRC_DIR)/out $(FFMPEG_SRC_DIR)/yasm $(FFMPEG_SRC_DIR)/out $(FFMPEG_SRC_DIR)/*.mk
	find $(SRC_DIR) \( -name \*.pyc -o -name \*.pyo -name \*.o \) -delete
	find $(SRC_DIR) -regex '.*/\([^/]*\)/\1[^/]*\.\(Makefile\|\(target\|host\)\.mk\)' -delete
#	sed -i 's,/etc/$(DEBIAN_NAME)/policies,/etc/chromium/policies,' \
#		$(SRC_DIR)/chrome/common/chrome_paths.cc
	
	dh_clean

# By default, Chromium expects a binary file that generates the build files. Fortunately,
# the source files for gn (the binary) are present in the source tree.
$(SRC_DIR)/gn:
	CC=$(shell which clang clang-4.0 | head -n 1) CXX=$(shell which clang++ clang++-4.0 | head -n 1) tools/gn/bootstrap/bootstrap.py --gn-gen-args '$(common_defines)'
	cp $(SRC_DIR)/out/Release/gn $(SRC_DIR)/gn
	rm -rf $(SRC_DIR)/out

debian/iridium-browser.sh: debian/iridium-browser.sh.in
	@set -eux
	### configure
	sed -e 's/@BUILD_DIST@/$(DEBIAN_DIST_NAME) $(DEBIAN_DIST_VERSION)/g' \
	    -e 's/@BUILD_DIST_NAME@/$(DEBIAN_DIST_NAME)/g' \
	    -e 's/@BUILD_DIST_VERSION@/$(DEBIAN_DIST_VERSION)/g' \
	    -e 's/@UPSTREAM_VERSION@/$(DEBIAN_UPSTREAM_VERSION)/g' \
	    $< > $@

# These are (leading-slash-less) files that are created by upstream builder,
# but intentionally not packaged.  The should match very specifically so we
# know they only match things we should ignore. No false negatives, plz.
# When composing a match, it has to be escaped for nonquoted shell expression
# first, then escaped for Make. No expression can contain a space, as it's
# used later to join expressions in alternation; a dot should suffice.
# Useful: https://code.google.com/p/chromium/codesearch#search/q=package:chromium
## webkit tests
#BUILT_UNUSED_MATCH  = ^usr/lib/iridium-browser/AHEM____.TTF$$
#BUILT_UNUSED_MATCH += ^usr/lib/iridium-browser/fonts.conf$$
#BUILT_UNUSED_MATCH += ^usr/lib/iridium-browser/libTestNetscapePlugIn.so$$
#BUILT_UNUSED_MATCH += ^usr/lib/iridium-browser/plugins/libTestNetscapePlugIn.so$$
#BUILT_UNUSED_MATCH += ^usr/lib/iridium-browser/ImageDiff$$
## xdg-utils install dep

BUILT_UNUSED_MATCH += ^usr/lib/iridium-browser/xdg-\(mime\|settings\)$$

INDEP_MATCH = ^usr/lib/iridium-browser/.\*\(?\!\<pseudo-\)locales/.\*.pak$$

PACKAGED_NOT_FROM_TREE_MATCH  = ^usr/share/applications/iridium-browser.desktop$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/apport/package-hooks/iridium-browser.py$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/iridium-browser/README.source$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/iridium-browser/TODO.Debian$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/iridium-browser/copyright.problems.gz$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/.\*/buildinfo_.\*.gz$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/gnome-control-center/default-apps/iridium-browser.xml$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/.\*/iridium-browser.png$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/.\*/iridium-browser.svg$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/.\*/copyright$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/.\*/changelog.Debian.gz$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/bin/iridium-browser$$
PACKAGED_NOT_FROM_TREE_MATCH += ^etc/iridium-browser/default$$
PACKAGED_NOT_FROM_TREE_MATCH += ^etc/iridium-browser/customizations/00-example$$

# Expressions to map filenames in the comparison logic so that a rename in
# packaging is correctly handled.  No expression contains a space.  Each should
# be able to be used as a SED expression.  This changes file names in
# pre-packaging listing. All of this is done AFTER removing items
# BUILD_UNUSED_MATCH .  Quote for regexp, then shell, then Make.
#  Man pages are moved, renamed, compressed.
RENAMED_FILE  = s,^usr/lib/iridium-browser/chrome.1\$$,usr/share/man/man1/iridium-browser.1.gz,
#  Sandbox name is wrong. Upstream builder deficiency.
RENAMED_FILE += s,^usr/lib/iridium-browser/chrome_sandbox$$,usr/lib/iridium-browser/chrome-sandbox,
#  Executable name is different.
RENAMED_FILE += s,^usr/lib/iridium-browser/chrome$$,usr/lib/iridium-browser/iridium-browser,

compare-indep: INDEP_MATCH = ^usr/lib/iridium-browser/.\*\(?\!\<pseudo-\)locales/.\*.pak$$
compare-indep: SPACE := $(eval) $(eval)
compare-indep:
	@set -eux; \
	T=$$(mktemp -d -t iridium-comparisons-XXXXXXX); \
	test "$${T}"; \
	test -d $${T}; \
	find debian/tmp debian/tmp-extra debian/tmp-std -type f |cut -d/ -f3- >$${T}/unfiltered-built; \
	grep -E \($(subst $(SPACE),\|,$(INDEP_MATCH))\) $${T}/unfiltered-built $(foreach expr,$(RENAMED_FILE), |sed -r -e $(expr))  |grep -v /en-US.pak | LC_ALL=C sort >$${T}/built; \
	dh_listpackages -i |while read pkgname; do find debian/$${pkgname} -type f; done |grep -v /DEBIAN |cut -d/ -f3- |grep -v ^usr/lib/debug/ | LC_ALL=C sort >$${T}/unfiltered-packaged; \
	grep -E \($(subst $(SPACE),\|,$(INDEP_MATCH))\) $${T}/unfiltered-packaged |grep -v /en-US.pak >$${T}/packaged || true; \
	if ! diff -U0 $${T}/built $${T}/packaged; then  \
	  echo " => Found indep differences, please investigate. $${T}/built $${T}/packaged" ; \
	  exit 1; \
	fi; \
	rm -r $${T}

compare-arch: INDEP_MATCH = ^usr/lib/iridium-browser/.\*\locales/.\*.pak$$
compare-arch: PKG_DIRS := $(addprefix debian/,$(shell dh_listpackages -a))
compare-arch: SPACE := $(eval) $(eval)
compare-arch:
	@set -eux; \
	T=$$(mktemp -d -t iridium-comparisons-XXXXXXX);\
	test "$${T}"; \
	test -d $${T};\
	DUPES=`find $(PKG_DIRS) -type f -print | grep -v /DEBIAN/ | cut -d/ -f3- | LC_ALL=C sort | uniq -c | grep -vE '^ *2 .*/libffmpeg.so$$' | grep -vE '^  *1 '` || true; \
	if [ "Z$$DUPES" != Z ] ; then \
	  echo " => Found duplicates:\n $$DUPES" ; \
	  exit 1 ; \
	fi; \
	find debian/tmp debian/tmp-extra debian/tmp-std -type f |cut -d/ -f3- >$${T}/unfiltered-built ;\
	grep -vE \($(subst $(SPACE),\|,$(BUILT_UNUSED_MATCH))\) $${T}/unfiltered-built $(foreach expr,$(RENAMED_FILE), |sed -r -e $(expr)) |grep -vE \($(subst $(SPACE),\|,$(INDEP_MATCH))\) | LC_ALL=C sort >$${T}/built ;\
	find $(PKG_DIRS) -type f |grep -v /DEBIAN |cut -d/ -f3- |grep -v ^usr/lib/debug/ | LC_ALL=C sort >$${T}/unfiltered-packaged ;\
	grep -vE \($(subst $(SPACE),\|,$(PACKAGED_NOT_FROM_TREE_MATCH))\) $${T}/unfiltered-packaged |grep -vE \($(subst $(SPACE),\|,$(INDEP_MATCH))\) >$${T}/packaged ;\
	if ! diff -U0 $${T}/built $${T}/packaged; then \
	  echo " => Found archdep differences, please investigate. $${T}/built $${T}/packaged" ; \
	  exit 1; \
	fi; \
	for expr in $(BUILT_UNUSED_MATCH); do if ! grep -E $$expr $${T}/unfiltered-built >/dev/null; then echo "Warning: Unused built matcher: $$expr in $${T}/unfiltered-built "; fi; done; \
	for expr in $(PACKAGED_NOT_FROM_TREE_MATCH); do if ! grep -E $$expr $${T}/unfiltered-packaged >/dev/null; then echo "Warning: Unused packaged matcher: $$expr"; fi; done; \
	rm -r $${T};

###############################################################################################
# Translations

# New langs with enough coverage to land in official builds
# (leave empty to accept all new lang)
patch-translations: T := $(shell mktemp -d -t iridium-launchpad-translations-XXXXXXX)
patch-translations: GRIT_WHITELISTED_LANGS ?= ca@valencia eu gl ug gd
patch-translations: TRANSLATIONS_TOOLS_BRANCH  := http://bazaar.launchpad.net/~chromium-team/chromium-browser/chromium-translations-tools.head
patch-translations: TRANSLATIONS_EXPORT_BRANCH := http://bazaar.launchpad.net/~chromium-team/chromium-browser/chromium-translations-exports.head
patch-translations: GRIT_TEMPLATES := chrome/app/chromium_strings.grd chrome/app/generated_resources.grd components/policy/resources/policy_templates.grd ui/strings/ui_strings.grd
patch-translations: OTHER_GRIT_TEMPLATES := ui/strings/app_locale_settings.grd chrome/app/resources/locale_settings.grd chrome/app/resources/locale_settings_linux.grd
patch-translations: MAPPED_GRIT_TEMPLATES := --map-template-names ui/strings/ui_strings.grd=ui/strings/app_strings.grd
patch-translations: GRIT_CONVERTER_FLAGS := --create-patches translations-patches --import-gettext ../translations-export --export-grit $(T)/translations-grit --build-gyp-file build/common.gypi --other-grd-files $(shell echo $(OTHER_GRIT_TEMPLATES) | tr ' ' ',') $(MAPPED_GRIT_TEMPLATES) 
ifeq (1,$(WANT_ONLY_WHITELISTED_NEW_LANGS))
patch-translations: GRIT_CONVERTER_FLAGS += --whitelisted-new-langs $(shell echo $(GRIT_WHITELISTED_LANGS) | tr ' ' ',')
endif
patch-translations: PATCH_FILE := launchpad_translations.patch
patch-translations:
	@set -eux
	ls $(GRIT_TEMPLATES)
	ls $(OTHER_GRIT_TEMPLATES)
	test -d $(T)/translations-tools || bzr export $(T)/translations-tools $(TRANSLATIONS_TOOLS_BRANCH)
	test -d $(T)/translations-export || bzr export $(T)/translations-export $(TRANSLATIONS_EXPORT_BRANCH)
	( cd $(SRC_DIR); $(T)/translations-tools/chromium2pot.py $(GRIT_CONVERTER_FLAGS) $$(ls $(GRIT_TEMPLATES)); )
	quilt delete "$(PATCH_FILE)" || true
	quilt new "$(PATCH_FILE)"
	( cd $(T)/translations-grit && find * -type f ) |while read updatedfile; do \
		quilt add -P "$(PATCH_FILE)" $(SRC_DIR)/"$$updatedfile"; \
		cp $(T)/translations-grit/"$$updatedfile" $(SRC_DIR)/"$$updatedfile"; \
	done;
	{ echo "Description: Contributed translations from Launchpad. "; echo; } |quilt header -r "$(PATCH_FILE)"
	quilt refresh -pab "$(PATCH_FILE)"
	@echo "Patch needs committing."

.PHONY: build get-packaged-orig-source override_dh_* local-install-* patch-translations compare-*
