#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/vendor.mk
# faster version of dpkg/architecture.mk defining only vars we actually need
$(foreach var,DEB_HOST_ARCH DEB_HOST_ARCH_OS DEB_HOST_MULTIARCH,\
	$(eval export ${var} ?= $$(shell dpkg-architecture -q${var})))

export PYTHON = python3

dh_packagelist := $(shell dh_listpackages)

LDB_EPOCH := 2:
debian/ldb-version.mk: lib/ldb/wscript
# define two variables based on local version of ldb
# samba ships ldb modules, which are specific to the ldb version,
# so we need a strict dependency on the upstream ldb version
	@echo "(Re)checkig version of LDB library.."
	@set -e; \
	v=$$(grep ^VERSION $< | cut -d\' -f2); \
	[ -n "$$v" ] || { echo "E: unable to get LDB version from $<" >&2; exit 1; }; \
	v="\$${LDB_EPOCH}$$v"; n="$${v%.*}.$$(($${v##*.}+1))"; exec 3>$@.tmp; \
	echo "# autogenerated from $<" >&3; \
	echo "LDB_DEB_VERSION := $$v+samba\$${DEB_VERSION_UPSTREAM_REVISION}" >&3; \
	echo "LDB_DEPENDS := libldb2 (>> $$v~), libldb2 (<< $$n~)" >&3; \
	exec 3>&-; mv -f $@.tmp $@
include debian/ldb-version.mk

V = 1
WAF_CMD := PYTHONHASHSEED=1 ${PYTHON} ./buildtools/bin/waf
WAF := ${WAF_CMD} $(if ${V},-v)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    WAF += -j $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

conf_args = \
		--prefix=/usr \
		--enable-fhs \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libexecdir=/usr/libexec \
		--with-privatedir=/var/lib/samba/private \
		--with-smbpasswd-file=/etc/samba/smbpasswd \
		--with-piddir=/run/samba \
		--with-pammodulesdir=/lib/$(DEB_HOST_MULTIARCH)/security \
		--with-pam \
		--with-syslog \
		--with-utmp \
		--with-winbind \
		--with-quota \
		--with-shared-modules=idmap_rid,idmap_ad,idmap_adex,idmap_hash,idmap_ldap,idmap_tdb2,vfs_dfs_samba4,auth_samba4,vfs_nfs4acl_xattr \
		--with-automount \
		--with-ldap \
		--with-ads \
		--with-gpgme \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--with-modulesdir=/usr/lib/$(DEB_HOST_MULTIARCH)/samba \
		--datadir=/usr/share \
		--with-lockdir=/run/samba \
		--with-statedir=/var/lib/samba \
		--with-cachedir=/var/cache/samba \
		--enable-avahi \
		--disable-rpath \
		--disable-rpath-install \
		--bundled-libraries=NONE,pytevent,ldb \
		--with-cluster-support \
		--with-socketpath=/run/ctdb/ctdbd.socket \
		--with-logdir=/var/log/ctdb \
		--enable-etcd-reclock \
		--enable-spotlight


WITH_CEPH = yes
# Ceph is not available on all platforms
ifeq (,$(filter amd64 arm64 armel armhf i386 mips64el mipsel ppc64el ppc64 s390x x32, $(DEB_HOST_ARCH)))
    WITH_CEPH =
endif
# Ubuntu i386 binary compatibility only effort: No Ceph
ifeq ($(DEB_VENDOR) $(DEB_HOST_ARCH), Ubuntu i386)
    WITH_CEPH =
endif

ifneq ($(WITH_CEPH),)
	conf_args += \
		--enable-ceph-reclock
else
	conf_args += \
		--disable-cephfs
endif


WITH_GLUSTERFS = yes
# GlusterFS is linux-only
ifneq ($(DEB_HOST_ARCH_OS), linux)
    WITH_GLUSTERFS =
endif
# GlusterFS is not in Ubuntu main
ifeq ($(DEB_VENDOR), Ubuntu)
    WITH_GLUSTERFS =
endif


ifeq ($(DEB_HOST_ARCH_OS), linux)
	conf_args += \
		--with-systemd \
		--systemd-install-services \
		--with-systemddir=/lib/systemd/system
else
	conf_args += \
		--without-systemd
endif

# Ubuntu i386 binary compatibility only effort: Disable some i386 packages
ifeq ($(DEB_VENDOR) $(DEB_HOST_ARCH), Ubuntu i386)
	BUILD_PACKAGES += \
		-Nctdb \
		-Nlibpam-winbind \
		-Nsamba \
		-Nsamba-testsuite
endif

binary binary-arch binary-indep build build-arch build-indep \
clean install install-arch install-indep: %:
	dh $* --with python3 $(BUILD_PACKAGES)

override_dh_auto_configure configure: bin/configured.stamp
.PHONY: configure
bin/configured.stamp:
	${WAF_CMD} -j 1 configure ${conf_args} || \
	  { $(if ${V},echo "==== contents of config.log:"; cat bin/config.log;) false; }
	touch $@

override_dh_auto_build:
# samba build system is designed so that default build (what is produced
# by waf build) supposed to be run directly from the build directory,
# with all the paths pointing there.  At the install stage, quite some
# recompilation/relinking is done again, to adopt to the actual install
# paths. There's no need (for now) to build samba to be run from the build
# directory, so we use `waf install' here instead of `waf build'.
# However, as of 4.16, `waf install' without the build step fails, since
# it tries to run two executables which are not built.
# Build these two executables first, and build the install stage.
# This will pefrorm unnecessary/extra install step (into d/tmp), which
# we'll repeat during actual install stage, but this is definitely
# better/faster than building whole thing for _not_ running from the build dir.
	${WAF} --targets=compile_et,asn1_compile
	${WAF} install --destdir="${CURDIR}/debian/tmp"

override_dh_auto_test:
	# Running make test requires configuration with --enable-selftest,
	# which we don't want to do for production systems.

override_dh_auto_install:
	${WAF} install --destdir="${CURDIR}/debian/tmp"
	# get list of files in build log
	find debian/tmp
	# Included in python-tevent
	rm debian/tmp/usr/lib/python*/*-packages/_tevent.*
	rm debian/tmp/usr/lib/python*/*-packages/tevent.py
	# Already documented in debian/copyright
	-rm debian/tmp/usr/share/samba/setup/ad-schema/licence.txt
	# pam stuff
	install -Dp -m0644 debian/winbind.pam-config debian/tmp/usr/share/pam-configs/winbind
	mv debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libnss_* debian/tmp/lib/$(DEB_HOST_MULTIARCH)/
	# Debian goodies to set global option in smb.conf and add a share
	install -p -m0755 debian/setoption.py -t debian/tmp/usr/share/samba/
	install -p -m0755 debian/addshare.py -t debian/tmp/usr/share/samba/
	# Debian goodies
	install -p -m0644 debian/smb.conf* -t debian/tmp/usr/share/samba/
	install -p -m755 debian/panic-action -t debian/tmp/usr/share/samba/
	install -p -m755 debian/update-apparmor-samba-profile -t debian/tmp/usr/share/samba/
	install -p -m0644 debian/gdbcommands -t debian/tmp/etc/samba/
	install -Dp -m0755 debian/samba-common.dhcp debian/tmp/etc/dhcp/dhclient-enter-hooks.d/samba
	install -p -m0755 debian/mksmbpasswd.awk debian/tmp/usr/sbin/mksmbpasswd
	install -Dp -m0644 debian/samba.ufw.profile debian/tmp/etc/ufw/applications.d/samba
	install -Dp -m0644 debian/source_samba.py -t debian/tmp/usr/share/apport/package-hooks/
ifeq ($(DEB_HOST_ARCH_OS), linux)
	# Install systemd configs
	install -Dp -m0644 ctdb/config/ctdb.service -t debian/tmp/lib/systemd/system/
	# Services fixups
	mv debian/tmp/lib/systemd/system/nmb.service debian/tmp/lib/systemd/system/nmbd.service
	mv debian/tmp/lib/systemd/system/smb.service debian/tmp/lib/systemd/system/smbd.service
	mv debian/tmp/lib/systemd/system/samba.service debian/tmp/lib/systemd/system/samba-ad-dc.service
	sed -i \
	  -e 's|/etc/sysconfig/|/etc/default/|' \
	  -e 's|nmb\.service|nmbd.service|' \
	  -e 's|smb\.service|smbd.service|' \
	  -e 's|samba\.service|samba-ad-dc.service|' \
	  debian/tmp/lib/systemd/system/nmbd.service \
	  debian/tmp/lib/systemd/system/samba-ad-dc.service \
	  debian/tmp/lib/systemd/system/smbd.service \
	  debian/tmp/lib/systemd/system/winbind.service
endif
	# Harmless on systems without systemd-tmpfiles, and other consumers exist
	# such as OpenRC's opentmpfiles
	mkdir -p debian/tmp/usr/lib/tmpfiles.d
	echo "d /run/samba 0755 root root -" > debian/tmp/usr/lib/tmpfiles.d/samba.conf
	echo "d /run/ctdb 0755 root root -" > debian/tmp/usr/lib/tmpfiles.d/ctdb.conf

	# install-and-rename docs for ctdb (also arch-specific)
	mkdir -p debian/tmp/ctdb
	install -p ctdb/config/events/README debian/tmp/ctdb/README.notification
	install -p ctdb/config/notification.README debian/tmp/ctdb/README.notification
ifeq ($(DEB_HOST_ARCH_OS), hurd)
	install -p debian/ctdb.README.hurd debian/tmp/ctdb/README.hurd
endif
ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	install -p debian/ctdb.README.kfreebsd debian/tmp/ctdb/README.kfreebsd
endif

override_dh_install:
	DEB_PY3_INCDIR=$$(${PYTHON}-config --includes | sed 's,^-I\([^ ]*\).*,\1,') \
		dh_install

override_dh_installinit:
ifneq (,$(filter samba, ${dh_packagelist}))
	dh_installinit -psamba --name smbd
	dh_installinit -psamba --name nmbd --error-handler nmbd_error_handler
	dh_installinit -psamba --name samba-ad-dc
endif
ifneq (,$(filter winbind, ${dh_packagelist}))
	dh_installinit -pwinbind
endif
ifneq (,$(filter ctdb, ${dh_packagelist}))
	# Install /etc/init.d/ctdb
	mkdir -p debian/ctdb/etc/init.d
	install -m755 ctdb/config/ctdb.init debian/ctdb/etc/init.d/ctdb
	# Install dh scripts
	dh_installinit -pctdb --no-start --no-stop-on-upgrade --onlyscripts
endif

override_dh_installsystemd:
ifneq (,$(filter samba, ${dh_packagelist}))
	dh_installsystemd -psamba
endif
ifneq (,$(filter winbind, ${dh_packagelist}))
	dh_installsystemd -pwinbind
endif
ifneq (,$(filter ctdb, ${dh_packagelist}))
	dh_installsystemd -pctdb --no-start --no-stop-on-upgrade
endif

override_dh_installpam:
	dh_installpam -psamba-common --name=samba

#override_dh_perl-arch:
#	dh_perl -a
#	# Fix Perl shbangs
#	sed -i '1s@^#!/usr/bin/env perl@#!/usr/bin/perl@' \
#	  debian/smbclient/usr/bin/findsmb

override_dh_fixperms:
	dh_fixperms
ifneq (,$(filter samba, ${dh_packagelist}))
	chmod 1777 debian/samba/var/spool/samba/
endif
ifneq (,$(filter smbclient, ${dh_packagelist}))
	chmod 0700 debian/smbclient/usr/libexec/samba/smbspool_krb5_wrapper
endif

override_dh_makeshlibs:
	# create symbols and shlibs files in separate wrapper script
	# to deal with private libraries
	debian/make_shlibs "${LDB_DEB_VERSION}|libldb2|ldb-tools|libldb-dev|python3-ldb|python3-ldb-dev"

override_dh_shlibdeps:
# for specific executables/modules, put dependencies in separate variables
# to change Depends to Recommends for them in d/control
	dh_shlibdeps -l${CURDIR}/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/samba \
	    -Xceph.so -Xglusterfs.so -Xsnapper.so -Xctdb_mutex_ceph_rados_helper
ifneq ($(WITH_CEPH),)
	dpkg-shlibdeps -Tdebian/ctdb.substvars -prados \
	    debian/ctdb/usr/libexec/ctdb/ctdb_mutex_ceph_rados_helper
endif
	dpkg-shlibdeps -Tdebian/samba-vfs-modules.substvars -pvfsmods \
	    debian/samba-vfs-modules/usr/lib/*/samba/vfs/snapper.so \
	    $(if ${WITH_CEPH}, debian/samba-vfs-modules/usr/lib/*/samba/vfs/ceph.so) \
	    $(if ${WITH_GLUSTERFS}, debian/samba-vfs-modules/usr/lib/*/samba/vfs/glusterfs.so)
# after shlibdeps run, check that we don't have wrong depdendencies
	# libldb2 should not depend on samba-libs
	# (also see debian/patches/use-bzero-instead-of-memset_s.diff)
	if egrep "^shlibs.Depends=.* (samba-libs|samba) " debian/libldb2.substvars; \
	then echo "E: libldb2 should not depend on samba libs" >&2; exit 1; fi
	# python3-samba should not depend on samba
	if egrep "^shlibs.Depends=.* (samba) " debian/python3-samba.substvars; \
	then echo "E: python3-samba should not depend on samba" >&2; exit 1; fi
	# libwbclient0 should not depend on samba-libs (probably)
	if egrep "^shlibs.Depends=.* (samba-libs|samba) " debian/libwbclient0.substvars; \
	then echo "E: libwbclient0 should not depend on samba libs" >&2; exit 1; fi

override_dh_gencontrol:
	dh_gencontrol -plibldb2 -pldb-tools -plibldb-dev -ppython3-ldb -ppython3-ldb-dev \
	  -- -v${LDB_DEB_VERSION}
	dh_gencontrol --remaining-packages -- -Vldb:Depends="${LDB_DEPENDS}"

override_dh_auto_clean:
	rm -rf bin/
	find -name __pycache__ -exec rm -rf {} +
	rm -f .lock-wscript
	rm -f compile_commands.json
