# -*- 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           github 1.0

github.setup        aria2 aria2 1.37.0 release-
revision            7
categories          net
maintainers         {mps @Schamschula} {@barracuda156 macos-powerpc.org:barracuda}
license             {GPL-2+ OpenSSLException}
description         download utility with resuming and segmented downloading
long_description    ${name} is a download utility with resuming and segmented \
                    downloading. Supported protocols are HTTP/HTTPS/FTP/BitTorrent.
homepage            https://aria2.github.io
checksums           rmd160  942de85a653d890bfa1198dc82069cff8267991b \
                    sha256  7e7541966bc0f1d7b121ea9f4189c65b5782e78771ec4f49f40b6cf53d7a12df \
                    size    4186060
github.tarball_from tarball

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

depends_lib-append      port:gettext-runtime \
                        port:libiconv \
                        path:lib/pkgconfig/libuv.pc:libuv \
                        port:libxml2 \
                        port:zlib

# use_* must be defined after depends_*, otherwise the automatic dependencies
# will be overwritten.
# Alternatively, depends_*-append could be used.
use_autoreconf      yes

# This is what FreeBSD uses: https://github.com/freebsd/freebsd-ports/blob/main/www/aria2/Makefile
set cache_size      16M

configure.args-append \
                    --disable-epoll \
                    --disable-silent-rules \
                    --enable-bittorrent \
                    --enable-libaria2 \
                    --enable-metalink \
                    --enable-shared \
                    --enable-ssl \
                    --enable-websocket \
                    --with-disk-cache=${cache_size} \
                    --with-libiconv-prefix=${prefix} \
                    --with-libintl-prefix=${prefix} \
                    --with-libuv \
                    --with-libz \
                    --with-xml-prefix=${prefix} \
                    --without-ca-bundle \
                    --without-gnutls \
                    --without-jemalloc \
                    --without-libcares \
                    --without-libexpat \
                    --without-libgcrypt \
                    --without-libgmp \
                    --without-libnettle \
                    --without-libssh2 \
                    --without-openssl \
                    --without-sqlite3

configure.cxxflags-append \
                    -I${prefix}/include/libxml2
configure.ldflags-append \
                    -lintl

# See https://trac.macports.org/ticket/68951
# and https://github.com/aria2/aria2/issues/2083
if {${os.platform} eq "darwin" && ${os.major} == 23} {
    configure.ldflags-append  -framework CoreServices
}

post-destroot {
    xinstall -d -m 0755 "${destroot}${prefix}/share/bash-completion/completions"
    xinstall -m 0644 "${destroot}${prefix}/share/doc/aria2/bash_completion/aria2c" \
        "${destroot}${prefix}/share/bash-completion/completions/aria2c"
    file delete -force "${destroot}${prefix}/share/doc/aria2/bash_completion"
}

if {![variant_isset gnutls]} {
    # C++14
    configure.cxxflags-append -std=c++14
    compiler.blacklist-append {clang < 800.0.38}
    compiler.cxx_standard  2014
} else {
    compiler.cxx_standard  2011
}

# See: https://trac.macports.org/ticket/69563
if {![variant_isset docs]} {
    patchfiles-append       patch-doc-Makefile.am.diff
}

variant cares description "Add c-ares support" {
    depends_lib-append      port:c-ares
    configure.args-replace  --without-libcares --with-libcares
}

variant docs description "Build documentation using sphinx" {
    # When updating the python.version also update the branch below
    set python.version      312

    depends_build-append    port:py${python.version}-sphinx

    # Don't build pt and ru man pages
    patchfiles-append       patch-doc-manual-src-Makefile.am.diff
    # Use MacPorts binary name for sphinx-build
    patchfiles-append       patch-doc-manual-src-en-Makefile.am.diff
    post-patch {
        reinplace "s|%BRANCH%|3.12|g" ${worksrcpath}/doc/manual-src/en/Makefile.am
    }
}

variant gnutls conflicts openssl description "Use GNU TLS instead of Apple built-in TLS library" {
    depends_lib-append      path:share/curl/curl-ca-bundle.crt:curl-ca-bundle \
                            path:lib/pkgconfig/gnutls.pc:gnutls
    configure.args-append   --with-libgnutls-prefix=${prefix} \
                            --without-appletls
    configure.args-replace  --without-ca-bundle --with-ca-bundle=${prefix}/share/curl/curl-ca-bundle.crt \
                            --without-gnutls --with-gnutls
}

variant openssl conflicts gnutls description "Use OpenSSL instead of Apple built-in TLS library" {
    PortGroup               openssl 1.0

    depends_lib-append      path:share/curl/curl-ca-bundle.crt:curl-ca-bundle \
                            port:libssh2
    configure.args-append   --with-openssl-prefix=[openssl::install_area] \
                            --without-appletls
    configure.args-replace  --without-ca-bundle --with-ca-bundle=${prefix}/share/curl/curl-ca-bundle.crt \
                            --without-libssh2 --with-libssh2 \
                            --without-openssl --with-openssl
}

variant sqlite3 description "Add sqlite3 support" {
    depends_lib-append      port:sqlite3
    configure.args-replace  --without-sqlite3 --with-sqlite3
}

# appletls not available on 10.6 or earlier
if {(${os.platform} ne "darwin" || ${os.major} <= 10) && \
    ![variant_isset openssl]} {
    default_variants-append +gnutls
}

if {[variant_isset gnutls] || [variant_isset openssl]} {
    patchfiles-append       patch-src-SimpleRandomizer.cc.diff
}

# libuv presently not available on 10.4 or earlier
if {${os.platform} eq "darwin" && ${os.major} <= 8} {
    depends_lib-delete      path:lib/pkgconfig/libuv.pc:libuv
    configure.args-replace  --with-libuv --without-libuv
}
