# -*- 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           muniversal 1.0
PortGroup           boost 1.0
PortGroup           qt6 1.0

# Please keep the mkvtoolnix and mkvtoolnix-devel ports as similar as
# possible.

# The developer does not accept macOS-specific bug reports, but does
# accept pull requests.

name                mkvtoolnix
conflicts           mkvtoolnix-devel mkvtoolnix-legacy
set my_name         mkvtoolnix

version             89.0
revision            0
categories          multimedia
maintainers         {ryandesign @ryandesign} {i0ntempest @i0ntempest} openmaintainer
license             GPL-2+ LGPL-2.1+

description         Matroska media files manipulation tools.
long_description    mkvtoolnix will evolve to a set of tools to create, \
                    alter and inspect Matroska files under Linux and other \
                    Unices, just what the OGMtools do for the OGM format.

homepage            https://mkvtoolnix.download
master_sites        ${homepage}/sources/
use_xz              yes

checksums           rmd160  2b1245b7dcd00e2ddbd2659091e9711a1e0cb9f5 \
                    sha256  6127b694b088e53270a946c178b6816da756200bdf672abdf65cfe0b50b260b2 \
                    size    11253572

distname            ${my_name}-${version}
dist_subdir         ${my_name}

set version_ruby    3.3
set version_ruby_mp [join [split $version_ruby "."] ""]

qt6.depends_lib     qtmultimedia \
                    qtsvg

depends_build       port:ruby${version_ruby_mp} \
                    port:docbook-xsl-nons \
                    port:pkgconfig \
                    port:libxslt \
                    port:po4a

depends_lib         port:bzip2 \
                    port:expat \
                    port:flac \
                    port:gettext-runtime \
                    port:libebml \
                    port:libmatroska \
                    port:libogg \
                    port:libvorbis \
                    port:lzo2 \
                    port:zlib \
                    port:pugixml \
                    port:cmark \
                    port:libdvdread \
                    port:gmp \
                    port:nlohmann-json

depends_run         port:ruby${version_ruby_mp}

depends_test        port:gtest

boost.version       1.81

set minosver 10.15
if {[vercmp ${macosx_deployment_target} ${minosver}] < 0} {
    known_fail yes
    pre-fetch {
        ui_error "${name} ${version} requires macOS ${minosver} to run."
        ui_error "Consider installing mkvtoolnix-legacy instead."
        return -code error "incompatible macOS version"
    }
}

post-patch {
    fs-traverse f ${worksrcpath} {
        set t [file tail ${f}]
        set e [file extension ${f}]
        if {${t} eq "Rakefile" || ${e} eq ".rb"} {
            reinplace -q -E "s|^#!/usr/bin/(env )?ruby|#!${prefix}/bin/ruby${version_ruby}|g" ${f}
        }
    }
}

compiler.cxx_standard \
                    2020
configure.cxxflags-append \
                    -std=c++20

set cxx_stdlibflags {}
if {[string match *clang* ${configure.cxx}]} {
    set cxx_stdlibflags -stdlib=${configure.cxx_stdlib}
}

build.cmd           rake${version_ruby}
build.target        default
# disable silent rules
build.args-append   V=1

configure.args      --mandir=${prefix}/share/man \
                    --with-boost=[boost::install_area] \
                    --with-extra-libs=${prefix}/lib \
                    --with-extra-includes=${prefix}/include \
                    --with-xsltproc=${prefix}/bin/xsltproc \
                    --with-docbook-xsl-root=${prefix}/share/xsl/docbook-xsl-nons \
                    --with-po4a=${prefix}/bin/po4a \
                    --with-po4a-translate=${prefix}/bin/po4a-translate \
                    --disable-update-check \
                    --disable-gui \
                    --with-qmake6=${qt6.dir}/bin/qmake6

configure.ldflags-append ${cxx_stdlibflags}

test.run            yes
test.cmd            rake
test.target         tests:unit

variant qtgui description {Build with the qt6 GUI} {
    PortGroup       app 1.0

    app.name        MKVToolNix
    app.executable  mkvtoolnix-gui
    app.icon        ${worksrcpath}/share/icons/256x256/mkvtoolnix-gui.png

    depends_build-append port:makeicns
    configure.args-replace \
                    --disable-gui \
                    --enable-gui
    configure.args-append \
                    --with-moc=${qt6.dir}/libexec/moc \
                    --with-uic=${qt6.dir}/libexec/uic \
                    --with-rcc=${qt6.dir}/libexec/rcc
}

default_variants    +qtgui

livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     ${my_name}-(\[0-9.\]+)${extract.suffix}