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

# EXPERIMENTAL
name                libsdl2-x11
conflicts           libsdl2-cocoa libsdl2-powerpc libsdl2-snowleopard
github.setup        libsdl-org SDL 2.32.10 release-
revision            0
categories          devel multimedia
license             zlib
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
description         Cross-platform multi-media development API
long_description    Simple DirectMedia Layer is a cross-platform development library \
                    designed to provide low-level access to audio, keyboard, mouse, \
                    joystick and graphics hardware via OpenGL and Direct3D. This port \
                    is aimed at supporting macOS PowerPC and uses X11 backend.

homepage            https://www.libsdl.org

github.tarball_from releases
distname            SDL2-${version}
checksums           rmd160  4212372b886f8dbab92141c520951e245e0c9657 \
                    sha256  5f5993c530f084535c65a6879e9b26ad441169b3e25d789d83287040a9ca5165 \
                    size    7630262

depends_lib-append  port:xorg-libX11 \
                    port:xorg-libXcursor \
                    port:xorg-libXfixes \
                    port:xorg-libXi \
                    port:xorg-libXinerama \
                    port:xorg-libXrandr \
                    port:xorg-libXScrnSaver \
                    port:xorg-libXxf86vm \
                    port:xrender

# xorg-server port does not build presently on any PowerPC macOS.
# Use xorg-server-legacy or xorg-server-1.8. Explicit dependency is dropped
# to avoid a need to activate the whole of xorg-server-legacy tree every time.
# depends_run-append  port:xorg-server-legacy

configure.args-append \
                    --disable-dbus \
                    --disable-esd \
                    --disable-libsamplerate \
                    --disable-pulseaudio \
                    --disable-render-metal \
                    --disable-video-cocoa \
                    --disable-video-metal \
                    --disable-video-opengl \
                    --disable-video-wayland \
                    --disable-x11-shared \
                    --enable-video-x11 \
                    --x-includes=${prefix}/include \
                    --x-libraries=${prefix}/lib

# Disable Jack until this is fixed:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61759
configure.args-append \
                    --disable-jack

# FIXME: look into fixing CoreAudio module for Tiger.
if {${os.platform} eq "darwin" && ${os.major} < 9} {
    configure.args-append \
                    --disable-audio \
                    --disable-haptic \
                    --disable-hidapi \
                    --disable-hidapi-joystick \
                    --disable-locale
}

build.args          V=1

# Avoid broken clang. Xcode gcc works, as of 2.32.x at least.
compiler.blacklist  {clang < 500}

# Fix-ups for PowerPC systems:
patchfiles-append   0001-Fixes-for-PowerPC.patch

# Fix building against Macports X11.
patchfiles-append   0002-fix-x11.diff

# Avoid broken joystick on < 10.7:
patchfiles-append   0003-patch-joystick.diff

# Trash clangisms:
if {[string match *gcc* ${configure.compiler}]} {
    patchfiles-append \
                    0004-gcc-build.patch
}

default_variants-append \
                    +opengl

# Config header hardcodes wrong paths.
post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" \
                    ${worksrcpath}/include/SDL_config_macosx.h \
                    ${worksrcpath}/configure

    if {[variant_isset opengl]} {
        reinplace "s|@GL_LIB@|${prefix}/lib/libGL.1.dylib|" \
            ${worksrcpath}/src/video/x11/SDL_x11opengl.c
    } else {
        reinplace "s|@GL_LIB@|/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib|" \
            ${worksrcpath}/src/video/x11/SDL_x11opengl.c
    }
}

variant dbus description "Enable DBus support" {
    depends_lib-append \
                    port:dbus
    configure.args-replace \
                    --disable-dbus --enable-dbus
}

# Notice, currently only Jack1 is available.
variant jack description "Build with Jack support" {
    depends_lib-append \
                    port:jack
    configure.args-replace \
                    --disable-jack --enable-jack
}

variant opengl conflicts opengl_apple description "Enable OpenGL support" {
    # Mesa dependency due to: https://trac.macports.org/ticket/61943
    # We cannot depend on XQuartz.
    depends_lib-append \
                    port:mesa
}

# This is experimental and untested!
variant opengl_apple conflicts opengl description "Use Apple OpenGL" {
    patchfiles-append \
                    patch-opengl_apple.diff
}

if {[variant_isset opengl] || [variant_isset opengl_apple]} {
    configure.args-replace \
                    --disable-video-opengl --enable-video-opengl
}

variant pulseaudio description "Build with PulseAudio support" {
    depends_lib-append \
                    port:pulseaudio
    configure.args-replace \
                    --disable-pulseaudio --enable-pulseaudio
}

variant samplerate description "Build with libsamplerate support" {
    depends_lib-append \
                    port:libsamplerate
    configure.args-replace \
                    --disable-libsamplerate --enable-libsamplerate
}

# FIXME: configure keeps ignoring wayland.
variant wayland description "Build with Wayland support" {
    patchfiles-append patch-wayland.diff
    depends_build-append \
                    path:bin/pkg-config:pkgconfig \
                    port:wayland-protocols
    depends_lib-append \
                    port:libxkbcommon \
                    port:wayland
    configure.args-replace \
                    --disable-video-wayland --enable-video-wayland
}

notes "
Please install xquartz port or another implementation of X server.
"

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} BUGS.txt CREDITS.txt LICENSE.txt \
        README-SDL.txt TODO.txt WhatsNew.txt \
        {*}[glob ${worksrcpath}/docs/*.md] ${destroot}${docdir}
}
