# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

# There's an ugliness issue in cyrus-sasl's build system.
# It builds libsasl2.dll with some compat objects, somehow
# gotten from saslauthd/Makefile.am?! but these files -
# getaddrinfo.c, getnameinfo.c (and one that doesn't ever
# get built, snprintf.c) depend on ws2_32 on Windows.
# The plugins need these objects too, so they are LN_S'ed
# as COMPAT_OBJS and that's added to plugins/Makefile.am:
# lib{plain,anonymous,kerberos4...}_la_{DEPENDENCIES,LIBADD}
# .. problem with doing it this way is that the ws2_32
# (encoded into LIB_SOCKET) dependency needs to be specified
# repeatedly for each one ..
# Instead these compat objects should be linked into a
# convenience libtool library (.a compiled with -fPIC) that
# carries the ws2_32 dependency with it, and this libtool
# library should be added as a dependency to each of the
# plugins. I've not fixed this because I've not got enough
# libtool experience to do it yet so instead 12-MinGW-w64..patch
# adds LIB_SOCKET all over the place in plugins/Makefile.am

_realname=cyrus-sasl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_rc=rc8
_basever=2.1.27
pkgver=${_basever}.${_rc}
pkgrel=1
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library"
arch=('any')
url="https://www.cyrusimap.org/sasl/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
options=('emptydirs' 'strip' '!makeflags')
source=(https://cyrusimap.org/releases/${_realname}-${_basever}-${_rc}.tar.gz
        02-exeext.patch
        03-fix-plugins.patch
        04-manpage-paths.patch
        14-MinGW-w64-add-LIBSASL_API-to-function-definitions.patch
        15-MinGW-w64-define-LIBSASL_EXPORTS_eq_1-for-sasldb.patch
        16-MinGW-w64-define-WIN32_LEAN_AND_MEAN-avoiding-handle_t-redef.patch
        17-MinGW-w64-define-S_IRUSR-and-S_IWUSR.patch
        19-paths-relocation.patch)
sha256sums=('8d95201b4f2c2ec4c0ebafd01c00d7d1e0f2513352b3f850ae2723a90c6c6789'
            '0d0220f721b07f821e14f00c04250c2243620301200f825e7f0532b581d63a7d'
            '62fca7407d288e021fac6fed47d8a94ef2775c257ddcb79071428fe7719485f5'
            'a4abc51b2509e0792f7dc019afcb0bf6dacfdcf1132d28cf7562bbb18828a8fc'
            'c40ab13f6f9a6ea1dcd602e308a51caf162f9853821123a3f8fe88897eda3b98'
            'c7ad8c4e90e130b34ec66ff4056b9bd0ea98144f16552dc0a23387fc18a001b2'
            '5b0104ec5fd190c7c6bd5b8d49c76e9c02609e449b499c0382cacd0e0f5032dd'
            'd058e921d5b7b292371cff2e0334c067bfb8ce0ba85e82726b18961ae2e4f509'
            'bd65e6b0705b5b7c81024435a386e6818aebc73e7681ceb4b8ca398f50661708')

prepare() {
  cd ${srcdir}/${_realname}-${_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}/14-MinGW-w64-add-LIBSASL_API-to-function-definitions.patch
  patch -p1 -i ${srcdir}/15-MinGW-w64-define-LIBSASL_EXPORTS_eq_1-for-sasldb.patch
  patch -p1 -i ${srcdir}/16-MinGW-w64-define-WIN32_LEAN_AND_MEAN-avoiding-handle_t-redef.patch
  patch -p1 -i ${srcdir}/17-MinGW-w64-define-S_IRUSR-and-S_IWUSR.patch
  patch -p1 -i ${srcdir}/19-paths-relocation.patch

  autoreconf -fiv
  cp -f ${srcdir}/${_realname}-${_basever}/win32/include/md5global.h ${srcdir}/${_realname}-${_basever}/include/md5global.h
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  # Useful ref:
  # https://fedorapeople.org/cgit/elmarco/public_git/mingw32-cyrus-sasl.git/tree/mingw32-cyrus-sasl.spec
  # --oldincludedir=${MINGW_PREFIX}/include is so that /usr/include isn't added by default.
  # --enable-sql seems to have problems with <mysql.h> not existing, I thought it would be a part of sqlite.
  # --enable-sql
  # --without-saslauthd because it needs sockaddr_un.

  # We disable OpenSSL (--without-openssl --without-des) so that applications
  # with licenses incompatible with OpenSSL (such as GPL applications) do not
  # end up linked with OpenSSL if they use and redistribute Cyrus SASL outside
  # MSYS2. See these links:
  #
  #     * https://www.openssl.org/support/faq.html#LEGAL2
  #     * https://en.wikipedia.org/wiki/OpenSSL#Licensing

  ../${_realname}-${_basever}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-configdir=${MINGW_PREFIX}/etc/sasl2:${MINGW_PREFIX}/etc/sasl:${MINGW_PREFIX}/lib/sasl2 \
    --with-plugindir=${MINGW_PREFIX}/lib/sasl2 \
    --disable-static --enable-shared \
    --with-sqlite3=${MINGW_PREFIX} \
    --disable-ldapdb \
    --oldincludedir=${MINGW_PREFIX}/include \
    --without-saslauthd \
    --without-pwcheck \
    --without-des \
    --without-authdaemond \
    --with-dblib=gdbm

  make
}

package() {
  local _plugindir=${MINGW_PREFIX}/lib/sasl2

  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install sasldir=${_plugindir}

  install -Dm644 ${srcdir}/${_realname}-${_basever}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
