#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# work around LP: #723515
export DEB_GCC_NO_O3=1

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

ifneq (,$(filter $(DEB_HOST_ARCH), amd64 ppc64 ppc64el))
  LIBARGS = LIB32=lib32 LIB64=lib V=1
else ifneq (,$(filter $(DEB_HOST_ARCH), i386 powerpc))
  LIBARGS = LIB32=lib LIB64=lib64 V=1
endif


%:
	dh $@

override_dh_auto_clean:
	$(MAKE) clean V=1

override_dh_auto_build:
	# Parallel builds are not reliable
	$(MAKE) BUILDTYPE=NATIVEONLY $(LIBARGS) V=1

override_dh_auto_install:
	mkdir -p debian/tmp/usr/share/libhugetlbfs/tests
	$(MAKE) install PREFIX=/usr DESTDIR=$(CURDIR)/debian/tmp BUILDTYPE=NATIVEONLY $(LIBARGS)
	$(MAKE) install-helper PREFIX=/usr DESTDIR=$(CURDIR)/debian/tmp BUILDTYPE=NATIVEONLY $(LIBARGS)
	$(MAKE) install-tests PREFIX=/usr DESTDIR=$(CURDIR)/debian/tmp BUILDTYPE=NATIVEONLY $(LIBARGS) INST_TESTSDIR64=/usr/share/libhugetlbfs/tests INST_TESTSDIR32=/usr/share/libhugetlbfs/tests

	# Fix minor issues
	find debian/tmp/  -name dummy.ldscript -print0 | xargs -r0 chmod a-x
	# Actually, we don't want hat helper.
	# mv debian/tmp/usr/bin/huge_page_setup_helper.py debian/tmp/usr/bin/huge_page_setup_helper

	install -m 755 -d $(CURDIR)/debian/tmp/etc/security/limits.d
	install -m 644 $(CURDIR)/debian/limits.conf $(CURDIR)/debian/tmp/etc/security/limits.d/hugepages.conf

#	mv debian/tmp/usr/lib/libhugetlbfs.so debian/tmp/usr/lib/libhugetlbfs.so.0
#	ln -sf libhugetlbfs.so.0 debian/tmp/usr/lib/libhugetlbfs.so
#	mv debian/tmp/usr/lib/libhugetlbfs_privutils.so debian/tmp/usr/lib/libhugetlbfs_privutils.so.0
#	ln -sf libhugetlbfs_privutils.so.0 debian/tmp/usr/lib/libhugetlbfs_privutils.so

	dh_install --sourcedir=debian/tmp

override_dh_auto_test:
	-echo "SKIP tests, as they require privilege (FIXME)"


