# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=openldap
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.4.57
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="OpenLDAP Software is an open source implementation of the Lightweight Directory Access Protocol (only client) (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-libtool"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         #"${MINGW_PACKAGE_PREFIX}-db<6.0.20"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "groff"
             "make")
options=('strip' 'staticlibs' '!makeflags')
license=("The OpenLDAP Public License")
url="https://www.openldap.org/"
source=(https://www.openldap.org/software/download/OpenLDAP/openldap-release/${_realname}-${pkgver}.tgz
        001-backmdb.patch
        003-symdummy-error.patch)
sha256sums=('c7ba47e1e6ecb5b436f3d43281df57abeffa99262141aec822628bc220f6b45a'
            'b462bfab9362cf5cab61d67cd24902a1800fb498a152b681ee182c713b79146c'
            'd697fe51361a5129a5af3ca317031ff6f4ca5ddd247ff53043880660154a4708')

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/001-backmdb.patch
  patch -p1 -i ${srcdir}/003-symdummy-error.patch

  #libtoolize --force --copy
  #AUTOMAKE=/usr/bin/true autoreconf -fi
}

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

  unset CPPFLAGS
  export lt_cv_deplibs_check_method='pass_all'
  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --sbindir=${MINGW_PREFIX}/bin \
    --with-tls=openssl \
    --with-cyrus-sasl \
    --enable-modules=yes \
    --enable-hdb=no \
    --enable-bdb=no \
    CC=${MINGW_PREFIX}/bin/gcc

  sed -i "s/#define socklen_t int/\/*#define socklen_t int*\//" include/portable.h
  sed -i "s/#define snprintf _snprintf/\/*#define snprintf _snprintf*\//" include/portable.h
  make depend
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

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