# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           codeberg 1.0
PortGroup           meson 1.0

codeberg.setup      valos Komikku 51.1.0 v
name                [string tolower ${codeberg.project}]
revision            0
categories          gnome graphics manga net
license             GPL-3+
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

description         Manga reader for GNOME
long_description    {*}${description}
checksums           rmd160  51a9b45e21e078cc437c48de2704dd0f0775d1fe \
                    sha256  f111171093b044b1ae3765c27edd2d5e874c2607e3ce087f70bc642886334eec \
                    size    5095300

meson.wrap_mode     nodownload

set py_ver          3.14
set py_ver_nodot    [string map {. {}} ${py_ver}]

configure.python    ${prefix}/bin/python${py_ver}

patch.pre_args-replace  -p0 -p1

patchfiles-append   0001-fix-meson.build.patch

post-patch {
    reinplace "s|@PYTHON@|${configure.python}|" ${worksrcpath}/meson.build
}

depends_build-append \
                    port:appstream-glib \
                    port:gettext \
                    port:blueprint-compiler \
                    path:bin/pkg-config:pkgconfig

# This is to avoid a conflict with py-urllib3-future port:
set urllib_path     ${frameworks_dir}/Python.framework/Versions/${py_ver}/lib/python${py_ver}/site-packages/urllib3

depends_lib-append  path:lib/pkgconfig/cairo.pc:cairo \
                    path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                    port:gettext-runtime \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    path:lib/pkgconfig/gobject-introspection-1.0.pc:gobject-introspection \
                    port:graphene \
                    path:lib/pkgconfig/gtk4.pc:gtk4 \
                    port:gtksourceview5 \
                    path:lib/pkgconfig/libadwaita-1.pc:libadwaita \
                    port:libnotify \
                    path:lib/pkgconfig/libsoup-3.0.pc:libsoup \
                    path:lib/pkgconfig/pango.pc:pango \
                    port:py${py_ver_nodot}-beautifulsoup4 \
                    port:py${py_ver_nodot}-brotli \
                    port:py${py_ver_nodot}-colorthief \
                    port:py${py_ver_nodot}-cryptography \
                    port:py${py_ver_nodot}-dateparser \
                    port:py${py_ver_nodot}-ebooklib \
                    port:py${py_ver_nodot}-emoji \
                    port:py${py_ver_nodot}-gobject3 \
                    port:py${py_ver_nodot}-jwt \
                    port:py${py_ver_nodot}-jxlpy \
                    port:py${py_ver_nodot}-keyring \
                    port:py${py_ver_nodot}-lxml \
                    port:py${py_ver_nodot}-magic \
                    port:py${py_ver_nodot}-natsort \
                    port:py${py_ver_nodot}-piexif \
                    port:py${py_ver_nodot}-Pillow \
                    port:py${py_ver_nodot}-pypdf \
                    port:py${py_ver_nodot}-pytesseract \
                    port:py${py_ver_nodot}-rarfile \
                    port:py${py_ver_nodot}-regex \
                    port:py${py_ver_nodot}-requests \
                    port:py${py_ver_nodot}-tzlocal \
                    port:py${py_ver_nodot}-unidecode \
                    path:${urllib_path}/__init__.py:py${py_ver_nodot}-urllib3

depends_run-append  port:dbus \
                    port:desktop-file-utils \
                    port:gsettings-desktop-schemas \
                    port:hicolor-icon-theme \
                    port:webp-pixbuf-loader

# https://codeberg.org/valos/Komikku/src/commit/d7392404125e43e66f49ad22425f5a66618f4b03/requirements-optional.txt
# On 10.5 curl-impersonate may not yet build. 10.6 ppc is good.
if {${os.platform} ne "darwin" && ${os.major} > 9} {
    depends_run-append \
                    port:py${py_ver_nodot}-curl-cffi
}

# https://codeberg.org/valos/Komikku/issues/847
pre-configure {
    system -W ${worksrcpath} "${configure.python} ./scripts/remove_webview.py"
}

configure.args-append \
                    -Dprofile=default

post-activate {
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
    system "${prefix}/bin/gtk4-update-icon-cache -f -t ${prefix}/share/icons/hicolor"
    system "${prefix}/bin/glib-compile-schemas ${prefix}/share/glib-2.0/schemas"
}

notes "
Prior to launching the app, please start DBus sesstion from a non-root user:
launchctl load -w /Library/LaunchAgents/org.freedesktop.dbus-session.plist
"

# https://trac.macports.org/ticket/70137
# This is not exclusive to legacy systems.
notes-append "
You may need to export GSK_RENDERER=cairo to the environment, since GTK4 removed support for older OpenGL.
"

# New issue observed since 50.13.0, likely via some dependency.
# The crash is caused by a typical libstdc++ symbol conflict between old system one and modern libgcc.
if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
    notes-append "
    IMPORTANT: it is needed to run the app with `DYLD_LIBRARY_PATH=${prefix}/lib/libgcc komikku` now.
    Without DYLD_LIBRARY_PATH being set, it crashes on launch.
    "
}
