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

LIB_DIR        := usr/lib/chromium-browser

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

# Set up Google API keys, see http://www.chromium.org/developers/how-tos/api-keys .
# Note: these are for Ubuntu use ONLY. For your own distribution,
# please get your own set of keys.
# Permission to add API keys, from Paweł Hajdan, To chad.miller@canonical.com
# msgid: CAADNaOFSFoch68NM1SGpCTRXqmspyKQgUPUtsF7SGRsRXiHZcg@mail.gmail.com
GOOGLEAPI_APIKEY_UBUNTU := AIzaSyDGQzx0c0ptZVDriLi9Wblo2voeLnmPq-o
GOOGLEAPI_CLIENTID_UBUNTU := 424119844901-tilod0e1nm0dt85e1evrdfp3cc3ec01d.apps.googleusercontent.com
GOOGLEAPI_CLIENTSECRET_UBUNTU := WCCejhqORuxKG272GAWxsPIU

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

# enable verbose build messages
export DH_VERBOSE=1

# enable all build hardening flags
#export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# Don't let dh_installinit install *.default in /etc/default
DEB_DH_INSTALLINIT_ARGS += --noscripts --onlyscripts

# Don't let scour touch the svg files
DEB_DH_SCOUR_ARGS += -Xsvg

# FFmpeg-mt
# Builds 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 ffmpeg 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
DEB_DH_SHLIBDEPS_ARGS_chromium-codecs-ffmpeg := -ldebian/chromium-codecs-ffmpeg/$(LIB_DIR)
DEB_DH_SHLIBDEPS_ARGS_chromium-codecs-ffmpeg-extra := -ldebian/chromium-codecs-ffmpeg-extra/$(LIB_DIR)

common_defines+=\
	enable_google_now=false \
	enable_hangout_services_extension=true \
	enable_mdns=true \
	enable_nacl=false \
	enable_wayland_server=false \
	enable_webrtc=true \
	enable_widevine=true \
	fieldtrial_testing_like_official_build=true \
	is_component_build=false \
	is_component_ffmpeg=true \
	is_debug=false \
	is_desktop_linux=true \
	is_official_build=true \
	remove_webcore_debug_symbols=true \
	symbol_level=1 \
	treat_warnings_as_errors=false \
	use_allocator="none" \
	use_alsa=true \
	use_aura=true \
	use_cups=true \
	use_dbus=true \
	use_gconf=false \
	use_gio=true \
	use_glib=true \
	use_gold=false \
	use_gtk3=true \
	use_libpci=true \
	use_pulseaudio=true \
	use_sysroot=false \
	use_system_harfbuzz=false \
	use_system_libjpeg=false \
	use_system_sqlite=false \
	rtc_enable_protobuf=false \
	is_clang=true \
	clang_base_path="/usr" \
	clang_use_chrome_plugins=false \
	use_custom_libcxx=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_std_defines   += proprietary_codecs=false
ffmpeg_extra_defines += proprietary_codecs=true ffmpeg_branding="Chrome"

# system libraries to use
use_system_libraries := #yasm libwebp libevent


#!#GN_DEF += enable_hidpi=true
$(warning gn hidpi)
#!#GYP_DEFINES += enable_touch_ui=1
$(warning gn touch ui)


## 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
common_defines += symbol_level=0
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 (armel,$(DEB_BUILD_ARCH))
else ifeq (armhf,$(DEB_BUILD_ARCH))
else ifeq (arm64,$(DEB_BUILD_ARCH))
else ifeq (i386,$(DEB_BUILD_ARCH))
else ifeq (amd64,$(DEB_BUILD_ARCH))
else ifeq (mipsel,$(DEB_BUILD_ARCH))
else
$(warning unanticipated arch $(DEB_HOST_ARCH))
endif


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

### Official interfaces.

# Debian Policy §4.9
binary binary-arch binary-indep build build-arch build-indep clean install install-arch install-indep patch:
	dh $@ --builddirectory=out --with quilt

# BZR build-deb rule
get-packaged-orig-source: URL=https://commondatastorage.googleapis.com/chromium-browser-official/chromium-$(ORIG_VERSION).tar.xz
get-packaged-orig-source: SUMURL=https://commondatastorage.googleapis.com/chromium-browser-official/chromium-$(ORIG_VERSION).tar.xz.hashes
get-packaged-orig-source: DEST_FILENAME=chromium-browser_$(ORIG_VERSION).orig.tar.xz
get-packaged-orig-source:
	wget --continue --progress=dot:giga $(URL)
	wget -O sumfile -q $(SUMURL); echo >>sumfile
	for hasher in md5 sha1 sha224 sha256 sha384 sha512; do echo -n "$$hasher  "; $${hasher}sum chromium-$(ORIG_VERSION).tar.xz; done >testfile
	diff testfile sumfile
	mv "chromium-$(ORIG_VERSION).tar.xz" "$(DEST_FILENAME)"

# Debian Policy §4.9
get-orig-source:
	test "$(ORIG_VERSION)" || { echo "Set ORIG_VERSION" to use this rule.; exit 1; }
	ORIG_VERSION=$(ORIG_VERSION) $(MAKE) get-packaged-orig-source


### Below here, rules that support those above required ones.

override_dh_auto_configure: ARG_TEST_FILE := debian/known_gn_gen_args-$(DEB_HOST_ARCH)
override_dh_auto_configure: out/Release/gn
	# Upstream changes often. Fail when they introduce a config flag we don't know about.
	out/Release/gn gen out/argtest --fail-on-unused-args --args='$(common_defines) $(browser_defines) $(ffmpeg_extra_defines)'
	out/Release/gn args out/argtest --list --short |sed -e 's,$(HOME),$$HOME,' >out/argtest/discovered-gn-values
	@diff --suppress-common-lines --unified=0 $(ARG_TEST_FILE) out/argtest/discovered-gn-values || { echo; echo; echo Do:  cat \>$(ARG_TEST_FILE) \<\<EOF; cat out/argtest/discovered-gn-values; echo EOF; echo; echo; }

	-grep is_component out/argtest/discovered-gn-values

	test ! "$(use_system_libraries)" || python2 build/linux/unbundle/replace_gn_files.py --system-libraries $(use_system_libraries)
	out/Release/gn gen --fail-on-unused-args --args='$(common_defines) $(browser_defines) $(ffmpeg_extra_defines)' out/Release

	out/Release/gn gen --fail-on-unused-args --args='$(common_defines) $(ffmpeg_std_defines)' out/ffmpeg-std


# There are two things to build. Standard ffmpeg+browser, and full ffmpeg.
override_dh_auto_build: BUILD_TARGETS := chrome chrome_sandbox chromedriver
override_dh_auto_build:

	while sleep 1200; do echo "#  I ATEN'T DEAD"; done& ninja -v -C out/Release $(BUILD_TARGETS) && { kill \%1; sleep 1; echo; true; } || { kill \%1; exit 1; sleep 2; echo; false; }

	ninja -v -C out/ffmpeg-std libffmpeg.so


override_dh_fixperms:
	dh_fixperms --exclude chrome-sandbox

override_dh_builddeb:
	dh_builddeb -- -Zxz

override_dh_installman:
	# install does this

override_dh_install-arch: INDEP_MATCH = ^usr/lib/chromium-browser/.\*\locales/.\*.pak$$
override_dh_install-arch: PKG_DIRS := $(addprefix debian/,$(shell dh_listpackages -a))
override_dh_install-arch: T := verify-install-integrity-dep
override_dh_install-arch: SPACE := $(eval) $(eval)
override_dh_install-arch: S_CR_PATH := apps/chromium/current
override_dh_install-arch: TRASH := \
	gen\* \
	obj \
	obj.host \
	obj.target \
	\*.lock \
	build.\* \
	.landmines \
	mksnapshot\* \
	protoc \
	pyproto \
	re2c \
	resources \
	yasm \
	\*.TOC \
	product_logo_\*png \
	chrome-wrapper \
	ar_sample_test_driver \
	unittests \
	locales \
	remoting_locales \
	v8_build_config.json \
	v8_context_snapshot_generator \
	args.gn \
	gn \
	\*.ninja \
	.ninja\* \
	angledata \
	brotli \
	character_data_generator \
	\*.pak.info \
	flatc \
	\*.service \
	./libEGL.so \
	./libGLESv2.so \
	libVkLayer_\* \
	libwidevinecdm\*
override_dh_install-arch: debian/chromium-browser.sh
override_dh_install-arch:
	# Two stages: Install out of source tree. Copy to packaging.

	mkdir -p $(T)

	mkdir -p                             $(CURDIR)/debian/tmp-extra/$(LIB_DIR)
	mv -v out/Release/libffmpeg.so       $(CURDIR)/debian/tmp-extra/$(LIB_DIR)

	mkdir -p                             $(CURDIR)/debian/tmp-std/$(LIB_DIR)
	mv -v out/ffmpeg-std/libffmpeg.so    $(CURDIR)/debian/tmp-std/$(LIB_DIR)

	mkdir -p $(CURDIR)/debian/tmp/$(LIB_DIR)
	(cd out/Release && tar --remove-files $(foreach excl,$(TRASH),--exclude=$(excl) ) --dereference -cf - .;) | (cd debian/tmp/$(LIB_DIR) && tar xvf -;)
	(cd out/Release && tar --remove-files -cf - $$(find *locales -type f -name en-US.pak) --dereference;) | (cd debian/tmp/$(LIB_DIR) && tar xvf -;)
#
	# NaCL may be blacklisted.
	cp -a out/Release/libppGoogleNaClPluginChrome.so debian/chromium-browser/$(LIB_DIR)/ || echo No NaCl. That is okay.
	cp -a out/Release/nacl_irt_* debian/chromium-browser/$(LIB_DIR)/ || echo No NaCl. That is okay.

	# Icons
	cp -a chrome/app/theme/chromium/product_logo_48.png debian/chromium-browser/usr/share/pixmaps/chromium-browser.png
	for size in 22 24 32 48 64 128 256 ; do \
	  if test -f "chrome/app/theme/chromium/product_logo_$$size.png"; then \
	    dh_installdirs --package=chromium-browser usr/share/icons/hicolor/$${size}x$${size}/apps ; \
	    cp -a chrome/app/theme/chromium/product_logo_$$size.png  \
	      debian/chromium-browser/usr/share/icons/hicolor/$${size}x$${size}/apps/chromium-browser.png ; \
	  else \
	    echo "There is no size $${size} icon. We expected one."; \
	  fi; \
	done
	cp -a debian/chromium-browser.svg debian/chromium-browser/usr/share/icons/hicolor/scalable/apps


	# Record files that were built, so we can compare later that we installed everything.
	find debian/tmp debian/tmp-std 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

	#
	#
	##### Copy installed to package ####
	dh_install -a

	mkdir -p debian/chromium-chromedriver/$(LIB_DIR)
	mkdir -p debian/chromium-browser/$(LIB_DIR)

	install -T -m 644 debian/chromium-browser.default debian/chromium-browser/etc/chromium-browser/default

	# Rename the binary from chrome to chromium-browser, this is required as
	# chromium dereferences all symlinks before starting its children making
	# the Gnome System Monitor confused with icons
	install -T -m 555 debian/tmp/$(LIB_DIR)/chrome debian/chromium-browser/$(LIB_DIR)/chromium-browser
	rm                debian/tmp/$(LIB_DIR)/chrome

	# "you have to change the underscore from the build target into a hyphen".
	# https://code.google.com/p/chromium/wiki/LinuxSUIDSandbox
	install -T -m 4555 debian/tmp/$(LIB_DIR)/chrome_sandbox debian/chromium-browser/$(LIB_DIR)/chrome-sandbox
	rm                 debian/tmp/$(LIB_DIR)/chrome_sandbox

	# move chromedriver into its own package
	install    -m 555 debian/tmp/$(LIB_DIR)/chromedriver debian/chromium-chromedriver/$(LIB_DIR)
	rm                debian/tmp/$(LIB_DIR)/chromedriver

	mv -iv debian/tmp/$(LIB_DIR)/* debian/chromium-browser/$(LIB_DIR)/

	install -T -m 755 debian/chromium-browser.sh debian/chromium-browser/usr/bin/chromium-browser

	# Generate the manpage and install it
	mkdir -p debian/chromium-browser/usr/share/man/man1
	sed \
	  -e "s:/@@PACKAGE@@:/chromium:g" \
	  -e "s:@@PACKAGE@@:chromium-browser:g" \
	  -e "s:@@MENUNAME@@:Chromium:g" \
	  chrome/app/resources/manpage.1.in \
	  > debian/chromium-browser/usr/share/man/man1/chromium-browser.1
	gzip -9n debian/chromium-browser/usr/share/man/man1/chromium-browser.1
	echo "usr/share/man/man1/chromium-browser.1.gz" >> $(T)/built
	LC_ALL=C sort $(T)/built -o $(T)/built

	# this should find almost nothing
	find debian/chromium-browser/$(LIB_DIR)/locales -type f -name \*.pak -print


ifneq (,$(findstring Ubuntu,$(DEBIAN_DIST)))
	# we need space on the CD, so remove duplicates of the doc files
	# (See LP: #194574 as for why we don't let cdbs do it)
	@for doc in copyright AUTHORS changelog.Debian.gz ; do \
	  F=debian/chromium-browser/usr/share/doc/chromium-browser/$$doc ; \
	  for file in `find $(addsuffix /usr/share/doc,$(filter-out debian/chromium-browser,$(PKG_DIRS))) -type f -name $$doc -print`; do \
	    cmp -s $$file $$F ; \
	    if [ $$? -eq 0 ] ; then \
	      rm -f $$file ; \
	      echo "  symlinking $$doc in '"`echo $$file | awk 'BEGIN{ FS="/"} { print $$2 }'`"' to file in 'chromium-browser'" ; \
	      ( cd `dirname $$file` && ln -s ../chromium-browser/$$doc ) ; \
	    fi ; \
	  done ; \
	done
endif


	# compare built to packaged
	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 '`; \
	if [ "Z$$DUPES" != Z ] ; then \
	  echo " => Found duplicates:\n $$DUPES" ; \
	  exit 1 ; \
	fi;

	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)


override_dh_install-indep: INDEP_MATCH = ^usr/lib/chromium-browser/.\*\(?\!\<pseudo-\)locales/.\*.pak$$
override_dh_install-indep: SPACE := $(eval) $(eval)
override_dh_install-indep: T := verify-install-integrity-indep
override_dh_install-indep:

	### Step 1: install into tmp
	mkdir -p debian/tmp/$(LIB_DIR)
	(cd out/Release && tar --remove-files -cf - $$(find *locales -type f -name \*.pak \! -name en-US.pak) --dereference;) | (cd debian/tmp/$(LIB_DIR) && tar xvf -;)

	# record built so we can compare later
	mkdir -p $(T)
	find debian/tmp 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


	### Step 2: install into packages
	dh_install -i

	install --directory debian/chromium-browser/etc/chromium-browser/customizations
	install --owner=root --mode=0644 --no-target-directory debian/chromium-browser-customization-example debian/chromium-browser/etc/chromium-browser/customizations/00-example

	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)


override_dh_clean:
	test ! "$(use_system_libraries)" || python2 ./build/linux/unbundle/replace_gn_files.py --undo --system-libraries $(use_system_libraries) || true
	dh_clean
	test -d chrome
	rm -rf out
	rm -rf debian/chromium-browser.sh debian/tmp-*
	rm -rf verify-install-integrity-indep verify-install-integrity-arch
	rm -rf launchpad-translations

	find . \( -name \*.pyc -o -name \*.pyo -name \*.o \) -delete
	find . -regex '.*/\([^/]*\)/\1[^/]*\.\(Makefile\|\(target\|host\)\.mk\)' -delete

	set +x
	echo
	echo VERIFICATION:
	cd .. && for crypto in md5 sha1 sha224 sha256 sha384 sha512; do $${crypto}sum 'chromium-browser_$(DEBIAN_UPSTREAM_VERSION).orig.tar.xz' |sed -e 's/-browser//' -e "s/^/$${crypto}  /"; done
	echo VERIFICATION-
	echo Compare to https://commondatastorage.googleapis.com/chromium-browser-official/chromium-$(DEBIAN_UPSTREAM_VERSION).tar.xz.hashes
	echo

	-find native_client build/linux third_party/binutils -type f -executable -exec chmod -x {} \;

debian/chromium-browser.sh: debian/chromium-browser.sh.in
	### 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' \
    $< > $@

out/Release/gn:
	CC=clang-4.0 CXX=clang++-4.0 tools/gn/bootstrap/bootstrap.py --verbose --no-rebuild --gn-gen-args '$(common_defines)'
	echo built $@


# 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/chromium-browser/ar_sample_test_driver$$

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

PACKAGED_NOT_FROM_TREE_MATCH := ^usr/share/applications/chromium-browser.desktop$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/apport/package-hooks/chromium-browser.py$$
#PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/chromium-browser/README.source$$
#PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/chromium-browser/TODO.Debian$$
#PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/doc/chromium-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/chromium-browser.xml$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/.\*/chromium-browser.png$$
PACKAGED_NOT_FROM_TREE_MATCH += ^usr/share/.\*/chromium-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/chromium-browser$$
PACKAGED_NOT_FROM_TREE_MATCH += ^etc/chromium-browser/default$$
#PACKAGED_NOT_FROM_TREE_MATCH += ^etc/chromium-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/chromium-browser/chrome.1\$$,usr/share/man/man1/chromium-browser.1.gz,
#  Sandbox name is wrong. Upstream builder deficiency.
RENAMED_FILE += s,^usr/lib/chromium-browser/chrome_sandbox$$,usr/lib/chromium-browser/chrome-sandbox,
#  Executable name is different.
RENAMED_FILE += s,^usr/lib/chromium-browser/chrome$$,usr/lib/chromium-browser/chromium-browser,


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

# New langs with enough coverage to land in official builds
# (leave empty to accept all new lang)
patch-translations: T := launchpad-translations
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 webkit/glue/resources/webkit_resources.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 $$(echo $(OTHER_GRIT_TEMPLATES) | tr ' ' ',') $(MAPPED_GRIT_TEMPLATES) 
ifeq (1,$(WANT_ONLY_WHITELISTED_NEW_LANGS))
patch-translations: GRIT_CONVERTER_FLAGS += --whitelisted-new-langs $$(echo $(GRIT_WHITELISTED_LANGS) | tr ' ' ',')
endif
patch-translations: PATCH_FILE := launchpad_translations.patch
patch-translations:
	ls $(GRIT_TEMPLATES)
	ls $(OTHER_GRIT_TEMPLATES)
	test "$(T)"
	test -d $(T)
	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)
	( $(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)" "$$updatedfile"; \
		cp $(T)/translations-grit/"$$updatedfile" "$$updatedfile"; \
	done
	{ echo "Description: Contributed translations from Launchpad. "; echo; } |quilt header -r "$(PATCH_FILE)"
	quilt refresh -pab --no-timestamps "$(PATCH_FILE)";
	echo "Patch needs committing."
	rm -rf "$(T)"



.PHONY: binary binary-arch binary-indep build build-arch build-indep clean install install-arch install-indep patch get-packaged-orig-source gos override_dh_* local-install-* patch-translations compare-*

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