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

_realname=unbound
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.24.0
pkgrel=1
pkgdesc="Validating, recursive, and caching DNS resolver (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://unbound.net/'
msys2_repository_url='https://github.com/NLnetLabs/unbound'
msys2_references=(
  "archlinux: unbound"
  "cpe: cpe:/a:nlnetlabs:unbound"
  "cpe: cpe:/a:unbound:unbound"
  "gentoo: net-dns/unbound"
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "bison"
             "flex")
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         #"${MINGW_PACKAGE_PREFIX}-libevent"
         "${MINGW_PACKAGE_PREFIX}-ldns")
source=("https://unbound.net/downloads/${_realname}-${pkgver}.tar.gz"{,.asc}
        'manifest')
sha256sums=('147b22983cc7008aa21007e251b3845bfcf899ffd2d3b269253ebf2e27465086'
            'SKIP'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855')
validpgpkeys=(EDFAA3F2CA4E6EB05681AF8E9F6F1C2D7E045F8D) # W.C.A. Wijngaards <wouter@nlnetlabs.nl>

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
}

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

  cp -f "${srcdir}"/manifest anchor-update.exe.manifest

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var \
    --sbindir=${MINGW_PREFIX}/bin \
    --disable-rpath \
    --with-libevent=no \
    --with-libexpat=${MINGW_PREFIX} \
    --with-conf-file=${MINGW_PREFIX}/etc/unbound/unbound.conf \
    --without-pyunbound \
    --without-pythonmodule \
    --with-ssl=${MINGW_PREFIX}

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make test || true
}

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

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/doc/example.conf.in "${pkgdir}${MINGW_PREFIX}/etc/unbound/unbound.conf.example"
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  for ff in anchor-update unbound-service-install unbound-service-remove; do
    install -Dm755 ${ff}.exe "${pkgdir}${MINGW_PREFIX}/bin/"
    install -Dm644 "${srcdir}"/manifest "${pkgdir}"${MINGW_PREFIX}/bin/${ff}.exe.manifest
  done
}
