# -*- 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           compiler_blacklist_versions 1.0
PortGroup           perl5 1.0

name                gimp2
conflicts           gimp2-devel gimp3-devel
set my_name         gimp2

# please remember to update the gimp metapackage to match
version             2.10.36
revision            0
epoch               0

categories          graphics
license             GPL-3+
maintainers         {mascguy @mascguy}

description         The GNU Image Manipulation Program
long_description    The GNU Image Manipulation Program (GIMP) is a powerful \
                    tool for the preparation and manipulation of digital images. \
                    The GIMP provides the user with a wide variety of image \
                    manipulation, painting, processing, and rendering tools.
homepage            https://gimp.org/

set branch          [join [lrange [split ${version} .] 0 1] .]
master_sites        gimp:gimp/v${branch}/
dist_subdir         ${my_name}
distname            gimp-${version}
use_bzip2           yes

checksums           rmd160  8481bdd91fd0d475979387c70d548f8b6bdd4499 \
                    sha256  3d3bc3c69a4bdb3aea9ba2d5385ed98ea03953f3857aafd1d6976011ed7cdbb2 \
                    size    31532334

perl5.branches      5.34

depends_build-append \
                    port:appstream-glib \
                    port:gtk-doc \
                    port:intltool \
                    port:perl${perl5.major} \
                    port:pkgconfig

depends_lib-append \
                    port:aalib \
                    port:atk \
                    path:lib/pkgconfig/babl.pc:babl \
                    port:bzip2 \
                    port:curl \
                    port:dbus-glib \
                    port:desktop-file-utils \
                    port:fontconfig \
                    port:freetype \
                    path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                    path:lib/pkgconfig/gegl-0.4.pc:gegl \
                    port:gexiv2 \
                    port:ghostscript \
                    port:glib-networking \
                    path:lib/pkgconfig/gtk+-2.0.pc:gtk2 \
                    port:libheif \
                    port:iso-codes \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libjxl \
                    port:lcms2 \
                    port:libmng \
                    port:libmypaint \
                    port:mypaint-brushes1 \
                    port:openexr \
                    port:openjpeg \
                    port:libpng \
                    path:lib/pkgconfig/poppler.pc:poppler \
                    path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                    port:tiff \
                    port:webp \
                    port:libwmf \
                    port:libxml2 \
                    port:libxslt \
                    port:xdg-utils \
                    port:xorg-libXcursor \
                    port:xorg-libXmu \
                    port:xorg-libXext \
                    port:xorg-libXfixes \
                    port:xpm \
                    port:xz \
                    port:zlib

depends_run-append \
                    port:gimp-help-en

# gcc-4.2 5493 and 5666.3_13: gimpcpuaccel.c:180: error: can't find a register in class 'BREG' while reloading 'asm'
# redefinition of typedef is invalid in C [-Wtypedef-redefinition] (#50329)
# as of version 2.10.0 requires a C++14 compatible compiler to configure

compiler.cxx_standard 2014
compiler.blacklist-append *gcc-3.* *gcc-4.* {clang < 700}

configure.cflags-append \
                    -Wno-deprecated-declarations

patchfiles-append   patch-etc-gimprc.in.diff
patchfiles-append   patch-mach-task-info.diff
patchfiles-append   patch-quartz-32bit.diff
patchfiles-append   patch-mypaint_brushes_dir.diff

# Disable update check for 10.8 and earlier, due to lack of NSURLSession
# Upstream merge request: https://gitlab.gnome.org/GNOME/gimp/-/merge_requests/913
patchfiles-append   patch-updatecheck-nsurlsession.diff

platform darwin {
    if {${os.major} < 11} {
        # avoid Cursor type conflict between X11 and Quickdraw
        # error: typedef redefinition with different types ('struct Cursor' vs 'XID' (aka 'unsigned long'))
        # not an issue on 10.7 and later
        patchfiles-append \
                    patch-x11-widgets-fix.diff
    }

    if {${os.major} < 10 || (${os.major} == 10 && ${configure.build_arch} eq "ppc")} {
        patchfiles-append \
                    patch-fix-pthread.diff
        # gimplineart.c: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
        configure.cflags-append \
                    -std=gnu11
    }
}

use_autoreconf      yes
autoreconf.args     -fvi
configure.checks.implicit_function_declaration.whitelist-append strchr

pre-configure {
    if {[file exists ${prefix}/lib/gtk-2.0/include/gdkconfig.h]} {
        set gtk_not_quartz [catch {exec grep -q GDK_WINDOWING_QUARTZ ${prefix}/lib/gtk-2.0/include/gdkconfig.h}]
        set gtk_not_x11 [catch {exec grep -q GDK_WINDOWING_X11 ${prefix}/lib/gtk-2.0/include/gdkconfig.h}]
        if {[variant_isset quartz] && ${gtk_not_quartz}} {
            error "+quartz variant selected, but gtk2+x11 is installed. Install gtk2+quartz."
        } elseif {![variant_isset quartz] && ${gtk_not_x11}} {
            error "+quartz variant is not selected, but gtk2+quartz is installed. Install gtk2+x11."
        }
    } else {
            error "Cannot find gdkconfig.h"
    }
}

configure.perl      ${perl5.bin}

configure.env-append \
                    CC_FOR_BUILD=${configure.cc}

if {[vercmp $xcodeversion 4.3] < 0 && [string match "*macports*" ${configure.compiler}]} {
    # Xcode 4.2 fails with load commands in the newer toolchain
    depends_build-append port:cctools

    configure.env-append \
        AR=${prefix}/bin/ar \
        RANLIB=${prefix}/bin/ranlib \
        NM=${prefix}/bin/nm
    configure.args-append \
        lt_cv_path_NM=${prefix}/bin/nm
}

configure.args-append \
                    --build=${configure.build_arch}-apple-${os.platform}${os.version} \
                    --enable-mp \
                    --with-bug-report-url=https://guide.macports.org/#project.tickets \
                    --with-pdbgen \
                    --with-x \
                    --x-includes=${prefix}/include \
                    --x-libraries=${prefix}/lib \
                    --disable-silent-rules \
                    --disable-python \
                    --disable-check-update \
                    --without-alsa \
                    --without-gudev \
                    --without-webkit \
                    ac_cv_path_PERL=${configure.perl}

# keep empty GIMP font directory
# silences warning message on startup:
# GIMP-Message: Some fonts failed to load:
# - /opt/local/share/gimp/2.0/fonts/
destroot.keepdirs   ${destroot}${prefix}/share/gimp/2.0/fonts

variant python27 description {Build with python plugin support using python 2.7} {
    configure.args-delete     --disable-python
    configure.python          ${prefix}/bin/python2.7
    depends_lib-append        port:py27-pygtk
    set python_framework      ${frameworks_dir}/Python.framework/Versions/2.7
    configure.pkg_config_path ${python_framework}/lib/pkgconfig
    configure.env-append      PATH=${python_framework}/bin:$env(PATH)
}


variant remote description {Enable building of obsolete gimp-remote helper app} {
    configure.args-append --enable-gimp-remote
}

variant debug description {Enable debugging} {
    configure.args-append --enable-debug
}

variant quartz {
    depends_lib-delete    port:xorg-libXcursor \
                          port:xorg-libXmu \
                          port:xorg-libXext \
                          port:xorg-libXfixes \
                          port:xpm
    depends_lib-append    port:gtk-osx-application-gtk2
    configure.args-delete --with-x \
                          --x-includes=${prefix}/include \
                          --x-libraries=${prefix}/lib
    configure.args-append --without-x
}

# set default variants
default_variants +python27

post-activate {
    system "${prefix}/bin/gtk-update-icon-cache -f -t ${prefix}/share/icons/hicolor"
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
}

livecheck.type      regex
livecheck.url       https://download.gimp.org/mirror/pub/gimp/v${branch}/
livecheck.regex     "gimp-(${branch}(?:\\.\\d+)*)${extract.suffix}"
