# -*- 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        0
    checksums       rmd160  721638bc3641199387ec5e371dd22ddabe3a1c92 \
                    sha256  2f1f9af17e7a0dd0ce6a12eb35f425cbc43b6869cb9a32bedfef3cca70908bc6 \
                    size    1228156
    github.tarball_from archive

    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-Qt-paths.patch

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

    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}"
        }
    }

    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} < 15} {
        github.setup    zaps166 QMPlay2 20.12.16
        revision        0
        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.05.23
        revision        0
        checksums       rmd160  45be92cd3a32f4adba4f323aa04391283f3d8bf9 \
                        sha256  4dd650b1b6507207ffcfcb57a454b2150b0e48a8efe649233ea2d5a16209a2be \
                        size    2040496
        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"
        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"
        if {${os.platform} eq "darwin" && ${os.major} < 15} {
            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 {
            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 ${prefix}/lib/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_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
}
