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

DEBIAN_CODECS_NAME      := $(subst browser,codecs-ffmpeg,$(DEBIAN_NAME))
DEBIAN_DRIVER_NAME      := $(subst browser,chromedriver,$(DEBIAN_NAME))

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
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

# 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_DEBUG       := 0
WANT_OZONE       := 0

# System libs off. Rationalle: We usually want to use as many system libraries
# as possible, as a security measure. If there's a security problem in one, the
# security team can replace it in the distro and everything magically starts
# working. Chromium is an exception. It moves quickly emough that testing that
# system libraries actually work is too hard, and upstream does a pretty good
# job of fixing known security problems soon.  As long as we keep the package
# fresh, no system libs is the smarter move.
WANT_SYSTEM_LIBS ?= 0
WANT_SHARED_LIBS ?= 0
VERBOSE          := 1
PROCESSORS       ?= $(shell grep -ic ^processor /proc/cpuinfo)
ifeq (0,$(PROCESSORS))
PROCESSORS := 1
endif

# Don't fail on compilation warnings
GN_DEFINES += treat_warnings_as_errors=false

# We are never using a sysroot-based toolchain; override the wrong
# autodetection for arm.
GN_DEFINES += use_sysroot=false
FFMPEG_MT_GN_DEFINES += use_sysroot=false

# NaCl isn't ready yet
GN_DEFINES += enable_nacl=false
FFMPEG_MT_GN_DEFINES += enable_nacl=false

ifneq (,$(filter arm%,$(DEB_HOST_ARCH_CPU)))
# Disable NaCl for arm until the toolchain is built
GN_DEFINES += enable_nacl=false
FFMPEG_MT_GN_DEFINES += enable_nacl=false
else
# Don't build anything related to newlib or glibc, as it's not needed for the
# core NaCl
GN_DEFINES += use_gcc_glibc=false use_clang_newlib=false
endif

# use gold, do not use bundled binutils
GN_DEFINES += use_gold=true linux_use_bundled_binutils=false
FFMPEG_MT_GN_DEFINES += use_gold=true linux_use_bundled_binutils=false

# get resources for high dpi and touch
# GN_DEFINES += enable_hidpi=true

# add support and stubbed implementation of widevine (needed for EME)
GN_DEFINES += enable_widevine=true

# disable field trial tests
GN_DEFINES += fieldtrial_testing_like_official_build=true

# don't vulcanize; we don't have the node.js modules needed readily available
GN_DEFINES += use_vulcanize=false

# don't use gtk3, mir is causing a conflict in the protobuf version
GN_DEFINES += use_gtk3=false

# use gtk3
# GN_DEFINES += use_gtk3=true
# FFMPEG_MT_GN_DEFINES += use_gtk3=true

# Intentional configuration, not bug work-arounds.
ifeq (arm,$(DEB_HOST_ARCH_CPU))

GN_DEFINES += \
	arm_use_neon=false \
	target_cpu="arm" \
	use_cups=true \
	$(NULL)
FFMPEG_MT_GN_DEFINES += \
	arm_use_neon=false \
	target_cpu="arm" \
	use_cups=true \
	$(NULL)

 ifeq (armel,$(DEB_HOST_ARCH))
GN_DEFINES += \
	arm_float_abi="soft" \
	arm_use_thumb=false \
	arm_version=6 \
	$(NULL)
 endif
 ifeq (armhf,$(DEB_HOST_ARCH))
# ARN Neon optional flag is off because it causes build errors, AND testing
# /proc/cpuinfo contents can't work with a sandbox anyway.
# In addition, some componenets appear to assume that Neon always exists.
GN_DEFINES += \
	arm_optionally_use_neon=false \
	arm_fpu="vfpv3-d16" \
	arm_float_abi="hard" \
	arm_use_thumb=true \
	arm_version=7 \
	$(NULL)
FFMPEG_MT_GN_DEFINES += \
	arm_optionally_use_neon=false \
	arm_fpu="vfpv3-d16" \
	arm_float_abi="hard" \
	arm_use_thumb=true \
	arm_version=7 \
	$(NULL)
 endif
endif
ifeq (arm64,$(DEB_HOST_ARCH))
GN_DEFINES += \
	target_cpu="arm64" \
	use_cups=true \
	use_allocator="none" \
	$(NULL)
FFMPEG_MT_GN_DEFINES += \
	target_cpu="arm64" \
	use_cups=true \
	use_allocator="none" \
	$(NULL)
endif
ifeq (amd64,$(DEB_HOST_ARCH))
GN_DEFINES += target_cpu="x64"
FFMPEG_MT_GN_DEFINES += target_cpu="x64"
endif
ifeq (i386,$(DEB_HOST_ARCH))
GN_DEFINES += target_cpu="x86"
FFMPEG_MT_GN_DEFINES += target_cpu="x86"
# SEGV on component builds, 2013-05
GN_DEFINES += use_allocator="none"
endif
ifeq (arm64,$(DEB_HOST_ARCH))
GN_DEFINES += use_allocator="none"
endif

# Do this for every architecture. More than 4GB of memory is too much to expect
# of builders.
COMPONENT_SHARED_LIB_BUILD := 0

ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
BROWSER_GN_DEFINES += is_component_build=true
FFMPEG_MT_GN_DEFINES += is_component_build=true
endif

# Webkit library is enormous. Exclude for now.
BROWSER_GN_DEFINES += remove_webcore_debug_symbols=true

ifeq (1,$(WANT_DEBUG))
BUILD_TYPE := Debug
else
BUILD_TYPE := Release
# Add symbols
GN_DEFINES += is_debug=false symbol_level=1
FFMPEG_MT_GN_DEFINES += is_debug=false symbol_level=2
endif

BUILD_ARGS += -C $(SRC_DIR)/out/$(BUILD_TYPE)

# Verbose?
ifeq (1,$(VERBOSE))
BUILD_ARGS += -v
endif

ifeq (1,$(WANT_OZONE))
BROWSER_GN_DEFINES += use_ozone=true ozone_auto_platforms=false ozone_platform_x11=true ozone_platform_wayland=true use_xkbcommon=true
endif

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

# 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
BROWSER_GN_DEFINES += \
	proprietary_codecs=true \
	ffmpeg_branding="Chrome" \
	$(NULL)
FFMPEG_MT_STD_GN_DEFINES    = ffmpeg_branding="Chromium"
FFMPEG_MT_EXTRA_GN_DEFINES  = ffmpeg_branding="Chrome"

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

# controlling the use of GConf (the classic GNOME configuration
# and GIO, which contains GSettings (the new GNOME config system)
GN_DEFINES += use_gconf=true use_gio=true

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

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

ifneq (,$(GOOGLEAPI_APIKEY_$(DISTRIBUTION)))
GYP_DEFINES += \
	-Dgoogle_api_key='$(GOOGLEAPI_APIKEY_$(DISTRIBUTION))' \
	-Dgoogle_default_client_id='$(GOOGLEAPI_CLIENTID_$(DISTRIBUTION))' \
	-Dgoogle_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 ($(DEB_HOST_ARCH),$(DEB_BUILD_ARCH))
$(note Cross compiling for $(DEB_HOST_ARCH), from this $(DEB_BUILD_ARCH))
CROSS_BUILD = PKG_CONFIG_PATH=/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig:/usr/$(DEB_HOST_MULTIARCH)/lib/pkgconfig CXX=$(DEB_HOST_GNU_TYPE)-$(CXX)
else
#ifneq (,$(filter 14.04%,$(UBUNTU_DIST)))
#CROSS_BUILD = CC=$(shell which clang-3.8) CXX=$(shell which clang++-3.8)
#GN_DEFINES += is_clang=true clang_use_chrome_plugins=false
#FFMPEG_MT_GN_DEFINES += is_clang=true clang_use_chrome_plugins=false
#else
CROSS_BUILD = CC=$(shell which gcc) CXX=$(shell which g++)
GN_DEFINES += is_clang=false
FFMPEG_MT_GN_DEFINES += is_clang=false
#endif
endif

### Official interfaces.

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

# 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=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:
	# Don't configure here. We configure and build many times in 'build'.

# There are three things to build. Standard, ffmpeg simple, and ffmpeg full.
#override_dh_auto_build: build-stamp-gn build-stamp build-stamp-ffmpeg-std build-stamp-ffmpeg-extra
override_dh_auto_build: build-stamp-gn build-stamp 

override_dh_fixperms:
	dh_fixperms --exclude $(DEB_FIXPERMS_EXCLUDE)

override_dh_builddeb:
	dh_builddeb -- -Zxz

override_dh_strip:
	dh_strip -Xnacl_irt_ -Xpnacl -X"Mojo Applications"

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

override_dh_install-arch: PKG_DIRS := $(addprefix debian/,$(shell dh_listpackages))
override_dh_install-arch: TRASH := .deps gen obj.host obj.target \*.lock obj build.\* .landmines mksnapshot.ia32 mksnapshot protoc pyproto re2c resources xdg-mime xdg-settings yasm \*.TOC \*.stamp product_logo_\*png gen\* libvpx_obj_int_extract libwidevinecdm.so libwidevinecdmadapter.so .ninja\* \*.ninja args.gn chrome-wrapper \*.breakpad.\* java_mojo dump_syms \*.cmd \*.debug \*.debug.raw character_data_generator flatc WidevineCdm Packages mkpeephole \*.service
override_dh_install-arch: build-stamp build-stamp-ffmpeg-std build-stamp-ffmpeg-extra
	@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) ) .;) | (cd debian/tmp/$(LIB_DIR) && tar -xvf -;)
#	rm debian/tmp/$(LIB_DIR)/libffmpeg.so
ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
#	mkdir -p debian/tmp/$(LIB_DIR)/libs
#	cp $(SRC_DIR)/out/$(BUILD_TYPE)/*.so debian/tmp/$(LIB_DIR)/libs/
#	rm debian/tmp/$(LIB_DIR)/libs/libffmpeg.so
#	mv debian/tmp/$(LIB_DIR)/libs/libclearkeycdm.so debian/tmp/$(LIB_DIR)/libs/libEGL.so debian/tmp/$(LIB_DIR)/libs/libGLESv2.so debian/tmp/$(LIB_DIR)/
endif
	
	# 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 \
	  dh_installdirs --package=$(DEBIAN_NAME) usr/share/icons/hicolor/$${size}x$${size}/apps ; \
	  cp -a $(SRC_DIR)/chrome/app/theme/chromium/product_logo_$$size.png  \
	    debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/$${size}x$${size}/apps/$(DEBIAN_NAME).png ; \
	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 -;)
#	# prev missed "libs"
#	(cd debian/tmp/$(LIB_DIR)/libs; tar cf - .;) | (cd debian/iridium-browser/$(LIB_DIR)/libs/; tar xvf -;)
	
ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
#	# Set the directory for looking up component libraries.
#	find debian/*/$(LIB_DIR)/libs             -type f -name \*.so             -execdir chrpath --replace "\$$ORIGIN" {} \;
#	find debian/*/$(LIB_DIR)      -maxdepth 1 -type f -name \*.so             -execdir chrpath --replace "\$$ORIGIN/libs" {} \;
#	find debian/*/$(LIB_DIR)      -maxdepth 1 -type f             -executable -execdir chrpath --replace "\$$ORIGIN/libs" {} \;
endif
	
	# Launcher script
	cp -a debian/$(DEBIAN_NAME).sh debian/$(DEBIAN_NAME)/usr/bin/$(DEBIAN_NAME)
	chmod 755 debian/$(DEBIAN_NAME)/usr/bin/$(DEBIAN_NAME)
	
	# Preferences
	cp -a debian/$(DEBIAN_NAME).default debian/$(DEBIAN_NAME)/etc/$(DEBIAN_NAME)/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/$(DEBIAN_NAME)/$(LIB_DIR)/chrome debian/$(DEBIAN_NAME)/$(LIB_DIR)/$(DEBIAN_NAME)
	
	# "you have to change the underscore from the build target into a hyphen".
	# https://code.google.com/p/chromium/wiki/LinuxSUIDSandbox
	mv debian/$(DEBIAN_NAME)/$(LIB_DIR)/chrome_sandbox debian/$(DEBIAN_NAME)/$(LIB_DIR)/chrome-sandbox
	chmod 4755 debian/$(DEBIAN_NAME)/$(LIB_DIR)/chrome-sandbox
	
	# Manpage is for wrong program name, and it's in a weird place.  Use and destroy.
	mkdir -p debian/iridium-browser/usr/share/man/man1
	mv debian/iridium-browser/$(LIB_DIR)/chrome.1 debian/iridium-browser/usr/share/man/man1/$(DEBIAN_NAME).1
	gzip -9 debian/iridium-browser/usr/share/man/man1/iridium-browser.1
	
	# Locales: only keep en-US in the main deb
	# Discard everything except en-US
	find debian/iridium-browser/$(LIB_DIR)/locales -type f \! -name en-US.pak -print -delete
	
	dh_install -a

override_dh_install-indep: build-stamp
	@set -eux
	dh_install -i
	install --directory debian/tmp/etc/iridium-browser/customizations
	install --owner=root --mode=0644 --no-target-directory debian/iridium-browser-customization-example debian/tmp/etc/iridium-browser/customizations/00-example
	install --owner=root --mode=0644 --no-target-directory debian/iridium-browser-customization-flash-staleness debian/iridium-browser/etc/iridium-browser/customizations/09-flash-staleness-check
	find debian/iridium-browser-l10n/$(LIB_DIR)/locales -name en-US.pak -print -delete

override_dh_clean:
	@set -eux
	rm -rf debian/*-stamp debian/stamp-* debian/tmp-*
	rm -f build-stamp build-stamp-ffmpeg-std build-stamp-ffmpeg-extra \
		configure-stamp-ffmpeg-std configure-stamp-ffmpeg-extra \
		configure-stamp-gn build-stamp-gn
	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. This section
# disables the default behavior, falls back to gyp, builds gn, and use that as the binary.
configure-stamp-gn:
	if [ -d chromium-$(DEBIAN_UPSTREAM_VERSION) ]; then \
		mv chromium-$(DEBIAN_UPSTREAM_VERSION)/* . ; \
		rm -r chromium-$(DEBIAN_UPSTREAM_VERSION); \
	fi
	
	touch $@

build-stamp-gn: configure-stamp-gn
	@set -eux
	
	$(CROSS_BUILD) tools/gn/bootstrap/bootstrap.py -v --gn-gen-args '$(GN_DEFINES)'
	cp $(SRC_DIR)/out/Release/gn $(SRC_DIR)
	
	rm -rf $(SRC_DIR)/out
	
	touch $@

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' \
	    $< > $@

configure: configure-stamp
configure-stamp: debian/iridium-browser.sh build-stamp-gn
	@set -eux
	if [ -d chromium-$(DEBIAN_UPSTREAM_VERSION) ]; then \
		mv chromium-$(DEBIAN_UPSTREAM_VERSION)/* . ; \
		rm -r chromium-$(DEBIAN_UPSTREAM_VERSION); \
	fi
	
	# Add symlink to Native Client toolchain
	ln -s /usr/lib/$(DEB_HOST_MULTIARCH)/native-client/ native_client/toolchain
	
	$(SRC_DIR)/gn gen $(SRC_DIR)/out/$(BUILD_TYPE) --args='$(GN_DEFINES) $(BROWSER_GN_DEFINES)'
	
	touch $@

build-stamp: configure-stamp
	@set -eux
	$(NINJA) $(BUILD_ARGS) $(BUILD_TARGETS)
	touch $@

configure-stamp-ffmpeg-%: GYP_DEFINES += $(FFMPEG_MT_$(subst std,STD,$(subst extra,EXTRA,$*))_GYP_DEFINES) $(FFMPEG_MT_GYP_DEFINES)
configure-stamp-ffmpeg-%: build-stamp-gn
	@set -eux
	$(SRC_DIR)/gn gen $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-$* --args='$(FFMPEG_MT_$(subst std,STD,$(subst extra,EXTRA,$*))_GN_DEFINES) $(FFMPEG_MT_GN_DEFINES)'
	touch $@

build-stamp-ffmpeg-%: configure-stamp-ffmpeg-%
	@set -eux
	$(NINJA) -v -C $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-$* third_party/ffmpeg
	mkdir -p debian/tmp-$*/$(LIB_DIR)
	#cp $(SRC_DIR)/out/$(BUILD_TYPE)-ffmpeg-$*/libffmpeg.so $(CURDIR)/debian/tmp-$*/$(LIB_DIR)
	touch $@

# 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/AHEM____.TTF$$
#BUILT_UNUSED_MATCH += ^usr/lib/chromium-browser/fonts.conf$$
#BUILT_UNUSED_MATCH += ^usr/lib/chromium-browser/libTestNetscapePlugIn.so$$
#BUILT_UNUSED_MATCH += ^usr/lib/chromium-browser/plugins/libTestNetscapePlugIn.so$$
#BUILT_UNUSED_MATCH += ^usr/lib/chromium-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/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: install get-packaged-orig-source gos override_dh_* local_install_* patch-translations compare_*
