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

github.setup            transmission transmission 4.1.0
revision                0
categories              net aqua
maintainers             {khindenburg @kurthindenburg} {i0ntempest @i0ntempest} openmaintainer
license                 MIT GPL-2

description             Lightweight BitTorrent client
long_description \
    Transmission is a free, lightweight BitTorrent client. \
    It features a simple, intuitive interface on top of an \
    efficient, cross-platform back-end. Transmission is open \
    source (MIT license) and runs on Mac OS X (Cocoa interface), \
    Linux/NetBSD/FreeBSD/OpenBSD (GTK+ interface) and BeOS \
    (native interface). This is the Cocoa version.

homepage                https://transmissionbt.com

github.tarball_from     releases
use_xz                  yes

checksums               rmd160  10821dce7e7c00aee10b64c9e7e719ec17777746 \
                        sha256  dcd28c1c9e6126229c4c17dbc9e95c9fd4aed7e76f4a1f2a74604c8cddec49d6 \
                        size    12184984

platforms               macosx

depends_build-append    port:libdeflate \
                        port:libevent
depends_lib-append      port:gettext-runtime \
                        port:curl \
                        path:lib/libssl.dylib:openssl

xcode.target            Transmission
xcode.configuration     Release
xcode.build.settings-append \
                        ENABLE_USER_SCRIPT_SANDBOXING=NO

compiler.cxx_standard   2020

# Hardened Runtime on 10.14 is not compatible with AdHoc signing
# Hardened Runtime on 10.15 causes crash when loading unsigned libs
if {${os.major} == 18 || ${os.major} == 19} {
    xcode.build.settings-append \
                        ENABLE_HARDENED_RUNTIME=NO \
                        CODE_SIGN_IDENTITY=-
}

patchfiles              patch-xcodebuild.diff

post-patch {
    reinplace -E "s%third-party/(curl|libevent|openssl|libdeflate)/(include|lib)%@@PREFIX@@/\\2%g" \
        ${worksrcpath}/Transmission.xcodeproj/project.pbxproj
    reinplace -E "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/Transmission.xcodeproj/project.pbxproj
    # Disable Sparkle without messing with xib files
    system -W ${worksrcpath}/macosx/ "/usr/libexec/PlistBuddy -c \"Delete :SUFeedURL\" Info.plist"
    system -W ${worksrcpath}/macosx/ "/usr/libexec/PlistBuddy -c \"Set :SUEnableAutomaticChecks bool false\" Defaults.plist"
    if {${os.major} <= 18} {
        # 10.14 and older do not have any of python3, python3.7, and python2
        reinplace "s|command -v python2|command -v python2.7|" ${worksrcpath}/Transmission.xcodeproj/project.pbxproj
        # systemTealColor not available in 10.14 SDK
        if {[vercmp ${macosx_sdk_version} 10.14] <= 0} {
            reinplace "s|systemTealColor|systemBlueColor|g" ${worksrcpath}/macosx/MessageWindowController.mm
        }
    }
}

variant daemon description {Builds headless daemon} {
    xcode.target-append transmission-daemon \
                        transmission-remote \
                        transmission-create \
                        transmission-edit \
                        transmission-show
}

destroot {
    file copy ${worksrcpath}/build/${xcode.configuration}/Transmission.app \
        ${destroot}${applications_dir}/Transmission.app

    if {[variant_isset daemon]} {
        xinstall -m 755 -W ${worksrcpath}/build/${xcode.configuration} \
        transmission-daemon transmission-remote transmission-create transmission-edit transmission-show \
        ${destroot}${prefix}/bin

        xinstall -m 644 -W ${worksrcpath} \
            daemon/transmission-daemon.1 \
            utils/transmission-remote.1 utils/transmission-create.1 utils/transmission-edit.1 utils/transmission-show.1 \
            ${destroot}${prefix}/share/man/man1
   }
}

set minxcodever 12.0
set minosver 10.13
if {([vercmp $xcodeversion ${minxcodever}] < 0) || ([vercmp ${macosx_deployment_target} ${minosver}] < 0)} {
    known_fail yes
    pre-fetch {
        ui_error "${name} ${version} requires Xcode ${minxcodever} or greater to build and macOS ${minosver} to run."
        ui_error "Consider installing transmission-x11 instead."
        return -code error "incompatible OS X version"
    }
}

# Ignore betas - can't use extract.suffix
livecheck.regex     (\\d+\.\\d+\.\\d+\).tar
