#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS)
CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)
# 2G per CPU seems OK.
ifeq ($(wildcard /proc/meminfo),)
	MAX_CPUS=1
else
	MAX_CPUS=$(shell awk '/^MemTotal/ { print int($$2/(2048*1024)+0.5) }' /proc/meminfo)
endif

ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
WEAK_ARCHES="mips mipsel armhf"
ifneq (,$(findstring ${ARCH},${WEAK_ARCHES}))
CXXOPT="-O1"
else
CXXOPT="-O2"
endif

export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

%:
	dh $@ --max-parallel="${MAX_CPUS}"

override_dh_auto_configure:
	./configure --prefix=/usr --without-java --without-callable\
		CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" \
		LDFLAGS="${LDFLAGS}" CXXOPT="${CXXOPT}"

override_dh_auto_test:
	# do nothing

override_dh_auto_clean:
	# avoid calling configure to clean
	rm -rf build.*

override_dh_perl:
	dh_perl usr/lib/polymake/perlx
