# -*- 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           active_variants 1.1
PortGroup           cmake 1.1
PortGroup           conflicts_build 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1

# strnlen
legacysupport.newest_darwin_requires_legacy 10

set myname          rawtherapee
name                ${myname}-devel
github.setup        RawTherapee RawTherapee 039b9b89d43315be6b42e8fbb33b8cfb39edd4bf
version             2026.05.23
revision            0
conflicts           ${myname}

categories          graphics
license             GPL-3
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

homepage            https://www.${myname}.com
description         Raw photo processing program
long_description    ${github.project} is a powerful, cross-platform \
                    ${description}. It is designed for developing raw \
                    files from a broad range of digital cameras, as \
                    well as HDR DNG files and non-raw image formats \
                    (JPEG, TIFF and PNG).

checksums           rmd160  23a83fceec473a4e6f72f63edeca625d8126e9a2 \
                    sha256  2bf987d6b8088284e1699276e6a6daa67f3ac6195744ff9fe801af08d6656506 \
                    size    82986156
github.tarball_from archive

cmake.generator     Ninja

compiler.cxx_standard   2011
compiler.openmp_version 3.1

depends_build-append    port:autoconf \
                        port:automake \
                        path:bin/git:git \
                        port:libtool \
                        path:bin/pkg-config:pkgconfig

# TODO: support using our libfmt12.
depends_lib-append      port:adwaita-icon-theme \
                        path:lib/pkgconfig/cairo.pc:cairo \
                        path:lib/pkgconfig/cairomm-1.0.pc:cairomm-1.0 \
                        port:exiv2 \
                        port:expat \
                        port:fftw-3 \
                        port:fftw-3-single \
                        path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                        path:lib/pkgconfig/glib-2.0.pc:glib2 \
                        port:glibmm-2.4 \
                        path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                        port:gtkmm3 \
                        port:lcms2 \
                        port:lensfun \
                        port:libiptcdata \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:libjxl \
                        port:libpng \
                        port:libraw \
                        port:libsigcxx2 \
                        port:lunasvg \
                        port:tiff \
                        port:zlib

patchfiles-append       patch-config_h_in.diff

# This is still broken...
post-patch {
    reinplace "s|-mtune=generic||g" ${worksrcpath}/CMakeLists.txt
}

pre-configure {
    if {![variant_isset quartz] && ![variant_isset x11]} {
        error "Either +x11 or +quartz is required"
    }
}

set app_contents ${applications_dir}/${github.project}.app/Contents

configure.args-append   -DBUILD_SHARED=ON \
                        -DOPTION_OMP=OFF \
                        -DUSE_LIBCANBERRA=OFF \
                        -DWITH_JXL=ON \
                        -DWITH_LTO=OFF \
                        -DWITH_SIMDE=OFF \
                        -DWITH_SYSTEM_LIBRAW=ON

# https://github.com/RawTherapee/RawTherapee/issues/7060
configure.args-append   -DSVG_BACKEND=lunasvg \
                        -DWITH_SYSTEM_LUNASVG=ON

# TODO: add optimized variants.
if {${configure.build_arch} eq "x86_64"} {
    configure.args-append   -DPROC_TARGET_NUMBER=1 \
                            -DPROC_LABEL="generic processor"
} elseif {${configure.build_arch} in [list arm64 ppc ppc64]} {
    configure.args-append   -DPROC_TARGET_NUMBER=2
}

if {[variant_isset debug]} {
    cmake.build_type    Debug
    configure.optflags  -O0
} else {
    cmake.build_type    RelWithDebInfo

    # Clear optflags for non-debug build; controlled by project
    configure.optflags
}

variant openmp description "Enable OpenMP" {
    # It does not work on aarch64 at the moment:
    # app builds, but segfaults on launch.
    # Unknown status on PowerPC.

    require_active_variants fftw-3          openmp
    require_active_variants fftw-3-single   openmp

    configure.args-replace \
                        -DOPTION_OMP=OFF -DOPTION_OMP=ON
}

variant lto description "Build with LTO" {
    # PortGroup           LTO 1.0

    configure.args-replace \
                        -DWITH_LTO=OFF -DWITH_LTO=ON
}

# No point in this on 32-bit at the moment.
variant quartz conflicts x11 {
    supported_archs     x86_64 arm64

    depends_lib-append  port:gtk-osx-application-gtk3

    post-patch {
        copy ${filespath}/FindGtkMacIntegration.cmake \
            ${worksrcpath}/cmake/modules/
        reinplace {s/MacIntegration/GtkMacIntegration/} \
            ${worksrcpath}/rtgui/CMakeLists.txt
    }

    configure.args-append \
                        -DBUNDLE_BASE_INSTALL_DIR=$app_contents/MacOS \
                        -DDATADIR=$app_contents/Resources \
                        -DLIBDIR=$app_contents/Frameworks

    require_active_variants gtk3    quartz
    require_active_variants gtkmm3  quartz
}

variant x11 conflicts quartz {
    legacysupport.redirect_bins rawtherapee rawtherapee-cli

    configure.args-append \
                        -DDATADIR=${prefix}/share/${myname} \
                        -DMACOS_NATIVE=OFF

    require_active_variants gtk3    x11
    require_active_variants gtkmm3  x11

    patchfiles-append   0001-Do-not-force-Cocoa-build.patch \
                        0002-Allow-non-Cocoa-code-in-sources.patch \
                        0003-patch-options.cc.diff

    post-patch {
        reinplace "s|@PREFIX@|${prefix}|" ${worksrcpath}/rtgui/options.cc
    }

    # Having wasted many hours on this silly issue, I think it makes
    # sense to display a warning which may save someone's time and sanity.
    notes "
    On the first run the app expects a user to confirm the launch\
    in a modal window. Some versions of X server may display that window\
    beneath the main one. Interface controls will not respond until\
    the modal window is closed. If you find yourself in such a situation,\
    move the main X window aside.
    "
}

# +quartz requires non-default variants of its dependencies,
# so it will be broken on buildbots.
if {![variant_isset quartz]} {
    default_variants-append +x11
}

if {${configure.build_arch} ni [list i386 ppc ppc64] \
    && ![variant_isset quartz]} {
    notes-append "
    Consider installing ${name} with macOS native interface:\
    sudo port upgrade ${name} -x11 +quartz.\
    It may offer a better user experience.
    "
}

post-destroot {
    # The following steps somewhat mimic certain parts of
    # the ${worksrcpath}/tools/osx/macosx_bundle.sh script. However,
    # we only selectively perform some of the steps from the upstream-
    #provided script because the 'macosx_bundle.sh' script performs many
    # undesirable actions, such as actually copying ALL of RawTherapee's
    # dependencies' library files directly into the .app bundle, and
    # then using install_name_tool to retroactively modify the @rpaths
    # in the binary executable to point to these local copies of dylibs.
    # In addition, their script also packs the .app bundle into a DMG,
    # so it's pretty safe to assume that their script is meant to be
    # used to generate a fully portable installer for distribution.

    # XDG = X Desktop Group, now known as freedesktop.org

    # Technically, we can build app bundle with gtk+x11,
    # but that would complicate making wrappers for libstdc++ systems,
    # while perhaps all users of modern macOS will use +quartz anyway.
    if {[variant_isset quartz]} {
        set xdg_share_root ${destroot}${prefix}/share

        set rt_data_home $xdg_share_root/${myname}
        ln -s $app_contents/Resources $rt_data_home

        set xdg_desktop_entries $xdg_share_root/applications
        xinstall -d $xdg_desktop_entries
        ln -s $app_contents/Resources/applications/${myname}.desktop \
            $xdg_desktop_entries/

        set xdg_desktop_icons $xdg_share_root/icons/hicolor
        xinstall -d $xdg_desktop_icons
        set icon_resolutions [list \
            16x16 24x24 32x32 48x48 64x64 128x128 256x256 scalable \
        ]
        foreach ico_res $icon_resolutions {
            xinstall -d $xdg_desktop_icons/$ico_res/apps
            ln -s $app_contents/Resources/icons/hicolor/$ico_res/apps/${myname}.png \
                $xdg_desktop_icons/$ico_res/apps/
        }

        set xdg_metainfo $xdg_share_root/metainfo
        xinstall -d $xdg_metainfo
        ln -s $app_contents/Resources/metainfo/com.${myname}.${github.project}.appdata.xml \
            $xdg_metainfo/

        set relative_manpath man/man1
        ln -s $app_contents/Resources/$relative_manpath/${myname}.1 \
            $xdg_share_root/$relative_manpath/

        set osx_tools ${worksrcpath}/tools/osx

        copy $osx_tools/PkgInfo ${destroot}$app_contents/

        # We overwrite the Info.plist file generated by the CMake build
        # with the one provided in the RawTherapee source code, because the
        # one provided by the devs is more complete than the one generated
        # during the course of the build.
        xinstall -m 644 $osx_tools/Info.plist.in \
            ${destroot}$app_contents/Info.plist
        set regexes [list \
            "s/@minimum_arm64_version@/11.0/" \
            "s/@minimum_x86_64_version@/10.6/" \
            "s/@version@/${version}/" \
            "s/@shortVersion@/${version}/" \
            "/XDG_CONFIG_HOME/,/XDG_DATA_DIRS/s|\(<string>\).*\(</string>\)|\\1${prefix}/share/${myname}\\2|" \
            "/XDG_DATA_HOME/,/GSETTINGS_SCHEMA_DIR/s|\(<string>\).*\(</string>\)|\\1${prefix}/share/${myname}\\2|" \
            "s|/Applications/${github.project}.app/Contents/Resources|${prefix}|" \
            "/GDK_PIXBUF_MODULEDIR/,/LIBDIR/s|\(<string>\).*\(</string>\)|\\1${prefix}/lib/gdk-pixbuf-2.0/2.10.0/loaders\\2|" \
            "/DATADIR/,/GDK_RENDERING/s|${prefix}|$app_contents/Resources|" \
        ]
        foreach re $regexes {
            reinplace -E $re ${destroot}$app_contents/Info.plist
        }

        # Copy the .icns files into the app bundle
        set icns_files [list ${myname} profile]
        foreach icns_file $icns_files {
            copy $osx_tools/${icns_file}.icns \
                ${destroot}$app_contents/Resources/
        }

        ln -s $app_contents/MacOS/${myname}     ${destroot}${prefix}/bin/
        ln -s $app_contents/MacOS/${myname}-cli ${destroot}${prefix}/bin/
    }
}

proc update_desktop_env {} {
    global prefix

    if {![catch {registry_active desktop-file-utils}]} {
        system [join [list \
            ${prefix}/bin/update-desktop-database \
            ${prefix}/share/applications \
        ] " "]
    }
    system [join [list \
        ${prefix}/bin/glib-compile-schemas \
        ${prefix}/share/glib-2.0/schemas \
    ] " "]
    if {![catch {registry_active shared-mime-info}]} {
        system [join [list \
            "env XDG_DATA_DIRS=${prefix}/share" \
            "${prefix}/bin/update-mime-database -V ${prefix}/share/mime" \
        ] " "]
    }
}

post-activate {
    update_desktop_env
}

post-deactivate {
    update_desktop_env
}

variant tcmalloc \
    description {Use Google's TCMalloc library for memory allocation} \
{
    depends_lib-append      port:gperftools
    configure.args-append   -DENABLE_TCMALLOC=ON
}

# Exclude beta/rc releases from livecheck
github.livecheck.regex {([0-9\.]+)}
