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

_realname=ldns
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.1
pkgrel=1
pkgdesc="Fast DNS library supporting recent RFCs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.nlnetlabs.nl/projects/ldns/'
license=('custom:BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python")
depends=("${MINGW_PACKAGE_PREFIX}-openssl")
optdepends=("${MINGW_PACKAGE_PREFIX}-dnssec-anchors")
options=(strip staticlibs)
source=("https://www.nlnetlabs.nl/downloads/${_realname}/${_realname}-${pkgver}.tar.gz"{,.asc}
        ldns-1.6.17-relocate.patch)
sha256sums=('8ac84c16bdca60e710eea75782356f3ac3b55680d40e1530d7cea474ac208229'
            'SKIP'
            '29728dac1131c0605e2742d40c7d6165d3c90558bab1b699b08408d38b669a33')
validpgpkeys=('DC34EE5DB2417BCC151E5100E5F8F8212F77A498')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  plain "Extracting ${_realname}-${pkgver}.tar.gz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzvf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true
  
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/ldns-1.6.17-relocate.patch

  WANT_AUTOMAKE=latest autoreconf -fiv
}

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

  # for asctime_r(): https://github.com/msys2/MINGW-packages/issues/6714
  CFLAGS+=" -D_POSIX_C_SOURCE"
  CXXFLAGS+=" -D_POSIX_C_SOURCE"

  PYTHON=${MINGW_PREFIX}/bin/python \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var \
    --with-trust-anchor=${MINGW_PREFIX}/etc/trusted-key.key \
    --with-drill \
    --with-examples \
    --with-ssl=${MINGW_PREFIX} \
    --without-pyldns \
    --without-pyldnsx \
    --disable-dane-verify \
    ac_cv_func_inet_ntop=yes \
    ac_cv_func_inet_pton=yes

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
