#!/usr/bin/make -f

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# reduce g++ memory usage to fix FTBFS
ifneq (,$(filter $(DEB_HOST_ARCH), mips mipsel sh4))
export DEB_CXXFLAGS_MAINT_APPEND = --param ggc-min-expand=10
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -Wall
include /usr/share/dpkg/buildflags.mk

DEB_BUILD_PARALLEL := 1
ifneq "$(filter parallel=%,$(DEB_BUILD_OPTIONS))" ""
	njobs_requested := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	# Artificially limit parallelism in order not to run out of RAM.
	mem_total := $(shell grep MemTotal /proc/meminfo | tr -s ' ' | cut -d' ' -f2)
	njobs_max := $(patsubst 0,1,$(shell echo $$(( $(mem_total) / 2000000 ))))
	DEB_PARALLEL_JOBS_BUILD := $(shell echo $$(( $(njobs_requested) > $(njobs_max) ? $(njobs_max) : $(njobs_requested) )))
	# Artificially limit parallelism in order not to run out of RAM while compiling the test suite.
	njobs_max_check := $(patsubst 0,1,$(shell echo $$(( $(mem_total) / 2000000 ))))
	DEB_PARALLEL_JOBS_CHECK := $(shell echo $$(( $(njobs_requested) > $(njobs_max_check) ? $(njobs_max_check) : $(njobs_requested) )))
	# Disable CDBS handling of parallel building.
	DEB_MAKE_PARALLEL :=
	# Manually handle parallelism.
	DEB_MAKE_PARALLEL_JOBS_CHECK_ARGS := -j$(DEB_PARALLEL_JOBS_CHECK)
	DEB_MAKE_PARALLEL_JOBS_BUILD_ARGS := -j$(DEB_PARALLEL_JOBS_BUILD)
	DEB_MAKE_BUILD_TARGET = $(DEB_MAKE_PARALLEL_JOBS_BUILD_ARGS)
endif

DEB_MAKE_CHECK_TARGET := $(DEB_MAKE_PARALLEL_JOBS_CHECK_ARGS) check TMPDIR="$(CURDIR)"/tmpdata
DEB_INSTALL_CHANGELOGS_ALL := NEWS
DEB_INSTALL_DOCS_ALL := AUTHORS README
DEB_DH_MAKESHLIBS_ARGS_ALL := -V -- -t

include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

DEB_COPYRIGHT_CHECK_IGNORE_REGEX := ^debian/(changelog|copyright(|_hints|_newhints))$$|^doc/.*[.](pdf|jpg|eps|gif|png)$$|^src/test/.*[.](min|det|far|fst|mod|cnts|mrg|pru|txt|sym|print|tgz)|.*/usr/(bin|lib)/.*)|src/(.*\.(o|lo|a|la|so|backup)|src/.*/.libs/.*)$$

DEB_TAR_SRCDIR := .

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_ACLOCAL_ARGS= 
DEB_AUTO_UPDATE_AUTOMAKE = ,
DEB_AUTOMAKE_ARGS = --foreign --add-missing --copy
DEB_AUTO_UPDATE_AUTOCONF = 2.69

DEB_CONFIGURE_USER_FLAGS = --enable-static

CDBS_BUILD_DEPENDS += , debhelper (>= 9), dpkg-dev (>= 1.16.1~), libfst-dev (>= 1.6.3), libfst-tools (>= 1.6.3)

BACKUP=                         \
        src/bin/Makefile.in     \
        src/lib/Makefile.in     \
        src/test/Makefile.in    \
        src/Makefile.in         \
        src/include/Makefile.in \
        Makefile.in             \
        configure               \
        aclocal.m4              \
        ltmain.sh               \
        config.h.in             \
        depcomp                 \
        missing                 \
        compile                 \
        m4/libtool.m4           \
        m4/ltoptions.m4         \
        m4/ltsugar.m4           \
        m4/ltversion.m4         \
        m4/lt~obsolete.m4


pre-build::
	# Backup files expected to be modified.
	set -e ; for i in $(BACKUP); do \
		if [ ! -f "$$i".backup ]; then cp -a "$$i" "$$i".backup; fi ; \
	done
	mkdir -p "$(CURDIR)"/tmpdata

clean::
	set -e ; for i in $(BACKUP); do \
		if [ -f "$$i".backup ]; then mv -f "$$i".backup "$$i"; fi ; \
	done
	$(RM) -r "$(CURDIR)"/tmpdata
	$(RM) -r config.log
