#!/usr/bin/make -f

# enable verbose build messages
export DH_VERBOSE=1

# compiler settings
export CC=gcc-4.9
export CXX=g++-4.9

# package data fields
package=$(shell dpkg-parsechangelog --show-field=source)
version=$(shell dpkg-parsechangelog --show-field=version | sed s/-.*//)

# upstream tarball info
url=https://gsdview.appspot.com/chromium-browser-official
tarball=chromium-$(version)-lite.tar.xz

# treat all warnings as errors
defines=werror=

# disabled features
defines+=use_ash=0 \
         use_ozone=0 \
	 use_allocator=none \
         use_gnome_keyring=0 \
         linux_use_libgps=0 \
	 linuk_use_tcmalloc=0 \
         linux_use_gold_flags=0 \
         linux_use_gold_binary=0 \
         remoting=0 \
         disable_nacl=1 \
         enable_webrtc=0 \
         enable_remoting_host=0 \

# enabled features
defines+=use_pulseaudio=1 \
         linux_link_gsettings=1 \
         linux_link_libspeechd=1 \
	 icu_use_data_file_flag=1 \

# system libraries to use
defines+=use_system_icu=1 \
         use_system_yasm=1 \
         use_system_flac=1 \
         use_system_opus=1 \
         use_system_speex=1 \
         use_system_expat=1 \
         use_system_snappy=1 \
         use_system_libpng=1 \
         use_system_libxml=1 \
         use_system_libjpeg=1 \
         use_system_libwebp=1 \
         use_system_libxslt=1 \
         use_system_jsoncpp=1 \
         use_system_libevent=1 \
         use_system_protobuf=1 \
         use_system_xdg_utils=1 \

# embedded libraries (0 is the default, so using comments to make some notes about the issues)
#	use_system_zlib=0 requires a libminizip1 package (rfp #709803)
#	use_system_libusb=0 requires modified source that exposes libusb_interrupt_handle_event api
#	use_system_libvpx=0 requires modified source that exposes different api
#	use_system_sqlite=0 build fails
#	use_system_ffmpeg=0 build fails

# make gyp a little more informative
options+=--check \
         --debug=includes \

# avoid the need for a special src directory (--depth=. seems to require --no-parallel)
options+=--depth=. \
         --no-parallel \

# handle parallel build options
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
njobs=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

%:
	dh $@ --parallel

override_dh_auto_configure:
	cp v8/third_party/icu/icu.isolate third_party/icu
	GYP_DEFINES="$(defines)" ./build/gyp_chromium $(options)
	
override_dh_auto_build:
	ninja -v -j $(njobs) -C out/Release chrome chrome_sandbox chromedriver 
	cp out/Release/chrome.1 out/Release/chromium.1
	mv out/Release/locales/en-US.pak out/Release/resources || true
	mv out/Release/chrome_sandbox out/Release/chrome-sandbox || true
	chmod 4755 out/Release/chrome-sandbox # suid sandbox

override_dh_fixperms:
	dh_fixperms --exclude chrome-sandbox

override_dh_strip:
	dh_strip --dbg-package=chromium-dbg

override_dh_clean:
	dh_clean
	./debian/scripts/build-removals

get-orig-source: 
	# fetch the upstream source
	cd .. && wget --progress=bar:force -c $(url)/$(tarball) 2>&1 | tail -f -n +13
	# fetch its hashes
	cd .. && wget --progress=bar:force -c $(url)/$(tarball).hashes 2>&1 | tail -f -n +13
	# extract the hashes 
	grep sha256 ../$(tarball).hashes | cut -d\  -f5,3 > ../$(tarball).sha256sum
	grep sha512 ../$(tarball).hashes | cut -d\  -f5,3 > ../$(tarball).sha512sum
	# verify hashes
	cd .. && sha256sum -c $(tarball).sha256sum
	cd .. && sha512sum -c $(tarball).sha512sum
	# extract the source
	cd .. && tar xf $(tarball) --overwrite
	# generate old file list
	cd ../chromium-$(version) && find . | sort > ../chromium-$(version).old
	# remove unwanted third_party bundles
	cd ../chromium-$(version)/third_party && for setting in $(defines); do \
	    dir=$$(echo $$setting | grep use_system_.*=1 | sed 's/use_system_\(.*\)=1/\1/' | tr _ -); \
	    test -z $$dir || find $$dir -mindepth 1 -maxdepth 1 ! -name \*.gyp\* -and ! -name chromium -execdir rm -rf {} \; 2>/dev/null; \
	    test -z $$dir || cp ../build/linux/unbundle/$$dir.gyp $$dir 2>/dev/null || true; \
	    test -z $$dir || rmdir $$dir --ignore-fail-on-non-empty; \
	    done
	# remove unwanted files from the upstream source
	cd ../chromium-$(version) && $(PWD)/debian/scripts/upstream-removals
	# remove files that are removed when cleaning
	cd ../chromium-$(version) && $(PWD)/debian/scripts/build-removals
	# generate new file list
	cd ../chromium-$(version) && find . | sort > ../chromium-$(version).new
	# record file removals list
	cd .. && diff -u chromium-$(version).old chromium-$(version).new \
	    > chromium-$(version)/UPSTREAM-REMOVALS.txt || true
	# create orig tarball (use -0 for fast compression/decompression)
	rm -rf ../$(package)_$(version).orig.tar*
	cd .. && tar cf $(package)_$(version).orig.tar chromium-$(version)
	cd .. && xz -0 $(package)_$(version).orig.tar
	@echo "successfully created ../$(package)_$(version).orig.tar.xz"
