# SPDX-License-Identifier: GPL-3.0-or-later
# Maintainer: Knot Resolver team <knot-resolver@labs.nic.cz>

pkgname=knot-resolver
pkgver={{ version }}
pkgrel={{ release }}
pkgdesc='Caching DNSSEC-validating DNS resolver'
arch=(x86_64 aarch64 armv7h)
url='https://www.knot-resolver.cz/'
license=('GPL-3.0-or-later')

depends=(
    dnssec-anchors
    gnutls
    jemalloc
    knot
    libcap-ng
    libnghttp2
    libuv
    lmdb
    luajit
    python
    python-aiohttp
    python-jinja
    python-yaml
    supervisor
    systemd
)

makedepends=(
    cmocka
    meson
    pkgconf
    python-build
    python-installer
    python-setuptools
    python-wheel
)

optdepends=(
    'lua51-basexx: experimental_dot_auth module'
    'lua51-cqueues: http and dns64 module, policy.rpz() function'
    'lua51-http: http and prefill modules, trust_anchors bootstrap'
    'lua51-psl: policy.slice_randomize_psl() function'
    'python-prometheus_client: Prometheus metrics support'
    'python-watchdog: files changes monitoring'
)

backup=('etc/knot-resolver/config.yaml')

options=(debug)

source=("knot-resolver-${pkgver}.tar.xz")
sha256sums=('SKIP')

build() {
    local meson_options=(
        --buildtype=plain
        --prefix=/usr
        --sbindir=bin
        -Dkeyfile_default=/etc/trusted-key.key
        -Dmalloc=jemalloc
        -Dsystemd_files=enabled
        -Dunit_tests=enabled
    )

    arch-meson \
        "${srcdir}/${pkgname}-${pkgver}" \
        build \
        "${meson_options[@]}"

    meson compile -C build

    # Build the Python wheel
    python -m build \
        --wheel \
        --no-isolation \
        "${srcdir}/${pkgname}-${pkgver}"
}

check() {
    meson test -C build --print-errorlogs
}

package() {
    meson install -C build --destdir "${pkgdir}"

    # Install the Python wheel
    python -m installer \
        --destdir "${pkgdir}" \
        "${srcdir}/${pkgname}-${pkgver}"/dist/*.whl

    # Install the default configuration
    install -Dm644 \
        "${srcdir}/${pkgname}-${pkgver}/etc/config/config.yaml" \
        "${pkgdir}/etc/knot-resolver/config.yaml"

    # Remove the optional module requiring unavailable dependencies
    rm -f "${pkgdir}/usr/lib/knot-resolver/kres_modules/etcd.lua"
}
