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

github.setup        transmission transmission 4.0.6
revision            1
name                transmission-x11
conflicts           transmission-qt
categories          net x11 p2p
license             {GPL-2 OpenSSLException}
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
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 GTK+ version.

homepage            https://transmissionbt.com

checksums           rmd160  b4fbc8aa3491eb775f3238cabea1c02ab50ec5db \
                    sha256  2a38fe6d8a23991680b691c277a335f8875bdeca2b97c6b26b598bc9c7b0c45f \
                    size    11908296

github.tarball_from releases
use_xz              yes

# Do not remove this as it allows transmission and this port to share files
dist_subdir         transmission

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

depends_lib-append  port:curl \
                    port:gettext-runtime \
                    port:libb64 \
                    port:libevent \
                    port:miniupnpc \
                    port:zlib

configure.args-append \
                    -DENABLE_CLI=ON \
                    -DENABLE_DAEMON=ON \
                    -DENABLE_GTK=OFF \
                    -DINSTALL_DOC=OFF \
                    -DENABLE_MAC=OFF \
                    -DENABLE_QT=OFF \
                    -DUSE_SYSTEM_B64=ON \
                    -DENABLE_TESTS=OFF \
                    -DENABLE_NLS=OFF

# Drop with the next release.
# https://github.com/transmission/transmission/commit/febfe49ca3ecab1a7142ecb34012c1f0b2bcdee8
patchfiles-append   febfe49ca3ecab1a7142ecb34012c1f0b2bcdee8.patch

compiler.c_standard 2011
compiler.cxx_standard 2017

post-extract {
    # https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=c13b5e88c6e9c7bd2698d844cb5ed127ed809f7e
    # Prevent m4_copy error when running aclocal
    # m4_copy: won't overwrite defined macro: glib_DEFUN
    delete ${worksrcpath}/m4/glib-gettext.m4
}

default_variants +gtk

# transmission-gtk(47187) malloc: *** error for object 0x33480a4: Non-aligned pointer being freed
legacysupport.redirect_bins transmission-cli transmission-create transmission-daemon transmission-edit transmission-gtk transmission-remote transmission-show

if [string match macports-gcc-7 ${configure.compiler}] {

    # two patches that I believe are only needed to build with gcc7

    # work around std::tolower not being a constexpr. I am not sure how this
    # works on other compilers, as it seems it should not
    patchfiles-append   patch-transmission-x11-gcc7-crypto-utils-constexpr.diff

    # work around std::ios_base::failure not deriving from std:system_error as
    # it should. This may be related to the way we force the CXX11_ABI off in
    # MacPorts. At any rate, the code has to be adjusted as std::ios_base::failure
    # derives from std::exception when building with gcc7 on older Darwin, even if
    # the c++ standard is set to c++11 or newer
    patchfiles-append   patch-transmission-x11-gcc7-messagelogwindow-errorcode-fix.diff
}

variant aqua description {Build Aqua front-end} {
    configure.args-replace -DENABLE_QT=OFF -DENABLE_QT=ON \
                           -DENABLE_MAC=OFF -DENABLE_MAC=ON
}

variant gtk description {Build Gtk3 front-end} {
    depends_lib-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                            port:gtkmm3 \
                            port:libnotify \
                            port:desktop-file-utils

    configure.args-replace  -DENABLE_GTK=OFF -DENABLE_GTK=ON

    depends_run             port:adwaita-icon-theme

    post-activate {
        system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
        system "${prefix}/bin/gtk-update-icon-cache -f -t ${prefix}/share/icons/hicolor"
    }
}

# As on now, GUI has troubles on PowerPC: https://trac.macports.org/ticket/67654
# CLI works perfectly fine, downloads are working.

if {${os.platform} eq "darwin" && ${os.major} < 9} {
    pre-fetch {
        ui_error "${name} requires Mac OS X 10.5 or greater."
        return -code error "incompatible Mac OS X version"
    }
}
