#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS := hardening=-all
export DEB_CFLAGS_MAINT_APPEND := -D__STDC_WANT_LIB_EXT1__=1

# For waf to make include path ordering fixed
# https://lists.samba.org/archive/samba-technical/2022-March/137230.html
# https://bugzilla.samba.org/show_bug.cgi?id=15033
export PYTHONHASHSEED := 1

# for some reason these platforms fail to find __atomic_{load,store}_8
# include a rough workaround for now, drop it when we know better, cf.
# https://salsa.debian.org/samba-team/samba/-/commit/338dccf8
include /usr/share/dpkg/architecture.mk
ifneq (,$(filter armel,${DEB_HOST_ARCH}))
LDFLAGS := ${LDFLAGS} -Wl,--as-needed -latomic
endif

SAMBA_LIBDIR := $(shell pkg-config --variable libdir samba-util)/samba
SAMBA_INCDIR := $(shell pkg-config --variable includedir samba-util)

empty :=
space := $(empty) $(empty)
samba_inc_dirs := \
	$(SAMBA_INCDIR) \
	$(CURDIR)/samba/source4 \
	$(CURDIR)/samba \
	$(CURDIR)/samba/bin/default/source \
	$(CURDIR)/samba/bin/default \
	$(CURDIR)/samba/lib/replace \
	$(CURDIR)/samba/lib \
	$(CURDIR)/samba/bin/default/include
samba_conf_args := \
	--prefix=/usr \
	--samba-lib-dirs=$(SAMBA_LIBDIR) \
	--samba-inc-dirs="$(subst $(space),:,$(strip $(samba_inc_dirs)))"

%:
	dh $@

override_dh_auto_clean:
	cd source && ./waf distclean
	rm -rf samba_* samba-* samba

override_dh_auto_configure:
	apt-get source samba
	ln -sf samba-* samba
	cd samba && ./buildtools/bin/waf -j 1 configure
	cd samba && ./buildtools/bin/waf build
	cd source && ./waf -j 1 configure $(samba_conf_args)

override_dh_auto_build:
	cd source && ./waf build --verbose

override_dh_auto_install:
	cd source && ./waf install --destdir $(CURDIR)/debian/winexe
	# bin2c is only useful at build time, and should not be installed
	# because it conflicts with nvidia-cuda-toolkit's one
	rm -f $(CURDIR)/debian/winexe/usr/bin/bin2c
