#!/usr/bin/make -f
# -*- makefile -*-

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

include /usr/share/dpkg/pkg-info.mk

# Use gcc and g++ by default, but allow setting them from the environment
CC ?= gcc
CXX ?= g++

DEB_SCONS_OPTIONS :=
ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
	DEB_SCONS_OPTIONS := --d=DEBUGBUILD
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	PROCS=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
# Kali: arm64 build failed so we reduced the number of parallel jobs
ifeq ($(DEB_BUILD_ARCH),arm64)
	DEB_SCONS_OPTIONS += -j$(shell expr $(PROCS) / 2)
else
	DEB_SCONS_OPTIONS += -j$(PROCS)
endif
endif

COMMON_OPTIONS = --use-system-tcmalloc \
		 --use-system-pcre2 \
		 --use-system-snappy \
		 --use-system-zlib --use-system-valgrind \
		 --use-system-stemmer --use-system-yaml \
		 --use-system-zstd \
		 --disable-warnings-as-errors \
		 --linker=gold \
		 --ssl $(DEB_SCONS_OPTIONS) \
		 MONGO_VERSION=$(DEB_VERSION_UPSTREAM) \
		 CC=$(CC) CXX=$(CXX)

gperftools_archs = amd64 arm64
ifneq (,$(filter $(DEB_HOST_ARCH), $(gperftools_archs)))
COMMON_OPTIONS += --allocator=tcmalloc
else
COMMON_OPTIONS += --allocator=system
endif

override_dh_auto_clean:
	python3 src/third_party/scons-3.1.2/scons.py --keep-going --clean $(COMMON_OPTIONS) .
	find $(CURDIR)/ -name "*.pyc" -delete
	rm -rf $(CURDIR)/.scons/
	rm -rf $(CURDIR)/build/

override_dh_auto_install:
	python3 src/third_party/scons-3.1.2/scons.py DESTDIR=debian/tmp/usr $(COMMON_OPTIONS) install-devcore

override_dh_installsystemd:
	dh_installsystemd -pmongodb-server --no-enable --name=mongodb

%:
	dh $@
