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

_realname=unbound
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.3
pkgrel=1
pkgdesc="Validating, recursive, and caching DNS resolver (mingw-w64)"
arch=('any')
url='https://unbound.net/'
license=('custom:BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
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=('2b692b8311edfad41e7d0380aac34576060d4176add81dc5db419c79b2a4cecc'
            '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
}
