# Template file for 'samba'
pkgname=samba
version=4.20.1
revision=3
build_style=waf3
build_helper="qemu"
configure_script="buildtools/bin/waf"
_idmap_modules="idmap_ad,idmap_rid,idmap_ldap,idmap_hash,idmap_tdb2"
_pdb_modules="pdb_tdbsam,pdb_ldap,pdb_smbpasswd,pdb_wbc_sam,pdb_samba4"
_auth_modules="auth_unix,auth_wbc,auth_server,auth_samba4,auth_skel"
_vfs_modules="vfs_io_uring"
configure_args="--enable-fhs --sysconfdir=/etc --localstatedir=/var
 --sbindir=/usr/bin --with-privatelibdir=/usr/lib
 --with-privatedir=/etc/samba/private --with-piddir=/run/samba
 --with-sockets-dir=/run/samba --with-lockdir=/run/lock/samba
 --disable-rpath --disable-rpath-install --nopyc --nopyo
 --without-gettext --without-systemd --without-gpgme
 --with-cluster-support --with-system-mitkrb5 --with-experimental-mit-ad-dc
 --bundled-libraries=NONE --builtin-libraries=NONE
 --with-shared-modules=${_idmap_modules},${_pdb_modules},${_auth_modules},${_vfs_modules}"
hostmakedepends="pkg-config perl-Parse-Yapp rpcsvc-proto docbook-xsl libxslt
 libtasn1-tools tdb-python3 tevent-python3 talloc-python3 ldb-python3 flex
 bison mit-krb5 python3-Markdown python3-dnspython"
makedepends="python3-devel libtirpc-devel popt-devel e2fsprogs-devel
 mit-krb5-devel pam-devel acl-devel cups-devel avahi-libs-devel tdb-devel
 talloc-devel tevent-devel ldb-devel cmocka-devel gnutls-devel zlib-devel
 ncurses-devel libldap-devel libarchive-devel jansson-devel liburing-devel"
short_desc="SMB/CIFS file, print, and login server for Unix"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-3.0-or-later"
homepage="http://www.samba.org"
distfiles="https://download.samba.org/pub/samba/stable/${pkgname}-${version}.tar.gz"
checksum=f93c3af5295340d08106c7c0dcfb85e4f85057dfd14587aa8817beb31aff88f7
lib32disabled=yes
conf_files="/etc/pam.d/samba /etc/samba/smb.conf"
make_dirs="/etc/samba/private 0750 root root"
subpackages="smbclient samba-ctdb samba-cups samba-devel samba-libs
 samba-python3 samba-dc"

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" musl-legacy-compat musl-nscd-devel libexecinfo-devel"
else
	makedepends+=" glusterfs-devel"
	subpackages+=" samba-glusterfs"

	if [ "$XBPS_CHECK_PKGS" ]; then
		configure_args+=" --enable-selftest"
		hostmakedepends+=" python3-cryptography python3-iso8601
		 python3-pyasn1"
		makedepends+=" nss_wrapper pam_wrapper resolv_wrapper
		  socket_wrapper uid_wrapper"
	fi
fi

export PYTHON_CONFIG="${XBPS_CROSS_BASE}/usr/bin/python3-config"
export PYTHONHASHSEED=1

post_patch() {
	# Look for properly named python support libraries
	vsed -i third_party/waf/waflib/Tools/python.py \
		-e "s|env.pyext_PATTERN = .*$|env.pyext_PATTERN = '%s.so'|g"

	# Fix for our `kdb` linking against the system `db` package which
	# doesn't distribute a pkg-config file and the samba build scripts just
	# can't handle that.
	cat >>wscript_configure_system_mitkrb5 <<_EOF
if ("db" in conf.env["LIB_KDB5"]
      and not conf.check_cfg(package="db", mandatory=False)):
    krb5_define_syslib(conf, "db", [])
_EOF

	# Fix log and spool locations in conf file
	vsed -i examples/smb.conf.default \
		-e 's|/usr/spool/samba|/var/spool/samba|g' \
		-e 's|log file = .*$|log file = /var/log/samba/%m.log|g'
}

post_install() {
	# install conf file
	vinstall examples/smb.conf.default 644 etc/samba smb.conf

	# move winbind krb5 locator into place
	vmkdir usr/lib/krb5/plugins/libkrb5
	mv ${DESTDIR}/usr/lib/samba/krb5/winbind_krb5_locator.so \
		${DESTDIR}/usr/lib/krb5/plugins/libkrb5

	vsv nmbd
	vsv smbd

	# PAM support
	vinstall ${FILESDIR}/samba.pam 644 etc/pam.d samba

	# Link cups backend
	vmkdir usr/lib/cups/backend
	ln -fs /usr/bin/smbspool ${DESTDIR}/usr/lib/cups/backend/smb

	vinstall packaging/printing/smbprint 744 usr/bin/

	# Do not ship sudo rule that grants nopasswd privileges by default
	rm -rf ${DESTDIR}/etc/sudoers.d

	# Do not ship no-op sysvinit scripts
	rm -rf ${DESTDIR}/etc/init.d
}

samba-cups_package() {
	build_style=meta
	depends="smbclient>=${version}_${revision}"
	short_desc="SMB backend for cups (transitional dummy package)"
}

smbclient_package() {
	short_desc+=" - client utilities"
	replaces="samba-cups>=0"
	pkg_install() {
		local _t _tools

		# These utilities have man pages in section 1
		_tools="dbwrap_tool mdsearch mvxattr nmblookup oLschema2ldif
		regdiff regpatch regshell rpcclient sharesec smbcacls smbclient
		smbcquotas smbget smbtar smbtree wspsearch"

		for _t in ${_tools}; do
			vmove usr/bin/${_t}
			vmove usr/share/man/man1/${_t}.1
		done

		# These utilities have man pages in section 8
		_tools="cifsdd samba-regedit smbspool"
		for _t in ${_tools}; do
			vmove usr/bin/${_t}
			vmove usr/share/man/man8/${_t}.8
		done

		# No man page for these
		vmove usr/bin/dumpmscat
		vmove usr/bin/smbprint

		vmove usr/libexec/samba/smbspool_krb5_wrapper
		vmove usr/share/man/man8/smbspool_krb5_wrapper.8

		# Move symlink
		vmove usr/lib/cups/backend/smb
	}
}

samba-ctdb_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - Clustered TDB support"
	pkg_install() {
		local _f
		vmove "usr/bin/ctdb*"

		for _f in man{1,5,7}; do
			vmove "usr/share/man/${_f}/ctdb*"
		done

		for _f in ltdbtool onnode ping_pong; do
			vmove "usr/bin/${_f}"
			vmove "usr/share/man/man1/${_f}.1"
		done

		vmove usr/libexec/ctdb
		vmove usr/share/ctdb
		vmove etc/ctdb

		vsv ctdbd
	}
}

samba-libs_package() {
	short_desc+=" - core libraries"
	provides="libsmbclient-${version}_${revision}"
	replaces="libsmbclient>=0"
	pkg_install() {
		local _libs _privlibs _lib
		_libs="dcerpc dcerpc-binding dcerpc-samr dcerpc-server
		 dcerpc-server-core ndr ndr-krb5pac ndr-nbt ndr-standard netapi
		 nss_winbind nss_wins samba-credentials samba-errors
		 samba-hostconfig samba-passdb samba-policy samba-util samdb
		 smbclient smbconf smbldap tevent-util wbclient"
		_privlibs="CHARSET3 LIBWBCLIENT-OLD MESSAGING MESSAGING-SEND
		 REG-FULL RPC-SERVER-LOOP RPC-WORKER ad-claims addns ads
		 asn1util auth auth-unix-token auth4 authkrb5 authn-policy-util
		 cli-cldap cli-ldap cli-ldap-common cli-nbt cli-smb-common
		 cli-spoolss cliauth clidns cluster cmdline cmdline-contexts
		 common-auth ctdb-event-client db-glue dbwrap dcerpc-pkt-auth
		 dcerpc-samba dcerpc-samba4 dfs-server-ad dlz-bind9-for-torture
		 dnsserver-common dsdb-garbage-collect-tombstones dsdb-module
		 events flag-mapping genrand gensec gpext gpo gse http idmap
		 interfaces iov-buf krb5samba ldbsamba libcli-lsa3
		 libcli-netlogon3 libsmb messages-dgm messages-util mscat
		 msghdr msrpc3 ndr-samba ndr-samba4 net-keytab netif
		 npa-tstream nss-info pac posix-eadb printer-driver
		 printing-migrate process-model registry replace
		 samba-cluster-support samba-debug samba-modules samba-net
		 samba-python samba-security samba-sockets samba3-util
		 samdb-common scavenge-dns-records secrets3 server-id-db
		 server-role service shares smb-transport smbclient-raw
		 smbd-base smbd-shim smbldaphelper smbpasswdparser
		 socket-blocking stable-sort sys-rw talloc-report
		 talloc-report-printf tdb-wrap time-basic torture trusts-util
		 util-reg util-setid util-tdb xattr-tdb"

		for _lib in ${_libs}; do
			vmove "usr/lib/lib${_lib}.so.*"
		done

		for _lib in ${_privlibs}; do
			vmove "usr/lib/lib${_lib}-private-samba.so"
		done

		vmove usr/share/man/man7/libsmbclient.7
	}
}

samba-devel_package() {
	depends="samba-libs>=${version}_${revision}
	 ${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/pkgconfig

		vmkdir usr/lib

		local _f
		for _f in ${DESTDIR}/usr/lib/*.so; do
			[ -L "$_f" ] || continue
			mv "$_f" ${PKGDESTDIR}/usr/lib
		done
	}
}

samba-python3_package() {
	depends="ldb-python3 talloc-python3 tdb-python3 tevent-python3
	 python3-cryptography python3-dnspython python3-Markdown
	 python3-requests"
	short_desc+=" - Python3 bindings"
	pkg_install() {
		vmove ${py3_lib}
	}
}

samba-glusterfs_package() {
	short_desc+=" - GlusterFS VFS module"
	pkg_install() {
		vmove usr/lib/samba/vfs/glusterfs.so
		vmove usr/share/man/man8/vfs_glusterfs.8
	}
}

samba-dc_package() {
	depends="samba-python3>=${version}_${revision}"
	short_desc+=" - AD Domain Controller"
	pkg_install() {
		vmove "usr/bin/samba*"
		vmove usr/share/samba
	}
}
