# -*- 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
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# filesystem
legacysupport.newest_darwin_requires_legacy 19

github.setup        jstkdng ueberzugpp 2.9.10 v
revision            1
categories          graphics
license             GPL-3
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         Drop in replacement for ueberzug written in C++
long_description    {*}${description}
checksums           rmd160  633f23c9c92800aacfcd5e5d4bd5a167da14e592 \
                    sha256  225b74ae3c71fd7d6dbd8b3a1740dacda95c40146a4fd00c792e4f18f200854d \
                    size    71764
github.tarball_from archive

installs_libs       no

compiler.cxx_standard   2023

# Match spdlog:
set libfmt_v        12
cmake.prefix_path-append \
                    ${prefix}/lib/libfmt${libfmt_v}/cmake \
                    ${prefix}/libexec/onetbb/lib/cmake/TBB

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:chafa \
                    port:CLI11 \
                    port:gettext-runtime \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:libfmt${libfmt_v} \
                    port:libsixel \
                    port:nlohmann-json \
                    port:onetbb \
                    port:range-v3 \
                    port:spdlog \
                    port:vips \
                    port:xorg-libxcb \
                    port:xorg-xcb-util-image

# OpenGL is really EGL, not GL.
configure.args-append \
                    -DENABLE_DBUS=OFF \
                    -DENABLE_OPENCV=OFF \
                    -DENABLE_OPENGL=OFF \
                    -DENABLE_TURBOBASE64=OFF \
                    -DENABLE_WAYLAND=OFF \
                    -DENABLE_X11=ON \
                    -DENABLE_XCB_ERRORS=OFF

if {[string match *gcc* ${configure.compiler}] && \
    ${configure.build_arch} in [list arm i386 ppc]} {
        configure.ldflags-append \
                    -latomic
}

post-destroot {
    if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
        # ueberzugpp is a symlink to ueberzug, delete it and install a wrapper instead:
        delete -f ${destroot}${prefix}/bin/${name}

        set  wrapper    [open "${destroot}${prefix}/bin/${name}" w 0755]
        puts ${wrapper} "#!/bin/sh"
        puts ${wrapper} ""
        puts ${wrapper} {if [ -n "$DYLD_LIBRARY_PATH" ]; then}
        puts ${wrapper} "   DYLD_LIBRARY_PATH=__PREFIX__/lib/libgcc:\${DYLD_LIBRARY_PATH}"
        puts ${wrapper} {else}
        puts ${wrapper} "   DYLD_LIBRARY_PATH=__PREFIX__/lib/libgcc"
        puts ${wrapper} {fi}
        puts ${wrapper} {export DYLD_LIBRARY_PATH}
        puts ${wrapper} ""
        puts ${wrapper} {exec "__PREFIX__/bin/ueberzug" "$@"}
        close $wrapper

        reinplace "s|__PREFIX__|${prefix}|g" ${destroot}${prefix}/bin/${name}
    }
}
