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

# DEB_TAR_SRCDIR has to be 'src' as this is what gyp expects :(
DEB_TAR_SRCDIR := src
SRC_DIR        := $(CURDIR)/$(DEB_TAR_SRCDIR)
LIB_DIR        := usr/lib/$(DEBIAN_NAME)
DEB_BUILDDIR   := $(SRC_DIR)
CDBS_NO_DOC_SYMLINKING := 1 # See LP #194574
KEEPALIVE      := $(CURDIR)/debian/keep-alive.sh

# 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)
 ifneq (,$(findstring 10.04,$(DEBIAN_DIST)))
BINARY_PACKAGE_COMPRESSION ?= bzip2# bzip2, gzip, lzma, xz, none
 else
BINARY_PACKAGE_COMPRESSION ?= xz# bzip2, gzip, lzma, xz, none
 endif
else
BINARY_PACKAGE_COMPRESSION ?= xz# bzip2, gzip, lzma, xz, none
endif

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

WANT_DEBUG       := 0
WANT_SYSTEM_LIBS ?= 0
WANT_SHARED_LIBS ?= 0
PROCESSORS       ?= $(shell grep -ic ^processor /proc/cpuinfo)
ifeq (0,$(PROCESSORS))
PROCESSORS := 1
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 := AIzaSyAQfxPJiounkhOjODEO5ZieffeBv6yft2Q
GOOGLEAPI_CLIENTID_UBUNTU := 424119844901.apps.googleusercontent.com
GOOGLEAPI_CLIENTSECRET_UBUNTU := AIienwDlGIIsHoKnNHmWGXyJ

# TODO: Remove this block? These filters don't do what the author expected.
# DEBIAN_DIST_VERSION has a dot-something on the end, often, and the pattern
# nn.nn doesn't match that.  -cm
#
# Enable the hardening wrapper (See https://wiki.ubuntu.com/Security/HardeningWrapper)
# Don't do it on {hardy,jaunty}+amd64 as it doesn't build for nacl on amd64
# Don't do it on maverick/armel either for now, it fails to link
# Don't do it on lucid/armel either, it crashes: LP #716703
ifeq (armel,$(DEB_HOST_ARCH))
 ifeq (,$(filter 10.04 10.10 11.04,$(DEBIAN_DIST_VERSION)))
export DEB_BUILD_HARDENING=1
 else
$(warning ## Not enabling DEB_BUILD_HARDENING on Ubuntu 10.04+ armel. See LP #641126 and LP #716703)
 endif
else
 ifeq (,$(filter 8.04 9.04,$(DEBIAN_DIST_VERSION)))
export DEB_BUILD_HARDENING=1
 else
  ifneq (amd64,$(DEB_HOST_ARCH))
export DEB_BUILD_HARDENING=1
  else
$(warning ## Not enabling DEB_BUILD_HARDENING on Ubuntu 8.04/9.04 amd64)
  endif
 endif
endif

GYP_PARAMS = 

# Disable SSE2
GYP_DEFINES = disable_sse2=1

# Tell scripts that are build where programs will be once installed
GYP_DEFINES += \
	linux_sandbox_path=/$(LIB_DIR)/$(DEBIAN_NAME)-sandbox \
	linux_sandbox_chrome_path=/$(LIB_DIR)/$(DEBIAN_NAME) \
	$(NULL)

# Build the launchpad translations (already landed upstream)
GYP_DEFINES += use_third_party_translations=1
# Also merges the newer translations from launchpad
WANT_LP_TRANSLATIONS_IN_BUILD ?= 0

## Policy: 2012-10-30, cm,kv,cc, disabling merging of LP translations. "0"
MERGE_LP_TRANSLATIONS_IN_TARBALL = 0
## Rationale: Instead of merging other stuff before "orig" (!) tarball
## creation, let's export translation templates to Launchpad, and send them
## upstream to be included in the real orig source.  It's slower for circuit,
## but all users win, and is less buggy for us.
##

# Always add debug symbols, and strip them when we don't want them.
#GYP_DEFINES += release_extra_cflags="-g"

# Don't fail on compilation warnings.
GYP_DEFINES += werror=$(NULL)

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

# Disable NaCl until we figure out what to do with the private toolchain
GYP_DEFINES += disable_nacl=1

# do not use third_party/gold as the linker.
GYP_DEFINES += linux_use_gold_binary=0 linux_use_gold_flags=0

COMPONENT_SHARED_LIB_BUILD := 0


# Intentional configuration, not bug work-arounds.
ifeq (arm,$(DEB_HOST_ARCH_CPU))
GYP_DEFINES += enable_webrtc=1

GYP_DEFINES += \
	target_arch=arm \
	use_cups=1 \
	$(NULL)
 ifeq (armel,$(DEB_HOST_ARCH))
GYP_DEFINES += \
	v8_use_arm_eabi_hardfloat=false \
	arm_float_abi=soft \
	arm_thumb=0 \
	armv7=0 \
	$(NULL)
 endif
 ifeq (armhf,$(DEB_HOST_ARCH))
GYP_PARAMS += -DUSE_EABI_HARDFLOAT 
GYP_DEFINES += arm_neon_optional=1
GYP_DEFINES += \
	v8_use_arm_eabi_hardfloat=true \
	arm_fpu=neon \
	arm_float_abi=hard \
	arm_thumb=1 \
	$(NULL)
  ifneq (,$(filter 10.10% 11.04% 11.10% 12.04% 12.10% 13.04% 13.10%,$(UBUNTU_DIST))$(filter unstable development,$(DEBIAN_DIST)))
GYP_DEFINES += \
	armv7=1 \
	$(NULL)
  endif
 endif
endif
ifeq (amd64,$(DEB_HOST_ARCH))
GYP_DEFINES += target_arch=x64
endif
ifeq (i386,$(DEB_HOST_ARCH))
GYP_DEFINES += target_arch=ia32
endif

ifeq (32,$(DEB_BUILD_ARCH_BITS))
$(warning Since this machine's word size is too small for linking full binary now, we're dynamically linking at run time instead, at a cost of some startup time.)
COMPONENT_SHARED_LIB_BUILD := 1
endif

ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
GYP_DEFINES += component=shared_library

# SEGV on component builds, 2013-05
GYP_DEFINES += linux_use_tcmalloc=0
endif

# Webkit library is enormous. Exclude for now.
GYP_DEFINES += remove_webcore_debug_symbols=1

# 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

DEB_DH_BUILDDEB_ARGS += -- -Z $(BINARY_PACKAGE_COMPRESSION)

ifeq (1,$(WANT_DEBUG))
BUILD_TYPE := Debug
else
BUILD_TYPE := Release
# Add symbols
GYP_DEFINES += linux_dump_symbols=1
endif
BUILD_ARGS += BUILDTYPE=$(BUILD_TYPE)
BUILD_ARGS += V=1

# Shared libraries
ifeq (1,$(WANT_SHARED_LIBS))
GYP_DEFINES += library=shared_library
endif

# There is no "clean" target in the upstream makefile
DEB_MAKE_CLEAN_TARGET=

#pre-patches::
	#-mkdir src
	#cd src; tar xf ../orig.tar.* --strip-components=1

INTERNAL_TAR_PARAMS := --transform=,^[^/]*/,src/,

# There is no "clean" target in the upstream makefile
DEB_MAKE_CLEAN_TARGET=

post-patches::
	sed -i 's^\("/usr/lib/nss"\)^/*REM \1 REM*/"/usr/lib/$(DEB_HOST_MULTIARCH)/nss"/**/^' src/crypto/nss_util.cc
	perl $(CURDIR)/debian/enable-dist-patches.pl $(DEBIAN_DIST_VERSION) $(CURDIR)/debian/patches/series

include $(CURDIR)/debian/cdbs/tarball.mk
-include /usr/share/cdbs/1/rules/patchsys-quilt.mk
-include /usr/share/cdbs/1/rules/debhelper.mk
-include /usr/share/cdbs/1/class/makefile.mk

ifneq (1,$(PROCESSORS))
BUILD_ARGS += -j$(PROCESSORS)
endif

INSTALL_EXCLUDE_DIRS = \
	obj \
	obj.target \
	obj.host \
	.deps \
	calendar_app \
	docs_app \
	gmail_app \
	pyproto \
	pseudo_locales \
	DumpRenderTree_resources \
	lib.target \
	$(NULL)

INSTALL_EXCLUDE_FILES = \
	chrome-wrapper \
	product_logo_48.png \
	libnpapi_layout_test_plugin.so \
	libnpapi_test_plugin.so \
	mksnapshot \
	ncdecode_table \
	ncdecode_tablegen \
	ssl_false_start_blacklist_process \
	protoc \
	gfx_unittests \
	linker.lock \
	genmacro \
	genmodule \
	genperf \
	genstring \
	genversion \
	re2c \
	yasm \
	libvpx_obj_int_extract \
	xdisplaycheck \
	libTestRunner.so \
	$(NULL)

# FFmpeg-mt
# Don't build it 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
DEB_DH_SHLIBDEPS_ARGS_$(DEBIAN_CODECS_NAME) := -l$(CURDIR)/debian/$(DEBIAN_CODECS_NAME)/$(LIB_DIR)
DEB_DH_SHLIBDEPS_ARGS_$(DEBIAN_CODECS_NAME)-extra := -l$(CURDIR)/debian/$(DEBIAN_CODECS_NAME)-extra/$(LIB_DIR)
BROWSER_GYP_DEFINES += \
	proprietary_codecs=1 \
	build_ffmpegsumo=0 \
	$(NULL)
FFMPEG_MT_GYP_DEFINES = \
	use_system_vpx=0 \
	release_extra_cflags=-g \
	$(NULL)
FFMPEG_MT_STD_GYP_DEFINES   = $(NULL)
FFMPEG_MT_EXTRA_GYP_DEFINES = ffmpeg_branding=Chrome

# Precise and earlier don't set some settings.
ifeq (,$(filter 12.04% 11.10% 11.04%,$(DEBIAN_DIST)))
# enable compile-time dependency on gnome-keyring
GYP_DEFINES += use_gnome_keyring=1 linux_link_gnome_keyring=1
# controlling the use of GConf (the classic GNOME configuration
# and GIO, which contains GSettings (the new GNOME config system)
GYP_DEFINES += use_gconf=1 use_gio=1
endif

# System libs
ifeq (1,$(WANT_SYSTEM_LIBS))
# Causes render hangs, 2013-05 raring
#GYP_DEFINES += use_system_libpng=1
# Causes link failures. "chromium_" prefixes.  2013-05 raring
#GYP_DEFINES += use_system_sqlite=$(USE_SYSTEM_SQLITE)

# Good in raring, saucy
GYP_DEFINES += use_system_libxml=1
GYP_DEFINES += use_system_libjpeg=1
GYP_DEFINES += use_system_bzip2=1 
# v9 is good, but not available as package.
#GYP_DEFINES += use_system_v8=1

# Arcot's suggestions.  raring, saucy
GYP_DEFINES += use_system_flac=1
GYP_DEFINES += use_system_libevent=1
GYP_DEFINES += use_system_protobuf=1
GYP_DEFINES += use_system_speex=1

GYP_DEFINES += use_system_xdg_utils=1
GYP_DEFINES += use_system_yasm=1
endif

# Build everything (like All), including test programs but minus things we don't
# want (breakpad, courgette, ..) or don't need (the other small tools)
BUILD_TARGETS := \
	chrome \
	chrome_sandbox \
	chromedriver \
	$(NULL)

GYP_DEFINES += \
	linux_sandbox_path=/$(LIB_DIR)/$(DEBIAN_NAME)-sandbox \
	linux_sandbox_chrome_path=/$(LIB_DIR)/$(DEBIAN_NAME) \
	$(NULL)

# needed to preserve the suid and make the sandbox happy
DEB_FIXPERMS_EXCLUDE := $(DEBIAN_NAME)-sandbox

# use system v8
ifeq (1,$(USE_SYSTEM_V8))
GYP_DEFINES += \
	javascript_engine=system-v8 \
	$(NULL)
endif

ifneq (,$(GOOGLEAPI_APIKEY_$(DISTRIBUTION)))
GYP_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 (,$(GOOGLEAPI_APIKEY_$(DISTRIBUTION)))
GYP_PARAMS += \
	-Dgoogle_api_key='$(GOOGLEAPI_APIKEY_$(DISTRIBUTION))' -Dgoogle_default_client_id='$(GOOGLEAPI_CLIENTID_$(DISTRIBUTION))' -Dgoogle_default_client_secret='$(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)
DEB_MAKE_ENVVARS += CXX=$(DEB_HOST_GNU_TYPE)-$(CXX)
else
CROSS_BUILD = 
endif
BUILD_ARGS += CC_host=$(CC) CXX_host=$(CXX) 

######
DO = (touch r1; echo '$(1)'; $(1); touch r2 ; T1=`date -r r1 +%s` ; T2=`date -r r2 +%s`; echo "PERF: '$(2)' built in "`expr $$T2 - $$T1`" sec"; rm -f r1 r2;)

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

%:: %.in
	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' \
	    < $< > $@

pre-build:: debian/pre-build-stamp $(subst_files)

debian/pre-build-stamp:
ifneq (,$(findstring Ubuntu,$(DEBIAN_DIST)))
 ifeq (1,$(WANT_LP_TRANSLATIONS_IN_BUILD))
	# Apply the translations patches generated by get-orig-sources
	@(set -e ; cd ./src ; if [ -d translations-patches ] ; then \
	for patch in translations-patches/*.patch ; do \
	  echo "# Applying $$patch..." ; \
	  patch -p 1 < $$patch ; done ; fi )
 endif
	sed -ri '/const PrepopulatedEngine google = \{/,/\}\;/ { s/sourceid=chrome\&/&client=ubuntu\&channel=cs\&/g; }' \
		src/chrome/browser/search_engines/template_url_prepopulate_data.cc
endif
	sed -i 's,/etc/chromium/policies,/etc/$(DEBIAN_NAME)/policies,' \
		src/chrome/common/chrome_paths.cc
	touch $@

DEB_MAKE_EXTRA_ARGS   := $(NULL)
DEB_MAKE_BUILD_TARGET := $(BUILD_ARGS) $(BUILD_TARGETS) || ( $(KEEPALIVE) stop ; exit 1 )

configure/$(DEBIAN_NAME):: debian/configure-stamp

debian/configure-stamp: GYP_ENV = GYP_GENERATORS=make
ifneq (,$(GYP_DEFINES))
debian/configure-stamp: GYP_ENV += GYP_DEFINES="$(GYP_DEFINES) $(BROWSER_GYP_DEFINES)"
endif
debian/configure-stamp:
	cd $(SRC_DIR) && $(GYP_ENV) $(CROSS_BUILD) python build/gyp_chromium build/all.gyp $(GYP_PARAMS)
	touch $@
	# Populate the LASTCHANGE file as we no longer have the VCS
	# files at this point
	echo "LASTCHANGE=$(DEBIAN_REVISION)" > $(SRC_DIR)/build/util/LASTCHANGE
	# BUILD_ARGS=$(BUILD_ARGS)
	# Start the keep-alive script
	@chmod 755 $(KEEPALIVE)
	$(KEEPALIVE) start &

common-build-arch common-build-indep:: debian/stamp-makefile-build-perf
debian/stamp-makefile-build-perf: debian/stamp-makefile-build
	@T2=`date +%s`; T1=`date -r debian/configure-stamp +%s`; echo "PERF: '$(BUILD_TARGETS)' built in "`expr $$T2 - $$T1`" sec"
	touch $@
	$(KEEPALIVE) stop

common-build-arch:: debian/stamp-build-ffmpeg-std debian/stamp-build-ffmpeg-extra

debian/stamp-configure-ffmpeg-%:
	rm -rf $(FFMPEG_SRC_DIR)/*.mk $(FFMPEG_SRC_DIR)/out
	cd $(SRC_DIR) && GYP_DEFINES="$(GYP_DEFINES)" $(CROSS_BUILD) GYP_GENERATORS=make build/gyp_chromium --depth=../src $(FFMPEG_DIR)/ffmpeg.gyp $(GYP_PARAMS)
	touch $@

debian/stamp-build-ffmpeg-%: GYP_DEFINES += $(FFMPEG_MT_$(subst std,STD,$(subst extra,EXTRA,$*))_GYP_DEFINES) $(FFMPEG_MT_GYP_DEFINES)
debian/stamp-build-ffmpeg-%: FFMPEG_DIR = third_party/ffmpeg
debian/stamp-build-ffmpeg-%: FFMPEG_SRC_DIR = $(SRC_DIR)/$(FFMPEG_DIR)
debian/stamp-build-ffmpeg-%: debian/stamp-configure-ffmpeg-%
	cd $(FFMPEG_SRC_DIR) ; $(MAKE) -f ffmpeg.Makefile $(BUILD_ARGS) $(CROSS_BUILD)
	install -d -m 755 $(CURDIR)/debian/tmp-$*/$(LIB_DIR)
	install -m 644 $(FFMPEG_SRC_DIR)/out/$(BUILD_TYPE)/libffmpegsumo.so $(CURDIR)/debian/tmp-$*/$(LIB_DIR)
	touch $@

clean::
	rm -rf debian/*-stamp debian/stamp-* debian/tmp-*
	rm -f $(subst_files) *.cdbs-config_list
	sed -i 's^/\*REM \(.*\) REM\*/.*/\*\*/^\1^' src/crypto/nss_util.cc
	perl $(CURDIR)/debian/enable-dist-patches.pl --clean $(CURDIR)/debian/patches/series
	find $(CURDIR) \( -name \*.pyc -o -name \*.pyo \) -delete
	test ! -d $(CURDIR)/src/out || rm -r $(CURDIR)/src/out

# Install: there's no install rules in scons yet, do it manually
common-install-prehook-impl::
	mkdir -p debian/tmp/$(LIB_DIR)
	( cd $(SRC_DIR)/out/$(BUILD_TYPE) && tar $(foreach excl,$(INSTALL_EXCLUDE_DIRS),--exclude=$(excl)) -cf - . ) | ( cd debian/tmp/$(LIB_DIR) && tar xf - )
ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
	mkdir -p debian/tmp/$(LIB_DIR)/libs
	( cd $(SRC_DIR)/out/$(BUILD_TYPE)/lib.target && tar cf - . ) | ( cd debian/tmp/$(LIB_DIR)/libs && tar xf - )
	$(warning Stripping symbols from enormous libraries.)
	-find debian/tmp/$(LIB_DIR)/libs -type f -size +400M -name \*.so -exec ls -l {} \; -exec strip {} \; -exec ls -l {} \;
endif
	cd debian/tmp/$(LIB_DIR) && rm -f $(INSTALL_EXCLUDE_FILES)
	# Launcher script
	mkdir -p debian/tmp/usr/bin
	cp -a debian/$(DEBIAN_NAME).sh debian/tmp/usr/bin/$(DEBIAN_NAME)
	chmod 755 debian/tmp/usr/bin/$(DEBIAN_NAME)
	# Preferences
	mkdir -p debian/tmp/etc/$(DEBIAN_NAME)
	cp -a debian/$(DEBIAN_NAME).default debian/tmp/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/tmp/$(LIB_DIR)/chrome debian/tmp/$(LIB_DIR)/$(DEBIAN_NAME)
	mv debian/tmp/$(LIB_DIR)/chrome_sandbox debian/tmp/$(LIB_DIR)/$(DEBIAN_NAME)-sandbox
	# Manpage
	mv debian/tmp/$(LIB_DIR)/chrome.1 debian/tmp/$(LIB_DIR)/chromium-browser.1
	dh_installman -pchromium-browser debian/tmp/$(LIB_DIR)/chromium-browser.1
	rm -f debian/tmp/$(LIB_DIR)/chromium-browser.1
	# chromedriver
	mkdir -p debian/$(DEBIAN_DRIVER_NAME)/$(LIB_DIR)
	cp -a debian/tmp/$(LIB_DIR)/chromedriver $(CURDIR)/debian/$(DEBIAN_DRIVER_NAME)/$(LIB_DIR)

common-install-prehook-impl:: LIBS=$(wildcard debian/tmp/$(LIB_DIR)/libs/*.so)
common-install-prehook-impl::
ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
	# Set the directory for looking up component libraries.
	for f in debian/tmp/$(LIB_DIR)/$(DEBIAN_NAME) $(LIBS); do chrpath --replace "\$$ORIGIN/libs" "$$f"; done
endif

binary-install/$(DEBIAN_NAME)::
ifeq (1,$(COMPONENT_SHARED_LIB_BUILD))
	dh_install -p$(DEBIAN_NAME) debian/tmp/$(LIB_DIR)/libs $(LIB_DIR)
endif
	mkdir -p $(CURDIR)/debian/$(DEBIAN_NAME)/usr/share/pixmaps
	cp -a src/chrome/app/theme/chromium/product_logo_48.png $(CURDIR)/debian/$(DEBIAN_NAME)/usr/share/pixmaps/$(DEBIAN_NAME).png
	for size in 22 24 32 48 64 128 256 ; do \
	  mkdir -p $(CURDIR)/debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/$${size}x$${size}/apps ; \
	  cp -a src/chrome/app/theme/chromium/product_logo_$$size.png  \
		$(CURDIR)/debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/$${size}x$${size}/apps/$(DEBIAN_NAME).png ; \
	done
	mkdir -p $(CURDIR)/debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/scalable/apps
	cp -a $(CURDIR)/debian/$(DEBIAN_NAME).svg $(CURDIR)/debian/$(DEBIAN_NAME)/usr/share/icons/hicolor/scalable/apps
	chmod 4755 $(CURDIR)/debian/$(DEBIAN_NAME)/$(LIB_DIR)/$(DEBIAN_NAME)-sandbox
	# NaCL may be blacklisted, so only include it when it's been built
ifeq (,$(filter disable_nacl=1,$(GYP_DEFINES)))
	cp -a debian/tmp/$(LIB_DIR)/libppGoogleNaClPluginChrome.so debian/$(DEBIAN_NAME)/$(LIB_DIR)/
	cp -a debian/tmp/$(LIB_DIR)/nacl_irt_* debian/$(DEBIAN_NAME)/$(LIB_DIR)/
endif
	# Locales: only keep en-US in the main deb
	cd $(CURDIR)/debian/$(DEBIAN_NAME)/$(LIB_DIR)/locales && \
	  rm -f $(filter-out en-US.pak,$(notdir $(wildcard $(CURDIR)/debian/$(DEBIAN_NAME)/$(LIB_DIR)/locales/*)))
	# DEB_ARCH_PACKAGES=$(DEB_ARCH_PACKAGES)
	# DEB_INDEP_PACKAGES=$(DEB_INDEP_PACKAGES)
ifneq (i386,$(DEB_HOST_ARCH_CPU))
	# DEB_BUILD_ARCH_CPU=$(DEB_BUILD_ARCH_CPU)
	# We assume we're *not* doing the "Architecture: all" packages so we need
	# to drop the -l10n files from debian/tmp to make 'compare' happy
	cd $(CURDIR)/debian/tmp/$(LIB_DIR)/locales && \
	  rm $(filter-out en-US.pak,$(notdir $(wildcard $(CURDIR)/debian/tmp/$(LIB_DIR)/locales/*)))
endif

binary-install/$(DEBIAN_NAME)-l10n::
	rm $(CURDIR)/debian/$(DEBIAN_NAME)-l10n/$(LIB_DIR)/locales/en-US.pak

# Compare
PKG_DIRS = $(addprefix debian/,$(shell grep ^Package debian/control | cut -d' ' -f2))

binary-predeb/$(DEBIAN_NAME):: compare
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/$(DEBIAN_NAME)/usr/share/doc/$(DEBIAN_NAME)/$$doc ; \
	  for file in `find $(addsuffix /usr/share/doc,$(filter-out debian/$(DEBIAN_NAME),$(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 '$(DEBIAN_NAME)'" ; \
	      ( cd `dirname $$file` ; ln -s ../$(DEBIAN_NAME)/$$doc ) ; \
	    fi ; \
	  done ; \
	done
endif

compare:
	# Look for duplicates, fail if we find any
	@DUPES=`find $(PKG_DIRS) -type f -print | grep -v /DEBIAN/ | cut -d/ -f3- | sort | uniq -c | grep -vE '^ *2 .*/libffmpegsumo.so$$' | grep -vE '^  *1 '` ; \
	if [ "Z$$DUPES" != Z ] ; then \
	  echo " => Found duplicates:\n $$DUPES" ; \
	  exit 1 ; \
	else \
	  echo " => No duplicate found" ; \
	fi
	# Find missing
	@find $(PKG_DIRS) -type f -print | grep -v /DEBIAN/ | \
	  grep $(LIB_DIR) | cut -d/ -f5- | sort | uniq > /tmp/pkg-$$$$.indebs ; \
	find debian/tmp/$(LIB_DIR) -type f -print | cut -d/ -f5- | sort | uniq > /tmp/pkg-$$$$.frombuild ; \
	diff -u /tmp/pkg-$$$$.frombuild /tmp/pkg-$$$$.indebs ; \
	if [ $$? -eq 0 ] ; then \
	  echo " => All fine" ; \
	else \
	  echo " => Found differences, please investigate" ; \
	fi ; \
	rm -f /tmp/pkg-$$$$.frombuild /tmp/pkg-$$$$.indebs

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

TRANSLATIONS_TOOLS_BRANCH  := lp:~chromium-team/chromium-browser/chromium-translations-tools.head
TRANSLATIONS_EXPORT_BRANCH := lp:~chromium-team/chromium-browser/chromium-translations-exports.head
GRIT_TEMPLATES := \
	chrome/app/chromium_strings.grd \
	chrome/app/generated_resources.grd \
	chrome/app/policy/policy_templates.grd \
	ui/base/strings/ui_strings.grd \
	webkit/glue/inspector_strings.grd \
	webkit/glue/webkit_strings.grd \
	$(NULL)
OTHER_GRIT_TEMPLATES := \
	ui/base/strings/app_locale_settings.grd \
	chrome/app/resources/locale_settings.grd \
	chrome/app/resources/locale_settings_linux.grd \
	$(NULL)
MAPPED_GRIT_TEMPLATES := \
	--map-template-names ui/base/strings/ui_strings.grd=ui/base/strings/app_strings.grd \
	$(NULL)

# New langs with enough coverage to land in official builds
# (leave empty to accept all new lang)
GRIT_WHITELISTED_LANGS ?= \
	ca@valencia \
	eu \
	gl \
	ug \
	$(NULL)

GRIT_CONVERTER_FLAGS := \
	--create-patches translations-patches \
	--import-gettext ../translations-export \
	--export-grit ../translations-grit \
	--build-gyp-file build/common.gypi \
	--other-grd-files $(shell echo $(OTHER_GRIT_TEMPLATES) | tr ' ' ',') \
	$(MAPPED_GRIT_TEMPLATES) \
	$(NULL)
	
ifeq (1,$(WANT_ONLY_WHITELISTED_NEW_LANGS))
GRIT_CONVERTER_FLAGS += --whitelisted-new-langs $(shell echo $(GRIT_WHITELISTED_LANGS) | tr ' ' ',')
endif

###############################################################################################
# Retrieve/fabricate orig tarball.

get-packaged-orig-source: gos

gos: URL=https://commondatastorage.googleapis.com/chromium-browser-official/chromium-$(ORIG_VERSION).tar.xz
gos: DEST_FILENAME=$(ORIG_PACKAGE)_$(ORIG_VERSION).orig.tar.xz
gos:
	wget --continue --progress=dot:mega --output-document=source.tar.xz $(URL)
	mkdir $(ORIG_PACKAGE)-$(ORIG_VERSION)
	mkdir $(ORIG_PACKAGE)-$(ORIG_VERSION)/src
	tar xf source.tar.xz --exclude-backups --exclude-vcs --directory $(ORIG_PACKAGE)-$(ORIG_VERSION)/src --strip-components=1
	tar cJf $(DEST_FILENAME) $(ORIG_PACKAGE)-$(ORIG_VERSION)
	rm -r $(ORIG_PACKAGE)-$(ORIG_VERSION)
	@echo "# Done (created $(CURDIR)/$(DEST_FILENAME))"


#ifeq (1,$(MERGE_LP_TRANSLATIONS_IN_TARBALL))
#	bzr export $(TMP_DIR)/translations-tools $(TRANSLATIONS_TOOLS_BRANCH)
#	bzr export $(TMP_DIR)/translations-export $(TRANSLATIONS_EXPORT_BRANCH)
#	( cd $(TMP_DIR)/src ; ../translations-tools/chromium2pot.py $(GRIT_CONVERTER_FLAGS) $(GRIT_TEMPLATES) )
#	# Patches created:
#	@( cd $(TMP_DIR)/src ; find translations-patches -type f | xargs --verbose -n 1 diffstat -p 1 )
#endif
