#!/usr/bin/make -f
# -*- makefile -*-
#export DH_VERBOSE=1

# minimise needless linking
export DEB_LDFLAGS_MAINT_APPEND= -Wl,--as-needed

# Enable hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

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

export JAVA_HOME=/usr/lib/jvm/default-java
## Set JAVAC to prevent FTBFS due to incorrect use of 'gcj' if found (see "m4/ac_prog_javac.m4").
export JAVAC=javac

extraopts += --disable-silent-rules
extraopts += --with-ocf --with-rest-bench --with-nss
extraopts += --with-debug
extraopts += --enable-cephfs-java
extraopts += --with-babeltrace

ifeq ($(DEB_HOST_ARCH), armel)
  # armel supports ARMv4t or above instructions sets.
  # libatomic-ops is only usable with Ceph for ARMv6 or above.
  extraopts += --without-libatomic-ops
endif

# Only selected architectures support gperftools
gperftools_archs = i386 amd64 powerpc armhf ppc64el
ifneq (,$(filter $(DEB_HOST_ARCH), $(gperftools_archs)))
  extraopts += --with-tcmalloc
else
  extraopts += --without-tcmalloc
endif

# Use system provided libs3
extraopts += --with-system-libs3

#extraopts += --with-cluster-tests

%:
	dh $@ --with javahelper,python2,autoreconf,systemd --parallel

# use --as-needed only if supported by dh-autoreconf (to simplify backporting)
DH_AS_NEEDED=$(shell dpkg --compare-versions $$(dpkg --status dh-autoreconf | grep Version | cut -d' ' -f2) ge 6 && echo --as-needed)
override_dh_autoreconf:
	dh_autoreconf $(DH_AS_NEEDED)

override_dh_auto_configure:
	## copy gtest sources
	#cp -Rv /usr/src/gtest src/
	dh_auto_configure -- $(extraopts)

override_dh_auto_build:
	dh_auto_build
	cp -v src/upstart/rbdmap.conf debian/ceph.rbdmap.upstart

override_dh_auto_clean:
	#$(RM) -rv src/gtest
	$(RM) -rv src/test/fake_home src/mkfs src/test-ceph-disk/osd
	dh_auto_clean
	rm -fv debian/*.upstart

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp
	# NOTE: ensure that any versioned erasure coding test code is dropped
	#	   from the package install - package ships unversioned modules.
	rm -f $(CURDIR)/debian/tmp/usr/lib/*/ceph/erasure-code/libec_*.so.*
	find $(CURDIR)/debian/tmp/usr/lib/*/ceph/erasure-code -type l -delete || :

override_dh_installinit:
	dh_installinit --no-start
	dh_installinit -pceph --no-start --name=rbdmap
	# Install upstart configurations using dh_installinit
	for conf in `ls -1 src/upstart/ceph*.conf | grep -v mds`; do \
		name=`basename $$conf | cut -d . -f 1`; \
		cp -v $$conf debian/ceph.$$name.upstart; \
		dh_installinit -pceph --no-start --name=$$name; \
	done
	for conf in `ls -1 src/upstart/ceph-mds*.conf`; do \
		name=`basename $$conf | cut -d . -f 1`; \
		cp -v $$conf debian/ceph-mds.$$name.upstart; \
		dh_installinit -pceph-mds --no-start --name=$$name; \
	done
	for conf in `ls -1 src/upstart/radosgw*.conf`; do \
		name=`basename $$conf | cut -d . -f 1`; \
		[ $$name = "radosgw" ] && name="radosgw-instance";\
		cp -v $$conf debian/radosgw.$$name.upstart; \
		dh_installinit -pradosgw --no-start --name=$$name; \
	done

override_dh_systemd_enable:
	## Do not enable services to match `dh_installinit --no-start`
	## behaviour.
	## Users are expected to "systemctl enable" services once their
	## configuration is correct.

override_dh_strip:
	dh_strip -pceph --dbg-package=ceph-dbg
	dh_strip -pceph-mds --dbg-package=ceph-mds-dbg
	dh_strip -pceph-fuse --dbg-package=ceph-fuse-dbg
	dh_strip -prbd-fuse --dbg-package=rbd-fuse-dbg
	dh_strip -pceph-common --dbg-package=ceph-common-dbg
	dh_strip -pceph-fs-common --dbg-package=ceph-fs-common-dbg
	dh_strip -plibrados2 --dbg-package=librados2-dbg
	dh_strip -plibradosstriper1 --dbg-package=libradosstriper1-dbg
	dh_strip -plibrbd1 --dbg-package=librbd1-dbg
	dh_strip -plibcephfs1 --dbg-package=libcephfs1-dbg
	dh_strip -pradosgw --dbg-package=radosgw-dbg
	dh_strip -prest-bench --dbg-package=rest-bench-dbg
	dh_strip -pceph-test --dbg-package=ceph-test-dbg
	dh_strip -plibrados-dev
	dh_strip -plibcephfs-jni --dbg-package=libcephfs-jni-dbg

override_dh_makeshlibs:
	# exclude jni libraries in libcephfs-jni to avoid pointless ldconfig
	# calls in maintainer scripts; exclude private erasure-code plugins.
	dh_makeshlibs -V -X/usr/lib/jni -X/usr/lib/$(DEB_HOST_MULTIARCH)/ceph/erasure-code

override_dh_auto_test:
	# Skip tests as they depend on virtualenv
	:

.PHONY: override_dh_auto_configure override_dh_installinit override_dh_strip override_dh_makeshlibs override_dh_auto_test
