# -*- 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             101.0
revision            1
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  0247f66a4556f7c7a214a8e81e70cf5b723afc90 \
                    sha256  f638b299e49cdd4efc4ab3c68dbb593ed6a61bd01bf8862da74ef7fb4d181ce8 \
                    size    11159732

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

set version_ruby    3.4
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.87

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

patchfiles-append   patch-package-data-path.diff

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.1

    app.name        MKVToolNix
    app.executable  mkvtoolnix-gui
    app.icon        ${worksrcpath}/share/icons/macos/MKVToolNix.icns
    app.manual_post_destroot \
                    yes

    set ::app_info_plist \
                    ${destroot}${applications_dir}/[option app.name].app/Contents/Info.plist
    # Keep these declarations in sync with packaging/macos/Info.plist
    set mtxcfg_uti  [option app.identifier].mtxcfg
    set ::document_types [list \
        [list "MKVToolNix GUI settings file" "MKVToolNix.icns" "Editor" "Owner" [list ${mtxcfg_uti}]] \
    ]
    set ::exported_type_declarations [list \
        [list "MKVToolNix GUI settings file" [list "public.text"] ${mtxcfg_uti} \
            [list "application/x-mkvtoolnix-gui-settings"] [list "mtxcfg"]] \
    ]

    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

    post-destroot {
        app::post_destroot

        set plistbuddy /usr/libexec/PlistBuddy

        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes array" ${app_info_plist}
        set i 0
        foreach document_type ${document_types} {
            lassign ${document_type} type_name icon_file type_role handler_rank content_types

            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i} dict" ${app_info_plist}
            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:LSItemContentTypes array" ${app_info_plist}

            set j 0
            foreach content_type ${content_types} {
                exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:LSItemContentTypes:${j} string ${content_type}" ${app_info_plist}
                incr j
            }

            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeIconFile string ${icon_file}" ${app_info_plist}
            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeName string ${type_name}" ${app_info_plist}
            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeRole string ${type_role}" ${app_info_plist}
            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:LSHandlerRank string ${handler_rank}" ${app_info_plist}

            incr i
        }

        exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations array" ${app_info_plist}
        set i 0
        foreach type_declaration ${exported_type_declarations} {
            lassign ${type_declaration} type_description conforms_to type_identifier mime_types extensions

            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i} dict" ${app_info_plist}
            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeDescription string ${type_description}" ${app_info_plist}
            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeConformsTo array" ${app_info_plist}

            set j 0
            foreach conformance ${conforms_to} {
                exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeConformsTo:${j} string ${conformance}" ${app_info_plist}
                incr j
            }

            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeIdentifier string ${type_identifier}" ${app_info_plist}
            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeTagSpecification dict" ${app_info_plist}
            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeTagSpecification:public.mime-type array" ${app_info_plist}

            set j 0
            foreach mime_type ${mime_types} {
                exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeTagSpecification:public.mime-type:${j} string ${mime_type}" ${app_info_plist}
                incr j
            }

            exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeTagSpecification:public.filename-extension array" ${app_info_plist}

            set j 0
            foreach extension ${extensions} {
                exec ${plistbuddy} -c "Add :UTExportedTypeDeclarations:${i}:UTTypeTagSpecification:public.filename-extension:${j} string ${extension}" ${app_info_plist}
                incr j
            }

            incr i
        }

    }
}

default_variants    +qtgui

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