# Template file for 'samba'
pkgname=samba
version=4.14.12
revision=2
build_style=waf3
build_helper="qemu"
configure_script="buildtools/bin/waf"
_idmap_modules="idmap_ad,idmap_rid,idmap_adex,idmap_hash,idmap_tdb2"
_pdb_modules="pdb_tdbsam,pdb_ldap,pdb_ads,pdb_smbpasswd,pdb_wbc_sam,pdb_samba4"
_auth_modules="auth_unix,auth_wbc,auth_server,auth_netlogind,autH_script,auth_samba4"
configure_args="--enable-fhs --sbindir=/usr/bin
 --localstatedir=/var --sysconfdir=/etc --with-piddir=/run/samba
 --with-sockets-dir=/run/samba --with-lockdir=/run/lock/samba
 --with-privatedir=/etc/samba/private --with-pammodulesdir=/usr/lib/security
 --with-modulesdir=/usr/lib/samba --disable-rpath --disable-rpath-install
 --without-systemd --without-gettext --bundled-libraries=NONE
 --with-system-mitkrb5 --without-ad-dc --with-cluster-support
 --with-shared-modules=${_idmap_modules},${_pdb_modules},${_auth_modules}"
hostmakedepends="pkg-config perl-Parse-Yapp rpcsvc-proto docbook2x
 libtasn1-tools tdb-python3 tevent-python3 talloc-python3"
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"
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=155d9c2dfb06a18104422987590858bfe5e9783ebebe63882e7e7f07eaaa512d
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"

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

# By default, samba wants to install a bunch of private "*-samba4.so" shared
# libraries that leak into shlib_requires dependencies. Most of these can be
# built into the public shared libraries, avoiding the mess of tracking shlibs
# that shouldn't be exposed locally. For those that cannot be built in, make
# sure to install them into /usr/lib so xbps-src picks them up.
_privlibs="CHARSET3 MESSAGING_SEND MESSAGING LIBWBCLIENT_OLD addns ads \
 asn1util auth authkrb5 cmdline_contexts cmdline-credentials cli_cldap \
 cli-ldap-common cli-nbt cli_smb_common cli_spoolss clidns common-auth \
 ctdb-event-client dbwrap dcerpc-pkt-auth events genrand gensec gse http \
 interfaces krb5samba ldbsamba libcli_lsa3 libcli_netlogon3 libsmb \
 messages_dgm messages_util mscat msghdr msrpc3 netif npa_tstream popt_samba3 \
 popt_samba3_cmdline registry replace samba-cluster-support samba-debug \
 samba-modules samba-security samba-sockets samba3-util samdb-common secrets3 \
 server_id_db server-role smbclient-raw smbd_shim socket-blocking sys_rw \
 talloc_report_printf talloc_report tevent-util time-basic trusts_util \
 util_reg util_setid util_tdb"
configure_args+=" --with-privatelibdir=/usr/lib --builtin-libraries=${_privlibs// /,}"

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

post_patch() {
	# Look for properly named python support libraries
	vsed -i third_party/waf/waflib/Tools/python.py \
		-e "/env.pyext_PATTERN/s/dct\['SO'\]/'.so'/"

	# 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

	# 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="findsmb dbwrap_tool mdfind mvxattr nmblookup ntlm_auth
		 oLschema2ldif regdiff regpatch regshell regtree rpcclient
		 sharesec smbcacls smbclient smbcquotas smbget smbtar smbtree
		 wbinfo"

		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 this one
		vmove usr/bin/dumpmscat

		# Extra man page for smbget
		vmove usr/share/man/man5/smbgetrc.5

		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 ndr-krb5pac ndr-nbt ndr-standard ndr
		 samba-credentials samba-errors samba-hostconfig samba-passdb
		 samba-util samdb smbclient smbconf smbldap wbclient"
		_privlibs="cli-ldap cliauth cluster common-auth dcerpc-samba
		 flag-mapping gpo iov-buf ndr-samba ndr printer-driver
		 smb-transport tdb-wrap util-cmdline winbind-client"

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

		for _lib in ${_privlibs}; do
			vmove "usr/lib/lib${_lib}-samba4.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() {
	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
	}
}
