# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=cyrus-sasl
pkgver=2.1.26
pkgrel=12
pkgdesc="Cyrus Simple Authentication Service Layer (SASL)"
url="https://cyrusimap.org/"
arch="all"
license="custom"
options="!check"  # No test suite.
subpackages="$pkgname-dev $pkgname-doc $pkgname-gssapi $pkgname-gs2
	$pkgname-scram $pkgname-ntlm $pkgname-crammd5 $pkgname-digestmd5
	libsasl $pkgname-openrc"
depends=
makedepends="db-dev libressl-dev heimdal-dev
	autoconf automake libtool"
source="ftp://ftp.cyrusimap.org/$pkgname/$pkgname-$pkgver.tar.gz
	saslauthd.initd
	cyrus-sasl-2.1.25-avoid_pic_overwrite.patch
	cyrus-sasl-2.1.26-size_t.patch
	CVE-2013-4122.patch
	"

# secfixes:
#   2.1.26-r7:
#   - CVE-2013-4122

_builddir="$srcdir"/$pkgname-$pkgver
prepare() {
	cd "$_builddir"
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done

	# the libtool they ship is broken
	sed 's/AM_CONFIG_HEADER/AC_CONFIG_HEADERS/' -i configure.in || return 1
	rm -rf config/config.guess config/config.sub config/ltconfig \
		config/ltmain.sh config/libtool.m4 autom4te.cache
	libtoolize -c && aclocal -I config -I cmulocal \
		&& automake -a -c && autoheader && autoconf
}

build() {
	cd "$_builddir"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--disable-anon \
		--enable-cram \
		--enable-digest \
		--enable-login \
		--enable-ntlm \
		--disable-otp \
		--enable-plain \
		--with-gss_impl=heimdal \
		--with-devrandom=/dev/urandom \
		--without-ldap \
		--with-saslauthd=/var/run/saslauthd \
		--mandir=/usr/share/man \
		|| return 1
	# parallell builds is broken
	make -j1 || return 1
}

package() {
	cd "$srcdir"/cyrus-sasl-$pkgver
	make -j1 DESTDIR="$pkgdir" install || return 1
	install -D -m644 COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING

	install -Dm755 ../saslauthd.initd "$pkgdir"/etc/init.d/saslauthd
	install -d "$pkgdir"/var/run/saslauthd
}

_plugindir=usr/lib/sasl2
_plugin() {
	depends=
	replaces="libsasl"
	pkgdesc="Cyrus SASL plugin for $1"
	mkdir -p "$subpkgdir"/$_plugindir || return 1
	mv "$pkgdir"/$_plugindir/lib${1}.so* "$subpkgdir"/$_plugindir/
}

gssapi() { _plugin gssapiv2; }
gs2() { _plugin gs2; }
scram() { _plugin scram; }
ntlm() { _plugin ntlm; }
crammd5() { _plugin crammd5; }
digestmd5() { _plugin digestmd5; }

libsasl() {
	depends=
	pkgdesc="Cyrus Simple Authentication and Security Layer (SASL) library"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

md5sums="a7f4e5e559a0e37b3ffc438c9456e425  cyrus-sasl-2.1.26.tar.gz
6e7cbe301015777bf53d5f08ac4362f0  saslauthd.initd
085acdc345bcce896f3eea8956cc0892  cyrus-sasl-2.1.25-avoid_pic_overwrite.patch
bcaafcbc79054e8356217213d6eda16d  cyrus-sasl-2.1.26-size_t.patch
8b3f65a7c8fbcbd7b7da2865f71b8aa7  CVE-2013-4122.patch"
sha256sums="8fbc5136512b59bb793657f36fadda6359cae3b08f01fd16b3d406f1345b7bc3  cyrus-sasl-2.1.26.tar.gz
d6d23c360d52cf35bf266ce32b7c0eccafd79f55daa3e97733a899c97211a90c  saslauthd.initd
80cb9cf22b0507b503ff0cf6c5946a44eb5c3808e0a77e66d56d5a53e5e76fa7  cyrus-sasl-2.1.25-avoid_pic_overwrite.patch
b85b20bdd25b42098e07a8ba7e435f02b5cd882dcf69572c4d32de4a5e4f41bb  cyrus-sasl-2.1.26-size_t.patch
e32013e7ba1d9a80c18524a413f3b3c4bfc325e1c07b1552908b631edb803346  CVE-2013-4122.patch"
sha512sums="78819cb9bb38bea4537d6770d309deeeef09ff44a67526177609d3e1257ff4334d2b5e5131d5a1e4dea7430d8db1918ea9d171f0dee38b5e8337f4b72ed068f0  cyrus-sasl-2.1.26.tar.gz
71a00a22f91f0fb6ba2796acede321a0f071b1d7a99616f0e36c354213777f30575c340b6df392dcbfc103ba7640d046144882f6a7b505f59709bb5c429b44d8  saslauthd.initd
033e3634116e1d3b316052dbe0b671cca0fcfb6063fca1a97d990c422c2ce05109a1e424e84ed9928dc0312a325a7248f2d2e3f9547f84453b36331c01f63be5  cyrus-sasl-2.1.25-avoid_pic_overwrite.patch
fe4c3e6d5230eb50b9e6885129760a12e7bce316b41a3e58b2c550fa83526b91205cd827f7d1367751313559875d32982b95b024b1a22300ac5b35214e7c2b78  cyrus-sasl-2.1.26-size_t.patch
08964bc3ad713e137b8f05f9bac345d79676d14784bc37525f195e8e2a3e6740428237b64f7eeeacc0c71ed6cf1664c6e9c2267ac6df327761d92174a1853744  CVE-2013-4122.patch"
