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

_realname=ldns
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.3
pkgrel=3
pkgdesc="Fast DNS library supporting recent RFCs (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.nlnetlabs.nl/projects/ldns/'
msys2_repository_url="https://github.com/NLnetLabs/ldns"
msys2_references=(
  "cpe: cpe:/a:nlnetlabs:ldns"
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
depends=("${MINGW_PACKAGE_PREFIX}-openssl")
optdepends=("${MINGW_PACKAGE_PREFIX}-dnssec-anchors")
source=("https://www.nlnetlabs.nl/downloads/${_realname}/${_realname}-${pkgver}.tar.gz"{,.asc}
        001-include-missing-header-ws2tcpip.patch
        pathtools.c
        pathtools.h
        ldns-1.6.17-relocate.patch)
sha256sums=('c3f72dd1036b2907e3a56e6acf9dfb2e551256b3c1bbd9787942deeeb70e7860'
            'SKIP'
            '2b7f5a1c9982134f69154cb7445ac16eae1170a86c1d631d56fb9a135c44ec0d'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            '5384bc9ed57a2b44999e2c850318001c36809a42e9f3117d42c83911540dd8d0')
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 -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true

  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] ./

  patch -p1 -i "${srcdir}"/ldns-1.6.17-relocate.patch

  patch -p1 -i "${srcdir}"/001-include-missing-header-ws2tcpip.patch

  WANT_AUTOMAKE=latest autoreconf -fiv
}

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

  PYTHON=${MINGW_PREFIX}/bin/python \
  ../${_realname}-${pkgver}/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-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-${MSYSTEM}"
  make -j1 DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
