# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           legacysupport 1.1

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                knot
version             3.5.7
revision            0

categories          net
license             GPL-3+
maintainers         {mps @Schamschula} openmaintainer
description         Knot DNS is a high-performance authoritative-only DNS server which \
                    supports all key features of the modern domain name system.
long_description    {*}${description}
homepage            https://www.knot-dns.cz
master_sites        https://secure.nic.cz/files/knot-dns/ \
                    https://sources.openwrt.org

use_xz              yes

checksums           rmd160  39888813da5e481ace70db7037effbd508b3e10e \
                    sha256  0e363e9160895e2b83d02b0a7488c3a566b605b92edac85d03be3ebce94b8214 \
                    size    1735896

depends_build       port:pkgconfig

depends_lib         port:fstrm \
                    path:lib/pkgconfig/gnutls.pc:gnutls \
                    port:libidn2 \
                    port:lmdb \
                    port:nghttp2 \
                    port:ngtcp2 \
                    port:protobuf-c \
                    port:userspace-rcu

# Should match knot-resolver port
platforms {darwin >= 11}

# error: address argument to atomic operation must be a
# pointer to non-const _Atomic type on macOS <10.14
# Uses C17 atomics.
compiler.c_standard 2017

patchfiles-append   patch-src-knot-server-quic-handler.c.diff

# https://github.com/CZ-NIC/knot/pull/35
# TCP Fast Open API support was implemented in macOS 10.11.
# Despite the correctness of the compatibility patch, it
# wasn't be added to upstream.
if {${os.platform} eq "darwin" && ${os.major} < 16} {
    patchfiles-append \
                    patch-for-macos-10.11-and-older.diff
}

post-patch {
    reinplace "s|task_t|k_task_t|g" \
        ${worksrcpath}/src/knot/events/events.c \
        ${worksrcpath}/src/knot/events/events.h \
        ${worksrcpath}/src/knot/worker/pool.c \
        ${worksrcpath}/src/knot/worker/queue.c \
        ${worksrcpath}/src/knot/worker/queue.h \
        ${worksrcpath}/tests/knot/test_worker_pool.c \
        ${worksrcpath}/tests/knot/test_worker_queue.c
}

configure.args      --disable-silent-rules

subport libknot {
    conflicts       ${name}

    configure.args-append \
                    --disable-daemon \
                    --disable-modules \
                    --disable-utilities 
} 

if {${name} eq ${subport}} {
    conflicts       libknot

    startupitem.create      yes
    startupitem.netchange   yes
    startupitem.executable  ${prefix}/sbin/knotd -c ${prefix}/etc/knot/knot.conf

    notes "
To complete the installation, run:

sudo cp ${prefix}/etc/knot/knot.sample.conf ${prefix}/etc/knot/knot.conf

Edit as necessary. (See the knot.conf manpage for additional information.)

You will also need to created *.zone files. See ${prefix}/etc/knot/example.com.zone.
"
}
