# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Jonathan Curran <jonathan@curran.in>
# Contributor: Shiz <hi@shiz.me>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=opensmtpd
pkgver=7.7.0_p0
pkgrel=0
pkgdesc="Secure, reliable, lean, and easy-to configure SMTP server"
url="https://www.opensmtpd.org/"
arch="all"
license="ISC"
depends="!postfix ca-certificates"
makedepends="
	bison
	db-dev
	flex-dev
	libevent-dev
	linux-pam-dev
	mandoc
	musl-fts-dev
	openssl-dev
	zlib-dev
	"
install="$pkgname.pre-install"
subpackages="$pkgname-dbg $pkgname-pam $pkgname-openrc $pkgname-doc"
source="https://www.opensmtpd.org/archives/opensmtpd-${pkgver//_/}.tar.gz
	smtpd.conf.patch
	smtpd.initd
	smtpd.confd
	aliases
	"
options="suid !check"
builddir=$srcdir/opensmtpd-${pkgver//_/}

# secfixes:
#   6.6.2p1-r0:
#     - CVE-2020-7247

prepare() {
	default_prepare
	cp -ar "$builddir" "$builddir-pam"
}

build() {
	_build

	cd "$builddir-pam"
	_build --with-auth-pam
}

_build() {
	CFLAGS="$CFLAGS -flto=auto -I/usr/include/openssl" \
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc/smtpd \
		--mandir=/usr/share/man \
		--libexecdir=/usr/lib \
		--with-path-mbox=/var/mail \
		--with-path-pidfile=/run \
		--with-path-socket=/run \
		--with-table-db \
		--with-user-queue=smtpq \
		--with-group-queue=smtpq \
		--with-user-smtpd=smtpd \
		--with-path-socket=/run \
		--with-pie \
		--with-path-CAfile=/etc/ssl/certs/ca-certificates.crt \
		--with-mantype=doc \
		"$@"
	make
}

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

	install -Dm755 "$srcdir"/smtpd.initd "$pkgdir"/etc/init.d/smtpd
	install -Dm644 "$srcdir"/smtpd.confd "$pkgdir"/etc/conf.d/smtpd
	install -Dm644 "$srcdir"/aliases "$pkgdir"/etc/smtpd/aliases

	# Create compatibility symlinks for mailq, newaliases, makemap and sendmail.
	local binary; for binary in mailq newaliases makemap sendmail; do
		ln -s smtpctl "$pkgdir"/usr/sbin/$binary
	done

	install -Dm755 "$builddir-pam"/mk/smtpd/smtpd "$pkgdir"/usr/sbin/smtpd-pam
}

pam() {
	pkgdesc="OpenSMTPD with PAM support"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/sbin/smtpd-pam
}

sha512sums="
6a46a93bc0689603250376e414fc481fd5997c2bc7fc4e12d1522a9f4a2cad610f1c9452e9501b077e3ba2219a253e693160540fcfba595da8f68349bf795b92  opensmtpd-7.7.0p0.tar.gz
3d9db98cc9f84afb76d2588c696573c3a508ca38301c60b7e2d9470efbf3efb004a4fb65e2d7910de4a73f06121bece86208473d8aabdecdcf6d3e3b05be123c  smtpd.conf.patch
cce0c3b014a02d46c77d4de6495cf8e7e48d17c89c27432f121060d6712ae3606a6e5d51a74cf5504e826f7dd72176297dc83c9e6623f8e3fe9a952c8d02add1  smtpd.initd
e68fca4a7e0ceda271ad61c5a6592a859789bea9ccb6417258f7a0b45d92163ed6097c208d3fdfb78bf978a6a01b6f3678e047e3ce972b2c521419d54a992e0a  smtpd.confd
51d47b34eb3d728daa45f29d6434cc75db28dfa69b6fb3ecd873121df85b296a2d2c81016d765a07778aa26a496e4b29c09a30b82678cf42596a536734b5deca  aliases
"
