# 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}")
pkgver=2.1.28
pkgrel=3
pkgdesc="Cyrus Simple Authentication Service Layer (SASL) library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.cyrusimap.org/sasl/"
msys2_references=(
  "cpe: cpe:/a:carnegie_mellon_university:cyrus-sasl"
  "cpe: cpe:/a:cyrus:sasl"
  "cpe: cpe:/a:cyrusimap:cyrus-sasl"
)
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
options=('!makeflags')
source=(https://github.com/cyrusimap/${_realname}/releases/download/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.gz{,.sig}
        pathtools.c
        pathtools.h
        04-manpage-paths.patch
        16-MinGW-w64-define-WIN32_LEAN_AND_MEAN-avoiding-handle_t-redef.patch
        19-paths-relocation.patch
        20-mingw-tchar.patch
        21-fix-getopt-guard.patch
        22-autoconf-prevent-full-path-resolution.patch
        23-Fix-building-digest-plugin.patch)
sha256sums=('7ccfc6abd01ed67c1a0924b353e526f1b766b21f42d4562ee635a8ebfc5bb38c'
            'SKIP'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            '432c24477aa527c1fdb42977797054ee1a541597448d07f8c0201909b3adaf8c'
            'e101e1bd3ee03650e89e05c18d5f699d2f69aecc4f7aac4b11db2d1755783c5c'
            'b55f1444f5c2b9f3ac252867c89de692dd69e82bff3f843f6340adea31e04d09'
            '57b1b40017fa2a3fb189deedc01959673ed96cc01e088fd85ae0d59e1e13f25f'
            'f51412367d893cb4c2e4ee2ec76a6a641abaaf6d3086af93a9b579a8d0ce26c5'
            '8595a5b83195cd950ab9f10d224b00686d8a84bb161f6aa7770a64e2c786c231'
            '7e3657f314a0af007f6de4f7b6eb7fdc8c850c81bb0f2c3b462b69f0acc18e4f')
validpgpkeys=('829F339F8C296FE80F409D93E3D7C118C7B9F46A'  # Partha Susarla <mail@spartha.org>
              'DEA1999F0CDB1AAEBA001E0DBEE3E3B4D2F06546') # Quanah Gibson-Mount <quanah@fast-mail.org>

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] lib/

  apply_patch_with_msg \
    04-manpage-paths.patch \
    16-MinGW-w64-define-WIN32_LEAN_AND_MEAN-avoiding-handle_t-redef.patch \
    19-paths-relocation.patch \
    20-mingw-tchar.patch \
    21-fix-getopt-guard.patch \
    23-Fix-building-digest-plugin.patch

  # Partially revert upstream b017b437bef7b329097945172b8516d434754d41 commit.
  apply_patch_with_msg \
    22-autoconf-prevent-full-path-resolution.patch

  # For clang
  sed -i "s/-fPIC //g" common/Makefile.am sasldb/Makefile.am

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

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # 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}-${pkgver}/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 \
    --enable-shared \
    --disable-static \
    --with-sqlite3=${MINGW_PREFIX} \
    --disable-ldapdb \
    --oldincludedir=${MINGW_PREFIX}/include \
    --without-saslauthd \
    --without-pwcheck \
    --without-des \
    --without-authdaemond \
    --with-dblib=gdbm \
    --disable-sample

  make
}

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

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

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