# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# strnlen
legacysupport.newest_darwin_requires_legacy 10
# lokinet-bootstrap(15734) malloc: *** error for object 0xa00a6754: pointer being freed was not allocated
legacysupport.redirect_bins lokinet lokinet-bootstrap lokinet-vpn

# TODO: we may need to switch upstream.
# https://github.com/session-foundation/session-router/issues/45
github.setup        oxen-io lokinet 0.9.14 v
revision            0

categories          net p2p
license             GPL-3
maintainers         {@barracuda156 macos-powerpc.org:barracuda}

description         Anonymous, decentralized and IP-based overlay network
long_description    ${name} is the reference implementation of LLARP \
                    (low latency anonymous routing protocol), a layer 3 onion routing protocol. \
                    The port is experimental and presently meant for development.
homepage            https://lokinet.org
# checksums           rmd160  b906e768ab075ec7355e2041a66a1efec830c277 \
#                     sha256  5a497f20c9646d85cf7c9e5fd77aad6d448b3fd378439c7aa0c1ecf0edf15851 \
#                     size    743883
# github.tarball_from archive

fetch.type          git

post-fetch {
    system -W ${worksrcpath} "git submodule update --init --recursive"
}

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:curl \
                    port:libsodium \
                    path:lib/pkgconfig/libuv.pc:libuv \
                    port:unbound

patchfiles-append   0001-CMakeLists-use-correct-flags-with-native-build-on-po.patch \
                    0002-Restore-support-for-VPN-on-Apple.patch \
                    0003-CMakeLists-minor-fixes-for-Apple.patch \
                    0004-llarp-add-missing-header-for-macOS.patch \
                    0005-fmt-do-not-install.patch \
                    0006-Use-prefix-for-datadir.patch

# libzmq_external/src/compat.hpp:64:22: error: 'size_t strnlen(const char*, size_t)'
# was declared 'extern' and later 'static' [-fpermissive]
patchfiles-append   0007-Fix-for-legacysupport.patch

patchfiles-append   0008-Hack-it-to-compile-for-now.patch \
                    0009-Add-missing-impl.patch

post-patch {
    reinplace "s|@PREFIX@|${prefix}|" ${worksrcpath}/llarp/constants/files.hpp
}

configure.args-append \
                    -DBUILD_DAEMON=ON \
                    -DBUILD_GUI=OFF \
                    -DBUILD_PACKAGE=OFF \
                    -DBUILD_SHARED_LIBS=OFF \
                    -DCODESIGN=OFF \
                    -DMACOS_DYNAMIC=OFF \
                    -DMACOS_LEGACY=ON \
                    -DNATIVE_BUILD=OFF \
                    -Dppc_support=ON \
                    -DSTRIP_SYMBOLS=OFF \
                    -DTESTNET=OFF \
                    -DUSE_AVX2=OFF \
                    -DUSE_JEMALLOC=OFF \
                    -DUSE_NETNS=OFF \
                    -DWARNINGS_AS_ERRORS=OFF \
                    -DWITH_BOOTSTRAP=ON \
                    -DWITH_COVERAGE=OFF \
                    -DWITH_EMBEDDED_LOKINET=OFF \
                    -DWITH_HIVE=OFF \
                    -DWITH_LIBCRYPT=OFF \
                    -DWITH_LTO=ON \
                    -DWITH_PEERSTATS=OFF \
                    -DWITH_TESTS=OFF \
                    -DXSAN=OFF

compiler.c_standard     1999
compiler.cxx_standard   2020

# ___atomic_fetch_add_8
# Included check fails to pass the flag.
if {[string match *gcc* ${configure.compiler}] &&
    ${configure.build_arch} in [list arm i386 ppc]} {
    configure.ldflags-append    -latomic
}

variant jemalloc description "Use JeMalloc" {
    depends_lib-append \
                    path:lib/pkgconfig/jemalloc.pc:jemalloc
    configure.args-replace \
                    -DUSE_JEMALLOC=OFF \
                    -DUSE_JEMALLOC=ON
}

# Not really supported.
variant dynamic description "Build dynamic libs, experimental" {
    depends_lib-append \
                    port:cppzmq \
                    port:cxxopts \
                    port:nlohmann-json \
                    port:oxen-logging \
                    port:oxenc \
                    port:oxenmq \
                    port:uvw \
                    path:lib/libzmq.dylib:zmq
    configure.args-replace \
                    -DBUILD_SHARED_LIBS=OFF \
                    -DBUILD_SHARED_LIBS=ON \
                    -DMACOS_DYNAMIC=OFF \
                    -DMACOS_DYNAMIC=ON
}

set realname        lokinet
set lokidir         ${prefix}/var/lib/${realname}

post-destroot {
    xinstall -d ${destroot}${prefix}/etc/${realname}
    copy ${worksrcpath}/contrib/bootstrap/mainnet.signed ${destroot}${prefix}/etc/${realname}/

    xinstall -d ${destroot}${prefix}/share/${realname}
    foreach item {bootstrap configs NetworkManager py lokinet-resolvconf} {
        move ${worksrcpath}/contrib/${item} ${destroot}${prefix}/share/${realname}/${item}
    }

    xinstall -d ${destroot}${prefix}/share/doc/${realname}
    xinstall -W ${worksrcpath} -m 0644 LICENSE readme.md ${destroot}${prefix}/share/doc/${realname}/
    move ${worksrcpath}/docs ${destroot}${prefix}/share/doc/${realname}/

    xinstall -d ${destroot}${lokidir}
    ln -sf ${prefix}/etc/${realname}/mainnet.signed ${destroot}${lokidir}/bootstrap.signed
}

notes "
lokinet must be launched under the root user.
"
