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

pkgname=(cyrus-sasl libsasl libsasl-devel)
_basever=2.1.27
_rc=
pkgver=${_basever}${_rc//-/}
pkgrel=1
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library"
arch=('i686' 'x86_64')
url="http://cyrusimap.web.cmu.edu/"
license=('custom')
groups=('sys-utils')
makedepends=('heimdal-devel' 'openssl-devel' 'libsqlite-devel' 'libcrypt-devel' 'libserf-devel')
options=('emptydirs' 'strip' '!makeflags')
source=(#http://ftp.cyrusimap.org/cyrus-sasl/cyrus-sasl-${pkgver}.tar.gz
        https://ftp.osuosl.org/pub/blfs/conglomeration/cyrus-sasl/cyrus-sasl-${_basever}${_rc}.tar.gz
        02-exeext.patch
        03-fix-plugins.patch
        04-manpage-paths.patch
        09-digest-md5-Remove-annoying-debug-message.patch)
sha256sums=('26866b1549b00ffd020f188a43c258017fa1c382b3ddadd8201536f72efb05d5'
            'b32d9e333ec68b949a60805cc663589f868062bac4f59bbb7624149e88fc66a4'
            '62fca7407d288e021fac6fed47d8a94ef2775c257ddcb79071428fe7719485f5'
            'a4abc51b2509e0792f7dc019afcb0bf6dacfdcf1132d28cf7562bbb18828a8fc'
            '8eec9903e048bf01633df93cea505a8f77aceb933dd5609fd85602c0c0baa5c0')

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

  patch -p1 -i ${srcdir}/02-exeext.patch
  patch -p1 -i ${srcdir}/03-fix-plugins.patch
  patch -p1 -i ${srcdir}/04-manpage-paths.patch
  patch -p1 -i ${srcdir}/09-digest-md5-Remove-annoying-debug-message.patch

  # sed 's/AM_CONFIG_HEADER/AC_CONFIG_HEADERS/' -i configure.in
  # rm -f config/config.guess config/config.sub
  # rm -f config/ltconfig config/ltmain.sh config/libtool.m4
  # rm -fr autom4te.cache
  # libtoolize -c
  # aclocal -I config -I cmulocal
  # automake -a -c
  # autoheader
  # autoconf

  # pushd saslauthd
    # rm -f config/config.guess config/config.sub
    # rm -f config/ltconfig config/ltmain.sh config/libtool.m4
    # rm -fr autom4te.cache
    # libtoolize -c
    # aclocal -I config -I ../cmulocal -I ../config
    # automake -a -c
    # autoheader
    # autoconf
  # popd
  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"
  #backup=('etc/conf.d/saslauthd')

  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=('libcrypt' '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/*.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/"
  # pushd "${pkgdir}"/usr/lib/sasl2
    # for x in .dll; do
      # mv $x $(echo $x | sed e 's/^msys-/lib/' e 's/[0-9][0-9]\.dll$/.so/')
    # done
  # popd
}

package_libsasl-devel() {
  pkgdesc="Libsasl headers and libraries"
  groups=('development')
  depends=("libsasl=${pkgver}" 'heimdal-devel' 'openssl-devel' 'libsqlite-devel' 'libcrypt-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
}
