# Packager = Tuxcore <dev@tuxcore.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=5
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library"
arch=(x86_64)
url="https://www.cyrusimap.org/sasl/"
_url=https://github.com/cyrusimap/cyrus-sasl
license=(BSD-3-Clause-Attribution)
depends=(glibc)
makedepends=(
  gdbm
  git
  krb5
  libldap
  mariadb-libs
  openssl
  postgresql-libs
  sqlite
)
provides=(libsasl2.so)
source=(
  $_name::git+$_url.git?signed#tag=$_name-$pkgver
)
sha512sums=('a055876910be34d59c6e1c8891244975d213490ba6aa0924e5f018094f3ef01871f627b076d6a6ad9547f77ad528f0ac20b3c0efd391cac97bb9e024de037271')
b2sums=('c4d417139a81ffb1ade5456ecd14be77bd2e0ff0e59cf818206dc3df78a4d4be1ee5b0988d3a3edbb1542041b9f3b921d855075df5d86b0458b99020192170d1')
validpgpkeys=(
  '829F339F8C296FE80F409D93E3D7C118C7B9F46A'  # Partha Susarla <mail@spartha.org>
  'DEA1999F0CDB1AAEBA001E0DBEE3E3B4D2F06546'  # Quanah Gibson-Mount <quanah@fast-mail.org>
)

prepare() {
  local commit
  local cherry_picks=(
    2d2e97b0eb53fa7f87a3bf1529d8f712dd954480  # fix for CVE-2022-24407
    266f0acf7f5e029afbb3e263437039e50cd6c262  # fix issues with time.h detection https://github.com/cyrusimap/cyrus-sasl/issues/859
    887dbc0435056ec58ee48c4d803f110ade1e4c39  # openssl 3.0 support: https://github.com/cyrusimap/cyrus-sasl/pull/653
    dfaa62392e7caecc6ecf0097b4d73738ec4fc0a8  # openssl 3.0 support: https://github.com/cyrusimap/cyrus-sasl/pull/653
    c2bd3afbca57f176d8c650670ce371444bb7fcc0  # openssl 3.0 support: https://github.com/cyrusimap/cyrus-sasl/pull/653
  )

  cd $_name
  for commit in "${cherry_picks[@]}"; do
    git cherry-pick -n "$commit"
  done

  autoreconf -fiv
}

build() {
  local configure_options=(
    --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
    --prefix=/usr
    --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
  )

  cd $_name
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking by libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

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

package() {
  depends+=(
    gdbm libgdbm.so
    openssl libcrypto.so
  )

  local _target
  make DESTDIR="$pkgdir" install-pkgconfigDATA -C $_name
  for _target in include lib sasldb plugins utils; do
    make DESTDIR="$pkgdir" install -C $_name/$_target
  done
  install -vDm 644 $_name/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*
}
