# -*- 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           conflicts_build 1.0
PortGroup           ffmpeg 1.0
PortGroup           github 1.0
PortGroup           qt4 1.0

# Stable version is the regular QMPlay2 port.
# Upd. This one is also usable normally now.
set real_name       QMPlay2
name                ${real_name}-devel
conflicts           ${real_name}

github.setup        zaps166 ${real_name} 25.09.11
revision            1
categories          multimedia audio
license             LGPL-3
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
description         Qt Media Player 2
long_description    QMPlay2 is a video and audio player. It can play all formats supported by FFmpeg, \
                    libmodplug (including J2B and SFX). It also supports Audio CD, raw files, \
                    Rayman 2 music and chiptunes. It contains YouTube and Prostopleer browser.

checksums           rmd160  231fe94b63f3074289f1c0701846fc4776a9d68e \
                    sha256  2821194dc09630b3238d4a95665501b5fa4578d8e76626c92f184dfd0025e782 \
                    size    2200356
github.tarball_from releases
distname            ${real_name}-src-${version}
use_xz              yes

cmake.generator     Ninja

# !THIS IS IMPORTANT!
conflicts_build     ffmpeg ffmpeg-devel

ffmpeg.version      8

patch.pre_args-replace  -p0 -p1

patchfiles-append   0001-Build-with-Qt4.patch
patchfiles-append   0002-GL-fixes.patch
patchfiles-append   0003-unbreak-objc.patch
patchfiles-append   0004-System-yt-dlp.patch

# Keep legacy hacks conditional:
if {${os.platform} eq "darwin" && ${os.major} < 10} {
    patchfiles-append \
                    0005-leopard.patch
    # https://github.com/macos-powerpc/powerpc-ports/issues/85
    # You can manually choose QRawFont codepath by passing -DUSE_QRAWFONT in cppflags.
    if {${configure.build_arch} in [list i386 ppc]} {
        patchfiles-append \
                    0006-QRawFont.patch
    }
}

# TODO: sort this out neatly and generally.
# For now, require -devel subport at least for the build.
depends_build-delete \
                    port:qt4-mac
depends_build-append \
                    port:qt4-mac-devel
depends_lib-delete  port:qt4-mac
depends_lib-append  port:qt4-mac-devel

depends_lib-append  path:lib/pkgconfig/libass.pc:libass \
                    port:libcddb \
                    port:libcdio \
                    port:libgme \
                    port:libsidplayfp \
                    port:portaudio \
                    port:QJson4 \
                    port:rubberband \
                    port:taglib \
                    port:zlib

# Requiring yt-dlp pulls in ffmpeg, which in turn totally breaks the app.
# This is a runtime-only dependency, not needed during the build.
# Just install/activate it upon the build completion.

# depends_run-append  port:yt-dlp

compiler.cxx_standard   2017

post-patch {
    reinplace "s|@prefix@|${prefix}|" ${worksrcpath}/src/qmplay2/YouTubeDL.cpp
    reinplace "s|@destroot@|${destroot}|" ${worksrcpath}/src/gui/CMakeLists.txt
    reinplace "s|@qt_libs_dir@|${qt_libs_dir}|" ${worksrcpath}/src/gui/CMakeLists.txt
    reinplace "s|@qt_plugins_dir@|${qt_plugins_dir}|" ${worksrcpath}/src/gui/CMakeLists.txt
}

# TODO: sidplay temporarily disabled due to breaking API changes:
# https://github.com/zaps166/QMPlay2/issues/1008

configure.args-append \
                    -DCMAKE_INSTALL_PREFIX=${applications_dir} \
                    -DCMAKE_LINK_DEPENDS_NO_SHARED=OFF \
                    -DQJSON4_INCLUDE_DIR=${prefix}/include/QJson4 \
                    -DSET_INSTALL_RPATH=OFF \
                    -DUSE_ALSA=OFF \
                    -DUSE_AUDIOCD=ON \
                    -DUSE_AUDIOFILTERS=ON \
                    -DUSE_AVRESAMPLE=OFF \
                    -DUSE_CHIPTUNE_GME=ON \
                    -DUSE_CHIPTUNE_SID=OFF \
                    -DUSE_CUVID=OFF \
                    -DUSE_FFMPEG=ON \
                    -DUSE_FFMPEG_VTB=OFF \
                    -DUSE_INPUTS=ON \
                    -DUSE_LASTFM=ON \
                    -DUSE_LIBASS=ON \
                    -DUSE_LINK_TIME_OPTIMIZATION=OFF \
                    -DUSE_LYRICS=ON \
                    -DUSE_MEDIABROWSER=OFF \
                    -DUSE_MODPLUG=ON \
                    -DUSE_NOTIFY=OFF \
                    -DUSE_OPENGL=ON \
                    -DUSE_PORTAUDIO=ON \
                    -DUSE_PULSEAUDIO=OFF \
                    -DUSE_QML=OFF \
                    -DUSE_QT4=ON \
                    -DUSE_RUBBERBAND=ON \
                    -DUSE_TAGLIB=ON \
                    -DUSE_UPDATES=OFF \
                    -DUSE_VIDEOFILTERS=ON \
                    -DUSE_VISUALIZATIONS=OFF \
                    -DUSE_XVIDEO=OFF \
                    -DUSE_YOUTUBE=ON

if {${os.platform} eq "darwin" && ${os.major} < 17} {
    configure.args-append \
                    -DUSE_FFMPEG_VTB=OFF
}

if {${os.platform} ne "darwin"} {
    configure.args-delete \
                    -DUSE_ALSA=OFF \
                    -DUSE_PORTAUDIO=ON \
                    -DUSE_XVIDEO=OFF
}

configure.cppflags-append \
                    -DTAGLIB_FULL_INCLUDE_PATH

# Undefined symbols for architecture ppc: "___atomic_fetch_add_8"
if {[string match *gcc* ${configure.compiler}] && ${configure.build_arch} in [list arm i386 ppc]} {
    configure.ldflags-append \
                    -latomic
}

# FIXME: specifically on Tiger this fails:
# OpenGL2Common.cpp:258:44: error: call of overloaded 'setUniformValue(const char*, int)' is ambiguous
# Disable for now, GL is optional anyway.
platform darwin 8 {
    configure.args-replace \
                    -DUSE_OPENGL=ON -DUSE_OPENGL=OFF
}

post-destroot {
    system "install_name_tool -id ${applications_dir}/${real_name}.app/Contents/MacOS/libqmplay2.dylib \
            ${destroot}${applications_dir}/${real_name}.app/Contents/MacOS/libqmplay2.dylib"
    system "install_name_tool -change @rpath/libqmplay2.dylib \
            ${applications_dir}/${real_name}.app/Contents/MacOS/libqmplay2.dylib \
            ${destroot}${applications_dir}/${real_name}.app/Contents/MacOS/${real_name}"
    foreach so [ exec find ${destroot}${applications_dir}/${real_name}.app/Contents/MacOS/modules -name "\*.so" ] {
            regsub ":$" ${so} "" destroot_so_path
            regsub ${destroot} ${destroot_so_path} "" so_path
            system "install_name_tool -id ${so_path} ${destroot_so_path}"
            system "install_name_tool -change @rpath/libqmplay2.dylib ${applications_dir}/${real_name}.app/Contents/MacOS/libqmplay2.dylib \
                ${destroot_so_path}"
    }
}

notes "
${real_name} expects to have yt-dlp in ~/.qmplay2/ and use it for YouTube playback.\
Please install it with `sudo port install yt-dlp`.\
After installation and initial launch you could make a symlink to PPCPorts-provided yt-dlp:\

ln -s ${prefix}/bin/yt-dlp ~/.qmplay2/yt-dlp

On resource-constrained systems you may find that disabling video-to-audio sync\
in Playback settings improves experience.
"

# Untested:
variant pulse description "Use Pulseaudio" {
    depends_lib-append \
                    port:pulseaudio
    configure.args-replace \
                    -DUSE_PULSEAUDIO=OFF -DUSE_PULSEAUDIO=ON
}

variant lto description "Enable LTO" {
    # Does not seem to do anything on its own.
    configure.args-replace \
                    -DUSE_LINK_TIME_OPTIMIZATION=OFF \
                    -DUSE_LINK_TIME_OPTIMIZATION=ON
    # So just pass flags explicitly:
    configure.cxxflags-append \
                    -flto
    configure.ldflags-append \
                    -flto
}

variant G4 conflicts G5 description "Optimize for G4" {
    configure.cxxflags-append \
                    -mcpu=G4 -mtune=G4
}

variant G5 conflicts G4 description "Optimize for G5" {
    configure.cxxflags-append \
                    -mcpu=970 -mtune=970

    # https://github.com/iains/gcc-14-branch/issues/24
    if {${configure.build_arch} ne "ppc64"} {
        configure.cxxflags-append \
                    -mno-powerpc64
    }
}

if {${os.platform} ne "darwin" || ${os.major} > 9} {
    default_variants-append +lto
}

# ppc64 can only be built on a G5, so default to that.
if {${os.platform} eq "darwin" && ${configure.build_arch} eq "ppc64"} {
    default_variants-append +G5
}
