# -*- 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.4
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  103cfc5fa94b46f8f7e293f4b3ad62dd59dd4a3c \
                        sha256  86e7aa020cdf64826447034bc85f4d3b7c848d8c0891d9dc9b9fddfb2c805d22 \
                        size    11901656

platforms               macosx

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

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

build.pre_args-append   -derivedDataPath ${worksrcpath}/DerivedData

compiler.cxx_standard   2020

# Hardened Runtime causes crash when loading unsigned libs
xcode.build.settings-append \
                        ENABLE_HARDENED_RUNTIME=NO \
                        CODE_SIGN_IDENTITY=-

patchfiles              patch-xcodebuild.diff

# Icon Composer .icon resources require Xcode 26; the PNG AppIcon is still bundled.
if {[vercmp ${xcodeversion} 26.0] < 0} {
    patchfiles-append   patch-legacy-appicon.diff
}

# The data-based Quick Look extension requires macOS 12. Keep the legacy plugin
# for older deployment targets, since xcode PortGroup overrides the target globally.
if {[vercmp ${macosx_deployment_target} 12.0] < 0} {
    patchfiles-append   patch-disable-quicklook-extension.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"
}

variant daemon description {Builds headless daemon} {
    xcode.scheme-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
   }
}

# PowerManager uses macOS 12 SDK declarations (first shipped with Xcode 13.1).
# The main app uses unguarded macOS 11 UI APIs, including SF Symbols.
set minxcodever 13.1
set minosver 11.0
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
