# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>

# NOTE: upgrade libsasl in tandem with extra/cyrus-sasl
# As one PKGBUILD can (currently) not provide packages for several
# repositories, libsasl and the rest of cyrus-sasl are provided separately
# (else this would require mariadb and postgresql in [core])

_name=cyrus-sasl
pkgname=libsasl
pkgver=2.1.28
pkgrel=3
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library"
arch=(x86_64)
url="https://www.cyrusimap.org/sasl/"
license=(custom)
depends=(gdbm libgdbm.so glibc openssl)
makedepends=(krb5 libldap mariadb-libs postgresql-libs sqlite)
provides=(libsasl2.so)
options=(debug)
source=(https://github.com/cyrusimap/$_name/releases/download/$_name-$pkgver/$_name-$pkgver.tar.gz{,.sig})
sha512sums=('db15af9079758a9f385457a79390c8a7cd7ea666573dace8bf4fb01bb4b49037538d67285727d6a70ad799d2e2318f265c9372e2427de9371d626a1959dd6f78'
            'SKIP')
b2sums=('6cca8c26cebb9c2ee5d539c43797d30b6309a476ec4233225789978e1d7315c4ea5d2abbc7f5464be0f3c0de5fd9212706b43fbc92f40b76cd0b1013cc00f823'
        'SKIP')
validpgpkeys=(
  '829F339F8C296FE80F409D93E3D7C118C7B9F46A'  # Partha Susarla <mail@spartha.org>
  'DEA1999F0CDB1AAEBA001E0DBEE3E3B4D2F06546'  # Quanah Gibson-Mount <quanah@fast-mail.org>
)

prepare() {
  cd $_name-$pkgver
  autoreconf -fiv
}

build() {
  cd $_name-$pkgver

  ./configure --prefix=/usr \
      --disable-krb4 \
      --disable-macos-framework \
      --disable-otp \
      --disable-passdss \
      --disable-srp \
      --disable-srp-setpass \
      --disable-static \
      --enable-alwaystrue \
      --enable-anon \
      --enable-auth-sasldb \
      --enable-checkapop \
      --enable-cram \
      --enable-digest \
      --enable-gssapi \
      --enable-ldapdb \
      --enable-login \
      --enable-ntlm \
      --enable-plain \
      --enable-shared \
      --enable-sql \
      --infodir=/usr/share/info \
      --mandir=/usr/share/man \
      --sbin=/usr/bin \
      --sysconfdir=/etc \
      --with-dblib=gdbm \
      --with-devrandom=/dev/urandom \
      --with-configdir=/etc/sasl2:/etc/sasl:/usr/lib/sasl2 \
      --with-ldap \
      --with-mysql=/usr \
      --with-pam \
      --with-pgsql=/usr/lib \
      --with-saslauthd=/var/run/saslauthd \
      --with-sqlite3=/usr/lib

  # prevent excessive overlinking by libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  make -k check -C $_name-$pkgver
}

package() {
  local _target
  make DESTDIR="$pkgdir" install-pkgconfigDATA -C $_name-$pkgver
  for _target in include lib sasldb plugins utils; do
    make DESTDIR="$pkgdir" install -C $_name-$pkgver/$_target
  done
  install -vDm 644 $_name-$pkgver/COPYING -t "$pkgdir/usr/share/licenses/$pkgname/"
  # remove files provided by extra/cyrus-sasl
  rm -fv "$pkgdir"/usr/lib/sasl2/lib{gs2,gssapiv2,ldapdb,sql}.so*
}
