# -*- 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           github  1.0
PortGroup           cmake   1.1

epoch               3
github.setup        FreeRDP FreeRDP 3.17.2
github.tarball_from releases
revision            0
categories          net
license             Apache
maintainers         nomaintainer
description         A Remote Desktop Protocol implementation
long_description    FreeRDP is a free implementation of the Remote Desktop Protocol (RDP), released \
                    under the Apache license.

homepage            https://www.freerdp.com

checksums           rmd160  ee60f05ec2fb784da454dfac9e441962d4d86823 \
                    sha256  c42c712ad879bf06607b78b8c3fad98e08c82f73f4e0bc1693552900041e692a \
                    size    10556445

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

depends_lib-append  port:xorg-libX11 \
                    port:xorg-libXcursor \
                    port:xorg-libXext \
                    port:xorg-libXi \
                    port:xorg-libXinerama \
                    port:xorg-libxkbfile \
                    port:xorg-libXrandr \
                    port:xorg-libXv \
                    port:xrender \
                    port:zlib \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    path:lib/libssl.dylib:openssl \
                    path:lib/pkgconfig/libusb-1.0.pc:libusb \
                    path:lib/pkgconfig/cairo.pc:cairo

worksrcdir          freerdp-${version}

# AVAudioBuffer was introduced in OS X 10.10
if {${os.platform} eq "darwin" && ${os.major} < 14} {
    configure.args-append \
        -DCHANNEL_RDPSND=OFF
}

# Disable check on cmake build type so we can control compiler flags
patchfiles-append patch-cmake_ignore_build_type.diff

post-patch {
    if {[string match "*gcc*" ${configure.compiler}]} {
        reinplace "s:-fobjc-nonfragile-abi::" \
            ${worksrcpath}/client/Mac/cli/CMakeLists.txt \
            ${worksrcpath}/client/Mac/CMakeLists.txt
    }
}

use_xcode           yes

# Related to state on macOS: https://github.com/FreeRDP/FreeRDP/issues/10558
# Server is non-functional, even if it compiles, thus -DWITH_SERVER=OFF
# SDL client is currently recommended over native Mac client
configure.args-append \
                    -DWITH_ALSA=OFF \
                    -DWITH_CCACHE=OFF \
                    -DWITH_FFMPEG=OFF \
                    -DWITH_X11=ON \
                    -DWITH_MANPAGES=OFF \
                    -DWITH_PULSE=OFF \
                    -DWITH_JPEG=ON \
                    -DWITH_LAME=OFF \
                    -DWITH_FAAD2=OFF \
                    -DWITH_FAAC=OFF \
                    -DWITH_SOXR=OFF \
                    -DWITH_CUPS=ON \
                    -DWITH_CAIRO=ON \
                    -DWITH_PKCS11=OFF \
                    -DWITH_SERVER=OFF \
                    -DWITH_CLIENT_MAC=OFF \
                    -DWITH_CLIENT_SDL_VERSIONED=ON \
                    -DWITH_CLIENT_SDL2=OFF \
                    -DWITH_CLIENT_SDL3=OFF

# ffmpeg only supports VDA (video hardware acceleration, mostly H264) on 10.6.3+ (#48397)
if {${os.platform} ne "darwin" || ${os.major} > 10 || (${os.major} == 10 && ${os.minor} >= 3)} {
    set ffmpeg_ver          6
    configure.args-replace  -DWITH_FFMPEG=OFF -DWITH_FFMPEG=ON
    depends_lib-append      port:ffmpeg${ffmpeg_ver}
    configure.pkg_config_path \
                            ${prefix}/libexec/ffmpeg${ffmpeg_ver}/lib/pkgconfig
}

# I've seen manpage building fail for unclear reasons, so provide a variant to turn it off
variant docs description {build the manpages} {
    depends_build-append \
                    port:libxslt \
                    port:docbook-xsl-nons
    configure.args-replace \
                    -DWITH_MANPAGES=OFF \
                    -DWITH_MANPAGES=ON
}
default_variants-append +docs

variant pulseaudio description {build PulseAudio support} {
    depends_lib-append \
                    port:pulseaudio
    configure.args-replace \
                    -DWITH_PULSE=OFF \
                    -DWITH_PULSE=ON
}

variant codecs description {support MP3, FAAD2, FAAC and SOXR codecs} {
    depends_lib-append \
                    port:lame \
                    port:faad2 \
                    port:faac \
                    port:soxr
    configure.args-replace \
                    -DWITH_LAME=OFF \
                    -DWITH_LAME=ON \
                    -DWITH_FAAD2=OFF \
                    -DWITH_FAAD2=ON \
                    -DWITH_FAAC=OFF \
                    -DWITH_FAAC=ON \
                    -DWITH_SOXR=OFF \
                    -DWITH_SOXR=ON
}

build.post_args-append -k

set app_dir         ${applications_dir}/MacFreeRDP.app/Contents
variant cocoa description {build native macOS/Cocoa client} {
    configure.args-replace \
                    -DWITH_CLIENT_MAC=OFF \
                    -DWITH_CLIENT_MAC=ON

    post-destroot {
        xinstall -d ${destroot}${app_dir}
        move ${destroot}${prefix}/Info.plist ${destroot}${prefix}/PkgInfo \
            ${destroot}${app_dir}

        xinstall -d ${destroot}${app_dir}/MacOS
        move ${destroot}${prefix}/bin/MacFreeRDP ${destroot}${app_dir}/MacOS

        xinstall -d ${destroot}${app_dir}/Resources
        move ${destroot}${prefix}/share/FreeRDP.icns \
            {*}[glob ${destroot}${prefix}/share/*.nib] \
            ${destroot}${app_dir}/Resources

        xinstall -d ${destroot}${app_dir}/Resources/en.lproj
        move {*}[glob ${destroot}${prefix}/share/*.strings] \
            ${destroot}${app_dir}/Resources/en.lproj
    }
}

variant sdl2 description {build SDL2 client} {
    depends_lib-append port:libsdl2_ttf
    configure.args-replace \
                    -DWITH_CLIENT_SDL2=OFF \
                    -DWITH_CLIENT_SDL2=ON
}

variant sdl3 description {build SDL3 client} {
    depends_lib-append port:SDL3_ttf
    configure.args-replace \
                    -DWITH_CLIENT_SDL3=OFF \
                    -DWITH_CLIENT_SDL3=ON
}
if {${os.platform} eq "darwin" && ${os.major} < 20} {
    default_variants-append +sdl2
} else {
    default_variants-append +sdl3
}
