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

_realname=unbound
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.13.0
pkgrel=1
pkgdesc="Validating, recursive, and caching DNS resolver (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://unbound.net/'
license=('custom:BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libtool")
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         #"${MINGW_PACKAGE_PREFIX}-libevent"
         "${MINGW_PACKAGE_PREFIX}-ldns")
options=(strip staticlibs)
source=("https://unbound.net/downloads/${_realname}-${pkgver}.tar.gz"
        'manifest')
sha256sums=('a954043a95b0326ca4037e50dace1f3a207a0a19e9a4a22f4c6718fc623db2a1'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855')

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

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  cp -f ${srcdir}/manifest anchor-update.exe.manifest
  libtool=${MINGW_PREFIX}/bin/libtool ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-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
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  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
}
