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

# if we are using libgcc7.5+, we need this wrapper bit
# if we are using libgcc7.4.x, we don't
legacysupport.redirect_bins gimp gimp-2.10 gimp-console gimp-console-2.10 gimp-debug-tool-2.0 gimp-test-clipboard-2.0 gimptool-2.0

# legacysupport issue with the realpath wrap -- disable for now
configure.cppflags-append -D__DISABLE_MP_LEGACY_SUPPORT_FUNCTION_WRAPPING__=1

name                gimp2
conflicts           gimp2-devel gimp3-devel
# please remember to update the gimp metapackage to match
version             2.10.22
revision            0
set branch          [join [lrange [split ${version} .] 0 1] .]
license             GPL-3+
categories          graphics
maintainers         {devans @dbevans}
platforms           darwin

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/
master_sites        gimp:gimp/v${branch}/

distname            gimp-${version}

use_bzip2           yes

checksums           rmd160  08ef0081200a851b642e9bd6014a7b1c9edf1db6 \
                    sha256  2db84b57f3778d80b3466d7c21a21d22e315c7b062de2883cbaaeda9a0f618bb \
                    size    33152226

# force default perl to match intltool
perl5.branches      5.28

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

depends_lib         port:desktop-file-utils \
                    port:iso-codes \
                    port:babl \
                    port:gegl \
                    port:atk \
                    port:gdk-pixbuf2 \
                    port:glib-networking \
                    port:gtk2 \
                    port:fontconfig \
                    port:freetype \
                    port:tiff \
                    port:jpeg \
                    port:zlib \
                    port:bzip2 \
                    port:ghostscript \
                    port:libpng \
                    port:libmng \
                    port:libheif \
                    port:openexr \
                    port:openjpeg \
                    port:gexiv2 \
                    port:aalib \
                    port:xpm \
                    port:librsvg \
                    port:poppler \
                    port:curl \
                    port:libwmf \
                    port:libmypaint \
                    port:mypaint-brushes1 \
                    port:lcms2 \
                    port:dbus-glib \
                    port:libxml2 \
                    port:libxslt \
                    port:xdg-utils \
                    port:xorg-libXcursor \
                    port:xorg-libXmu \
                    port:xorg-libXext \
                    port:xorg-libXfixes

depends_run         port:gimp-help-en

# libgimpwidgets/gimpwidgetsmarshal.h can be referenced by a parallel
# build before it is created
use_parallel_build  no

# 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}

patchfiles          patch-etc-gimprc.in.diff \
                    patch-mach-task-info.diff \
                    patch-quartz-32bit.diff \
                    MYPAINT_BRUSHES_DIR.patch

if {${os.platform} eq "darwin" && ${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

    # work around no pthread_threadid_np on < 10.6 until legacysupport has this
    patchfiles-append patch-gimp2-pthread-threadid-np.diff
}

use_autoreconf      yes
autoreconf.args     -fvi

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       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
}

# Temporary fix for linking against macOS SDK >10.13
if {${os.platform} eq "darwin" && ${os.major} > 13} {
    configure.ldflags-append \
                        -Wl,-sdk_version,10.13
}

configure.args      --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_INTLTOOL_PERL=${configure.perl} \
                    ac_cv_path_PERL=${configure.perl}

if {${os.major} < 10 && ${build_arch} in "i386 x86_64" && [string match *clang* ${configure.compiler}] } {
	depends_build-append  port:cctools
	configure.env-append  NM=${prefix}/bin/nm
	configure.args-append lt_cv_path_NM=${prefix}/bin/nm
}

# 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}"
