# Contributor: Shiz <hi@shiz.me>
# Maintainer: Shiz <hi@shiz.me>
pkgname=opensmtpd-extras
_pkgname=OpenSMTPD-extras
pkgver=6.7.1
pkgrel=1
pkgdesc="OpenSMTPD addons"
url="https://opensmtpd.org/"
arch="all"
license="ISC"
makedepends="
	autoconf
	automake
	hiredis-dev
	libevent-dev
	libpq-dev
	libtool
	mariadb-connector-c-dev
	sqlite-dev
	"
subpackages="$pkgname-doc"
source="https://github.com/OpenSMTPD/$_pkgname/archive/$pkgver/opensmtpd-extras-$pkgver.tar.gz
	0001-uint8_t-is-defined-in-stdint.h-at-least-on-Linux.patch
	0002-wrap-stdint.h-in-autoconf-variables.patch
	remove-decls.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"
options="!check"  # upstream does not provide tests

# secfixes:
#   6.6.4-r0:
#     - CVE-2020-8794

_extras="
	table-ldap
	table-mysql
	table-passwd
	table-postgres
	table-redis
	table-socketmap
	table-sqlite
	table-stub
	"
for _extra in $_extras; do
	subpackages="$subpackages opensmtpd-$_extra:_package_extra"
	depends="$depends opensmtpd-$_extra"
done

prepare() {
	default_prepare
	./bootstrap
}

build() {
	local with_extras
	local extra; for extra in $_extras; do
		with_extras="$with_extras --with-$extra"
	done

	./configure \
		--prefix=/usr \
		--sysconfdir=/etc/smtpd \
		--mandir=/usr/share/man \
		--libexecdir=/usr/lib \
		--with-pie \
		--with-mantype=man \
		$with_extras

	# the Redis table expects <hiredis.h> without subdirectory, so indulge it
	make REDIS_CPPFLAGS='-I /usr/include/hiredis'
}

package() {
	make DESTDIR="$pkgdir" install
}

_package_extra() {
	local name="${subpkgname#opensmtpd-}"
	pkgdesc="OpenSMTPD $name addon"
	depends=""

	mkdir -p "$subpkgdir"/usr/lib/opensmtpd
	mv "$pkgdir"/usr/lib/opensmtpd/$name "$subpkgdir"/usr/lib/opensmtpd/
}

sha512sums="3f7b3b050f2fb499a880dce7c6a89c2ff8332f9c32c4539fb12c63997263ba7b032b72b9937126654862e5fa81013acc176a7a84147bf027e99ea5c081519247  opensmtpd-extras-6.7.1.tar.gz
2e12845233437bef691ef92a2b4ffcc307b7cd72ec61b2063604034e28266550940ed432ef66a871fe82030df68b01cdd50ac97a255bf7724ab62a43d45ca4e5  0001-uint8_t-is-defined-in-stdint.h-at-least-on-Linux.patch
df6f52669e1df3d2b134fa8ec99795b0cef3f6aa38ccb5f85def240370b487988d6576fb769dee7d3eba30cfb291298b1c5a22433f4e6243307c589a7bbdb538  0002-wrap-stdint.h-in-autoconf-variables.patch
36efd3b6cf75728cc8b75b3d9d6bf464d1e949ccfbe6151ed53dbba0f9ee1e50eb61afcca05af302ab359bc9ea1136e7750a15e5f5b824899141298d3060782a  remove-decls.patch"
