#!/usr/bin/make -f

CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS | sed s/-O2/-O3/) -pthread

%:
	dh $@ --parallel

override_dh_auto_build:
	rm -f util/valgrind.h
	if [ -e /usr/include/valgrind/valgrind.h ]; then \
		ln -s /usr/include/valgrind/valgrind.h util; \
	else \
		touch util/valgrind.h; \
	fi
	dh_auto_build -- CXXFLAGS="$(CXXFLAGS)" REBUILD_TABLES=1

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp/ -- prefix=/usr

override_dh_clean:
	dh_clean
	rm -f util/valgrind.h

REV=$(shell dpkg-parsechangelog | sed -rne 's,^Version: .*hg([0-9]+).*,\1,p')
VER=$(shell dpkg-parsechangelog | sed -rne 's,^Version: (.+)\+dfsg-.*,\1,p')

get-orig-source:
ifeq ($(REV),)
	uscan --noconf --force-download --rename --download-version=$(VER) --destdir=.
	tar -xf re2_$(VER).orig.tar.gz
	rm re2_$(VER).orig.tar.gz
else
	hg clone https://re2.googlecode.com/hg re2-$(VER)
	cd re2-$(VER) && hg up -r $(REV)
	rm -r re2-$(VER)/.hg re2-$(VER)/.hgignore
endif
	# Licensing issues
	rm -r re2-$(VER)/doc/*xkcd* re2-$(VER)/lib/codereview
	# Automatically generated code
	cd re2-$(VER)/re2 && rm perl_groups.cc unicode_casefold.cc unicode_groups.cc
	GZIP=--best tar -cz --owner root --group root --mode a+rX \
	                -f re2_$(VER)+dfsg.orig.tar.gz re2-$(VER)
	rm -r re2-$(VER)
