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

# strnlen, clock_gettime
legacysupport.newest_darwin_requires_legacy 15

github.setup                uTox uTox 0.18.1 v
revision                    0
categories                  net p2p security
license                     MIT
maintainers                 nomaintainer
description                 µTox the lightest and fluffiest Tox client
long_description            {*}${description}
distname                    ${name}-${version}-full
checksums                   rmd160  4842d6ae85818c7df37a6f2c54b47a91b3a82e18 \
                            sha256  fc883483d86b1830674f3b08515daa27ae54ab47646c7edc48a067e8d1e8c114 \
                            size    3944947
github.tarball_from         releases

worksrcdir                  ${workpath}/${name}

depends_build-append        path:bin/pkg-config:pkgconfig

depends_lib-append          port:libopus \
                            port:libsodium \
                            port:toxcore \
                            path:lib/pkgconfig/vpx.pc:libvpx

# https://github.com/uTox/uTox/issues/1573
# https://github.com/uTox/uTox/pull/1575
patchfiles-append           0001-Allow-X11-build-on-macOS.patch

configure.args-append       -DENABLE_FILTERAUDIO=OFF \
                            -DENABLE_LTO=OFF \
                            -DENABLE_TESTS=OFF \
                            -DSTATIC_DEFAULT=OFF

compiler.c_standard         2011

# https://github.com/uTox/uTox/issues/1571
if {[string match *clang* ${configure.compiler}]} {
    configure.cflags-append -Wno-error=int-conversion \
                            -Wno-error=incompatible-function-pointer-types
}

# https://github.com/uTox/uTox/issues/1572
if {${os.platform} eq "darwin" && ${os.major} > 10} {
    use_xcode               yes

    destroot {
        copy ${cmake.build_dir}/utox.app ${destroot}${applications_dir}/
    }
} else {
    # FIXME: OpenAL is broken: https://github.com/uTox/uTox/issues/1574
    # Possibly Big-endian issue.

    # utox(45546,0xf0305000) malloc: *** error for object 0xc96328: Non-aligned pointer being freed
    legacysupport.redirect_bins utox

    depends_lib-append      port:dbus \
                            port:desktop-file-utils \
                            port:fontconfig \
                            port:freetype \
                            port:xorg-libice \
                            port:xorg-libsm \
                            port:xorg-libX11 \
                            port:xorg-libXext \
                            port:xrender

    depends_run-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3

    configure.args-append   -DENABLE_COCOA=OFF

    # macOS restricts section name to 16 chars, so shorten the path.
    # https://gareus.org/wiki/embedding_resources_in_executables
    post-extract {
        copy ${worksrcpath}/icons/utox-128x128.png ${worksrcpath}/icons/utox.png
    }

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

variant openal_soft description "Use openal-soft instead of Apple framework" {
    patchfiles-append       0002-Use-openal-soft.patch

    depends_lib-append      port:openal-soft
}
