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

PortSystem      1.0
PortGroup       github 1.0
PortGroup       qmake5 1.0
PortGroup       compiler_blacklist_versions 1.0
PortGroup       boost 1.0

categories      net
name            qBittorrent-qt5
conflicts       qBittorrent
maintainers     {i0ntempest @i0ntempest} openmaintainer
license         {GPL-2+ OpenSSLException}

description     The qBittorrent project aims to provide an open-source software alternative to µTorrent.
long_description \
                qBittorrent is a bittorrent client programmed in C++ / Qt that uses \
                libtorrent (sometimes called libtorrent-rasterbar) by Arvid Norberg. \
                It aims to be a good alternative to all other bittorrent clients out \
                there. qBittorrent is fast, stable and provides unicode support as well \
                as many features.

homepage        https://www.qbittorrent.org

if {${os.platform} ne "darwin" || ${os.major} >= 19} {
    github.setup    qbittorrent qBittorrent 4.6.7 release-
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    # v5.0 no longer support Qt 5 - do not update
    revision        1
    checksums       rmd160  cbef8ea1e0a248a2b98088cc04aa38bf84a4abc2 \
                    sha256  a3691d6acfaf31cfdc583d5d8029b67e5c8099cffe3ab73eadf89085611b3956 \
                    size    9097754

    qt5.min_version 5.11
    configure.args-append \
                    CONFIG+=c++17 \
                    DEFINES+=QBT_USES_LIBTORRENT2
    compiler.cxx_standard 2017
    compiler.blacklist-append *gcc-3.* *gcc-4.* *gcc-5.* *gcc-6.* {clang < 1200}
    boost.version   1.81
} else {
    # qBittorrent 4.3.4 to 4.3.4.1 required Qt 5.12 then lowered to 5.11 after
    # qBittorrent 4.3.3 and later requires C++17 features and therefore won't build with
    # libc++ on Sierra and older
    # That is 4.3.3 might work here but needs testing
    github.setup    qbittorrent qBittorrent 4.3.2 release-
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision        4
    checksums       rmd160  543c1a5b59c15912cde31d98bed55810709aef75 \
                    sha256  187b8625e8787384a68a30eff3702b0e5c246512cb20013a38a25553cb18c5e7 \
                    size    7867939

    qt5.min_version 5.9
    configure.args-append \
                    CONFIG+=c++14
    compiler.cxx_standard 2014
    compiler.blacklist-append *gcc-3.* *gcc-4.* {clang < 900.0.39.2}
    boost.version   1.76
}


depends_build-append \
                port:pkgconfig

depends_lib-append \
                port:libtorrent-rasterbar \
                path:lib/libssl.dylib:openssl \
                port:zlib

boost.depends_type  \
                build

qt5.depends_build_component \
                qttools

qt5.depends_component \
                qttranslations

# Workaround xcrun: error: SDK "macosx12" cannot be located
configure.sdk_version

configure.args-append \
                CONFIG-=silent \
                CONFIG+=nogui \
                CONFIG+=nowebui \
                CONFIG+=link_pkgconfig \
                PKGCONFIG+=libtorrent-rasterbar \
                PKGCONFIG+=openssl \
                PKGCONFIG+=zlib \
                INCLUDEPATH+=[boost::include_dir] \
                QMAKE_MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target}

# fix the deployment target setting to the MacPorts setting
post-patch {
         reinplace "s|QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.15|QMAKE_MACOSX_DEPLOYMENT_TARGET = ${macosx_deployment_target}|g" \
             ${worksrcpath}/macxconf.pri
}

destroot {
    if {[variant_isset gui]} {
        set appname "qbittorrent"
    } else {
        set appname "qbittorrent-nox"
    }
    reinplace "s|Plugins = PlugIns|Plugins = ${qt_dir}/plugins|g" ${worksrcpath}/src/${appname}.app/Contents/Resources/qt.conf
    system -W ${worksrcpath}/src "/usr/bin/codesign --sign - ${appname}.app"
    copy ${worksrcpath}/src/${appname}.app ${destroot}${applications_dir}
    ln -s ${applications_dir}/${appname}.app/Contents/MacOS/${appname} ${destroot}${prefix}/bin
}

variant gui description {Enable GUI} {
                configure.args-delete CONFIG+=nogui
                qt5.depends_component qtmacextras \
                                      qtsvg
}

variant webui description {Enable WebUI} {
                configure.args-delete CONFIG+=nowebui
}

default_variants \
                +gui +webui

livecheck.type  none