# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=(cyrus-sasl libsasl libsasl-devel)
_basever=2.1.28
pkgver=${_basever}
pkgrel=5
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library"
arch=('i686' 'x86_64')
url="https://www.cyrusimap.org/sasl/"
msys2_repository_url="https://github.com/cyrusimap/cyrus-sasl"
msys2_references=(
  "cpe: cpe:/a:carnegie_mellon_university:cyrus-sasl"
  "cpe: cpe:/a:cyrus:sasl"
  "cpe: cpe:/a:cyrusimap:cyrus-sasl"
)
license=('custom')
groups=('sys-utils')
makedepends=('heimdal-devel' 'openssl-devel' 'libsqlite-devel' 'libxcrypt-devel' 'libserf-devel' 'autotools' 'gcc')
options=('emptydirs' 'strip' '!makeflags')
source=(https://github.com/cyrusimap/cyrus-sasl/releases/download/cyrus-sasl-2.1.28/cyrus-sasl-2.1.28.tar.gz{,.sig}
        01-fix-plugin-ext.patch
        03-fix-plugins.patch
        09-digest-md5-Remove-annoying-debug-message.patch)
sha256sums=('7ccfc6abd01ed67c1a0924b353e526f1b766b21f42d4562ee635a8ebfc5bb38c'
            'SKIP'
            '3567f22ee5f4dc4174c4d47b10287f84992fb3dee8fd5fa9e5cbb34541b99b9f'
            '62fca7407d288e021fac6fed47d8a94ef2775c257ddcb79071428fe7719485f5'
            '8eec9903e048bf01633df93cea505a8f77aceb933dd5609fd85602c0c0baa5c0')
validpgpkeys=(
  '829F339F8C296FE80F409D93E3D7C118C7B9F46A'  # Partha Susarla <mail@spartha.org>
  'DEA1999F0CDB1AAEBA001E0DBEE3E3B4D2F06546'  # Quanah Gibson-Mount <quanah@fast-mail.org>
)

prepare() {
  cd ${srcdir}/${pkgname}-${_basever}

  patch -p1 -i ${srcdir}/01-fix-plugin-ext.patch
  patch -p1 -i ${srcdir}/03-fix-plugins.patch
  patch -p1 -i ${srcdir}/09-digest-md5-Remove-annoying-debug-message.patch

  autoreconf -fiv
}

build() {
  cd ${srcdir}/${pkgname}-${_basever}

  ./configure \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --build=${CHOST} \
    --host=${CHOST} \
    --with-configdir=/etc/sasl2:/etc/sasl:/usr/lib/sasl2 \
    --with-plugindir=/usr/lib/sasl2 \
    --disable-static --enable-shared \
    --enable-sql \
    --with-sqlite3=/usr \
    --disable-ldapdb \
    sasl_cv_dlsym_adds_uscore=yes

  make CFLAGS="$CFLAGS -Wall -Wno-char-subscripts -Wno-unused -Wno-pointer-sign -Wno-uninitialized"
}

package_cyrus-sasl() {
  depends=("libsasl=${pkgver}")
  pkgdesc="Cyrus saslauthd SASL authentication daemon"

  cd cyrus-sasl-${_basever}/saslauthd
  make DESTDIR="${pkgdir}" install

  install -m755 -d "${pkgdir}/usr/share/licenses/cyrus-sasl"
  cp -pR ../COPYING "${pkgdir}/usr/share/licenses/cyrus-sasl/"
}

package_libsasl() {
  groups=('libraries')
  options=('emptydirs' 'strip' 'libtool')
  pkgdesc="Cyrus Simple Authentication Service Layer (SASL) Library"
  depends=('libxcrypt' 'libopenssl' 'heimdal-libs' 'libsqlite')

  cd cyrus-sasl-${_basever}
  for dir in include lib sasldb plugins utils; do
    pushd ${dir}
    make DESTDIR="${pkgdir}" install
    popd
  done
  rm -f "${pkgdir}"/usr/lib/sasl2/*.a
  rm -f "${pkgdir}"/usr/lib/sasl2/*.la
  rm -f "${pkgdir}"/usr/lib/*.a
  rm -f "${pkgdir}"/usr/lib/*.la
  rm -rf "${pkgdir}"/usr/include
  install -m755 -d "${pkgdir}/usr/share/licenses/libsasl"
  install -m644 COPYING "${pkgdir}/usr/share/licenses/libsasl/"
}

package_libsasl-devel() {
  pkgdesc="Libsasl headers and libraries"
  groups=('development')
  depends=("libsasl=${pkgver}" 'heimdal-devel' 'openssl-devel' 'libsqlite-devel' 'libxcrypt-devel')
  options=('strip' 'libtool')

  cd cyrus-sasl-${_basever}
  for dir in include lib; do
    pushd ${dir}
    make DESTDIR="${pkgdir}" install
    popd
  done
  rm -rf "${pkgdir}"/usr/bin
}
