# -*- 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               github 1.0
PortGroup               meson 1.0

github.setup            vapoursynth vapoursynth 82 R
github.tarball_from     archive
worksrcdir              ${name}-R${version}
revision                0

description             A video processing framework with simplicity in mind

long_description        VapourSynth is an application for video manipulation. \
                        Or a plugin. Or a library. It’s hard to tell because \
                        it has a core library written in C++ and a Python \
                        module to allow video scripts to be created. The \
                        software has been heavily inspired by Avisynth.

set glslang_version     1.4.357.0
set glslang_distfile    glslang-vulkan-sdk-${glslang_version}.tar.gz
master_sites-append     https://codeload.github.com/KhronosGroup/glslang/tar.gz/refs/tags/vulkan-sdk-${glslang_version}?dummy=:glslang
distfiles-append        ${glslang_distfile}:glslang

checksums               ${distname}${extract.suffix} \
                        rmd160  e7f4b3f8aad322ced4a134b1e494f6523640c765 \
                        sha256  8994fbcfa2fcaa5dc94109e547060521f60829a0899747e98e351bed72bd9611 \
                        size    1138823 \
                        ${glslang_distfile} \
                        rmd160  3f9165ad372e614ed3f6817bd2c8b6b9c09b2c2c \
                        sha256  81038794e20494556edbcc0fc70fa984d71d1b440f9c49adf2cbaaa60a519757 \
                        size    4412438

categories              multimedia python
# The statically linked glslang sources carry additional permissive licenses.
license                 LGPL-2.1+ BSD MIT Apache-2
maintainers             {gmail.com:herby.gillot @herbygillot} \
                        openmaintainer

compiler.cxx_standard   2020
compiler.thread_local_storage yes

set python_branch       3.14
set python_version      [string map {. {}} ${python_branch}]

patchfiles-append       patch-src-vspipe-double-to-string.diff

depends_build-append    path:bin/pkg-config:pkgconfig \
                        port:py${python_version}-cython \
                        port:vulkan-headers

depends_lib-append      port:python${python_version} \
                        port:zimg

post-extract {
    move ${workpath}/glslang-vulkan-sdk-${glslang_version} ${worksrcpath}/subprojects/glslang
    copy ${worksrcpath}/subprojects/packagefiles/glslang/meson.build \
        ${worksrcpath}/subprojects/glslang/meson.build
    copy ${worksrcpath}/subprojects/packagefiles/glslang/glslang/meson.build \
        ${worksrcpath}/subprojects/glslang/glslang/meson.build
    file mkdir ${workpath}/bin
    ln -s ${prefix}/bin/cython-${python_branch} ${workpath}/bin/cython
}

post-patch {
    reinplace {s|is_system: true|is_system: false|} \
        ${worksrcpath}/subprojects/glslang/meson.build
    reinplace "s|find_installation(pure: false)|find_installation('${prefix}/bin/python${python_branch}', pure: false)|" \
        ${worksrcpath}/meson.build
    reinplace {/^ *"\/opt\/homebrew\/lib\/"/d; /^ *"\/usr\/local\/lib\/"/d} \
        ${worksrcpath}/src/core/vsvulkan.cpp
    reinplace "s|/opt/local/lib/|${prefix}/lib/|g" ${worksrcpath}/src/core/vsvulkan.cpp
}

configure.env-append    PATH=${workpath}/bin:$env(PATH)
build.env-append        PATH=${workpath}/bin:$env(PATH)
meson.wrap_mode         nodownload
# Older Apple linkers cannot resolve the embedded glslang archive with LTO.
configure.args-append   -Db_lto=false \
                        -Dpython.platlibdir=${frameworks_dir}/Python.framework/Versions/${python_branch}/lib/python${python_branch}/site-packages

# R82 installs its libraries and headers inside the Python package. Retain
# the old development paths for ports that link against VapourSynth.
post-destroot {
    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 ${worksrcpath}/subprojects/glslang/LICENSE.txt \
        ${destroot}${prefix}/share/doc/${name}/glslang-LICENSE.txt
    set vsdir ${frameworks_dir}/Python.framework/Versions/${python_branch}/lib/python${python_branch}/site-packages/vapoursynth
    xinstall -d ${destroot}${prefix}/bin ${destroot}${prefix}/lib/pkgconfig ${destroot}${prefix}/include
    ln -s ${vsdir}/vspipe ${destroot}${prefix}/bin/vspipe
    ln -s ${vsdir}/include ${destroot}${prefix}/include/vapoursynth
    foreach lib {libvapoursynth libvsscript} {
        foreach file [glob -nocomplain -tails -directory ${destroot}${vsdir} ${lib}*.dylib] {
            ln -s ${vsdir}/${file} ${destroot}${prefix}/lib/${file}
        }
    }
    ln -s ${vsdir}/libvsscript.dylib ${destroot}${prefix}/lib/libvapoursynth-script.dylib
    set pc [open ${destroot}${prefix}/lib/pkgconfig/vapoursynth.pc w]
    puts $pc "prefix=${prefix}\nincludedir=\${prefix}/include/vapoursynth\nlibdir=\${prefix}/lib\n\nName: vapoursynth\nDescription: A video processing framework\nVersion: ${version}\nLibs: -L\${libdir} -lvapoursynth\nCflags: -I\${includedir}"
    close $pc
    set pc [open ${destroot}${prefix}/lib/pkgconfig/vapoursynth-script.pc w]
    puts $pc "prefix=${prefix}\nincludedir=\${prefix}/include/vapoursynth\nlibdir=\${prefix}/lib\n\nName: vapoursynth-script\nDescription: VapourSynth Python scripting library\nVersion: ${version}\nRequires: vapoursynth\nLibs: -L\${libdir} -lvapoursynth-script\nCflags: -I\${includedir}"
    close $pc
}

github.livecheck.regex  {([0-9]+)}
