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

name                QMPlay2

# Does not build with Qt5 on 10.10 and below.
if {${os.platform} eq "darwin" && ${os.major} < 15} {
    PortGroup       qt4 1.0

    github.setup    zaps166 QMPlay2 b76e6ea46b4a32bb2becc9444ddc6f5fdff699cd
    version         18.12.08
    revision        1
    checksums       rmd160  721638bc3641199387ec5e371dd22ddabe3a1c92 \
                    sha256  2f1f9af17e7a0dd0ce6a12eb35f425cbc43b6869cb9a32bedfef3cca70908bc6 \
                    size    1228156
    github.tarball_from archive

    # These are added to support YouTube:
    depends_lib-append \
                    port:qjson4
    depends_run-append \
                    port:yt-dlp

    patchfiles-append \
                    0001-macOS-unbreak-build-with-Qt4.patch \
                    0002-MainWidget-fix-for-missing-qt_mac_set_dock_menu.patch \
                    0003-ScreenSaver-fix-IOKit-defines.patch \
                    0004-PlayClass-do-not-use-QRawFont.patch \
                    0005-Revert-some-broken-code.patch \
                    0006-FFMpeg-fix-compatibility-with-modern-FFMpeg.patch \
                    0007-Fix-YT-search.patch \
                    0008-Switch-to-yt-dlp.patch \
                    0009-PortAudio-Allow-bit-perfect-audio-output-for-macOS-1.patch \
                    0010-Do-not-force-fetching-yt-dlp-it-does-not-work-use-Ma.patch \
                    0011-YouTubeDL-move-to-QJson.patch \
                    0012-Fix-Qt-paths.patch

    configure.args-append \
                    -DDEFAULT_QT5=OFF \
                    -DUSE_FFMPEG_VTB=OFF \
                    -DUSE_JEMALLOC=OFF \
                    -DUSE_OPENGL2=OFF \
                    -DUSE_TEKSTOWO=ON

    compiler.cxx_standard   2011

    post-destroot {
        system "install_name_tool -id ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot}${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib"
        system "install_name_tool -change ${prefix}/lib/libqmplay2.dylib \
                    ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot}${applications_dir}/${name}.app/Contents/MacOS/QMPlay2"
        foreach dylib [ exec find ${destroot}${applications_dir}/${name}.app/Contents/MacOS/modules -name "\*.dylib" ] {
            regsub ":$" ${dylib} "" destroot_dylib_path
            regsub ${destroot} ${destroot_dylib_path} "" dylib_path
            system "install_name_tool -id ${dylib_path} ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libqmplay2.dylib ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                ${destroot_dylib_path}"
        }
    }

    notes "
    ${name} expects to have yt-dlp in ~/.qmplay2/ and use it for YouTube playback.\
    After installation you could make a symlink to MacPorts-provided yt-dlp:\
    \
    ln -s ${prefix}/bin/yt-dlp ~/.qmplay/
    "

    variant jemalloc description "Use Jemalloc" {
        depends_lib-append \
                    port:jemalloc
        configure.args-replace \
                    -DUSE_JEMALLOC=OFF -DUSE_JEMALLOC=ON
    }
} else {
    PortGroup       qt5 1.0

    qt5.depends_component qtdeclarative qttools qttranslations

    # Support for Qt5 < 5.10 has been dropped in 21.03.09:
    # https://github.com/zaps166/QMPlay2/commit/02684fec6217ac87a37dbd40d7b07683122e5997
    if {${os.platform} eq "darwin" && ${os.major} < 19} {
        github.setup    zaps166 QMPlay2 20.12.16
        revision        1
        checksums       rmd160  582ec7368e582b4b29280d6c76d4dc54d1fd0bb3 \
                        sha256  d6a5425f552e33a70b254ed27631878f20ea92850ccf221c2f2322200783d278 \
                        size    1564588
        github.tarball_from releases
        distname        ${name}-src-${version}

        patchfiles-append \
                        1001-Fix-Qt-paths.patch
     } else {
        github.setup    zaps166 QMPlay2 24.12.06
        revision        0
        checksums       rmd160  9d6026b129cd43595025ebb4dd4f042d42a53ddb \
                        sha256  dc59d9b87f3af6753195be1793a2e10aebf3ad59d20e2e43a5c21e10a46c7cec \
                        size    2058012
        github.tarball_from releases
        distname        ${name}-src-${version}
        # Unsupported generator Unix Makefiles. Ninja is the only supported generator.
        cmake.generator Ninja

        patchfiles-append \
                        2001-Fix-Qt-paths.patch

        configure.args-append \
                        -DBUILD_WITH_QT6=OFF
    }

    use_xz          yes

    configure.args-append \
                    -DCMAKE_LINK_DEPENDS_NO_SHARED=OFF \
                    -DSET_INSTALL_RPATH=OFF \
                    -DUSE_LYRICS=ON \
                    -DUSE_OPENGL=ON \
                    -DUSE_RUBBERBAND=ON \
                    -DUSE_UPDATES=OFF

    # VTBOpenGL.cpp: error: use of undeclared identifier 'kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange'
    if {${os.platform} eq "darwin" && ${os.major} < 17} {
        configure.args-append \
                    -DUSE_FFMPEG_VTB=OFF
    }

    depends_lib-append \
                    port:rubberband

    compiler.cxx_standard   2014

    post-destroot {
        system "install_name_tool -id ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot}${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib"
        if {${os.platform} eq "darwin" && ${os.major} < 19} {
            system "install_name_tool -change ${prefix}/lib/libqmplay2.dylib \
                    ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot}${applications_dir}/${name}.app/Contents/MacOS/QMPlay2"
            foreach dylib [ exec find ${destroot}${applications_dir}/${name}.app/Contents/MacOS/modules -name "\*.dylib" ] {
                regsub ":$" ${dylib} "" destroot_dylib_path
                regsub ${destroot} ${destroot_dylib_path} "" dylib_path
                system "install_name_tool -id ${dylib_path} ${destroot_dylib_path}"
                system "install_name_tool -change ${prefix}/lib/libqmplay2.dylib ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot_dylib_path}"
            }
        } else {
            system "install_name_tool -change @rpath/libqmplay2.dylib \
                    ${applications_dir}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot}${applications_dir}/${name}.app/Contents/MacOS/QMPlay2"
            foreach so [ exec find ${destroot}${applications_dir}/${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}/${name}.app/Contents/MacOS/libqmplay2.dylib \
                    ${destroot_so_path}"
            }
        }
    }
}

categories          multimedia
license             LGPL-3
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
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.

depends_lib-append  path:lib/libavcodec.dylib:ffmpeg \
                    path:lib/pkgconfig/libass.pc:libass \
                    port:libcddb \
                    port:libcdio \
                    port:portaudio \
                    port:taglib \
                    port:zlib

post-patch {
    reinplace "s|@destroot@|${destroot}|" ${worksrcpath}/src/gui/CMakeLists.txt
    reinplace "s|@prefix@|${prefix}|g" ${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
}

configure.args-append \
                    -DCMAKE_INSTALL_PREFIX=${applications_dir} \
                    -DUSE_ALSA=OFF \
                    -DUSE_AUDIOCD=ON \
                    -DUSE_CHIPTUNE_GME=OFF \
                    -DUSE_CHIPTUNE_SID=OFF \
                    -DUSE_FFMPEG=ON \
                    -DUSE_LASTFM=ON \
                    -DUSE_LIBASS=ON \
                    -DUSE_PORTAUDIO=ON \
                    -DUSE_PULSEAUDIO=OFF \
                    -DUSE_TAGLIB=ON \
                    -DUSE_VISUALIZATIONS=OFF \
                    -DUSE_XVIDEO=OFF

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