# -*- 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
# $Id: Portfile 119058 2014-04-15 23:03:07Z devans@macports.org $

PortSystem      1.0
PortGroup  compiler_blacklist_versions 1.0

name            gimp2
conflicts       gimp2-devel gimp3-devel
# please remember to update the gimp metapackage to match
version         2.8.10
revision        6
license         {GPL-2+ LGPL}
categories      graphics
maintainers     devans
homepage        http://www.gimp.org/
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.

set branch      [join [lrange [split ${version} .] 0 1] .]
master_sites    gimp:gimp/v${branch}/

distname        gimp-${version}

use_bzip2       yes

checksums       rmd160  c5364c49af2c695bc3d0bd97e792bf3ad75b1175 \
                sha256  e7fd8b19f989138d826003c75f56bd5b6f136eef597e86e3978ede0bba470ae6

depends_build   port:pkgconfig \
                port:intltool \
                port:gtk-doc

depends_lib     port:desktop-file-utils \
                port:iso-codes \
                port:babl \
                port:gegl \
                port:atk \
                port:gtk2 \
                port:fontconfig \
                port:freetype \
                port:tiff \
                port:jpeg \
                port:zlib \
                port:bzip2 \
                port:ghostscript \
                port:libpng \
                port:libmng \
                port:libexif \
                port:aalib \
                port:xpm \
                port:librsvg \
                port:poppler \
                port:curl \
                port:libwmf \
                port:jasper \
                port:lcms \
                port:dbus-glib \
                port:libxml2 \
                port:libxslt \
                port:xorg-libXcursor \
                port:xorg-libXmu \
                port:xorg-libXext \
                port:xorg-libXfixes

depends_run     path:share/gimp/2.0/help:gimp-help-en

# TODO: Check ${configure.cxx_stdlib} directly once MacPorts 2.3 is released
platform darwin {
    set cxxstdlib {}

    if {[info exists configure.cxx_stdlib] &&
        ${configure.cxx_stdlib} ne {} &&
        [string match *clang* ${configure.cxx}]} {
        set cxxstdlib ${configure.cxx_stdlib}
    } elseif {[string match *clang* ${configure.cxx}] &&
              ${os.major} >= 13} {
        set cxxstdlib libc++
    } else {
        set cxxstdlib libstdc++
    }
}

patchfiles      patch-etc-gimprc.diff \
                patch-freetype-includes.diff \
                patch-brush-outline-not-displayed-bz719593.diff \
                patch-palette-editor-not-working-new-palettes-bz719634.diff \
                patch-plug-in-borderaverage-return-garbage-alpha-value-bz719662.diff \
                patch-error-close-port-bz719396.diff \
                patch-file-xwd-sanity-check-colormap-size-CVE-2013-1913.diff \
                patch-file-xwd-sanity-check-number-colors-CVE-2013-1978.diff \
                patch-widget-direction-set-system-locale-not-user-bz679214.diff \
                patch-mistaken-description-new-guides-from-selection-bz720598.diff \
                patch-letter-spacing-in-text-tool-bz720492.diff \
                patch-shadow-layer-for-drop-shadow-filter-bz721058.diff \
                patch-gimpressionist-background-paper-not-aligned-bz720711.diff \
                patch-gimp-paint-options-set-default-brush-size.diff \
                patch-remove-accelerator-markers-python-fu-tooltips.diff \
                patch-create-filechooserbutton-bz699978.diff \
                patch-crash-removing-tag-from-palette-bz722975.diff \
                patch-ICC-profile-with-long-description-bz723177.diff \
                patch-remove-confirm-on-close-preference.diff \
                patch-snapping-behavior-scale-tool-bz722034.diff \
                patch-plugins-bmp-dont-check-biClrUsed-bz697431.diff \
                patch-dont-reset-brush-size-before-each-paint-stroke-bz721249.diff \
                patch-writing-character-constants-bz721775.diff \
                patch-halt-transform-tool-when-switching.diff \
                patch-wrong-name-transfer-tool-dialogs-bz701159.diff \
                patch-plug-ins-twain-tw_mac.c.diff

# gcc-4.2 5493 and 5666.3_13: gimpcpuaccel.c:180: error: can't find a register in class 'BREG' while reloading 'asm'
compiler.blacklist-append gcc-3.3 gcc-4.0 gcc-4.2 apple-gcc-4.2

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.args  --build=${configure.build_arch}-apple-${os.platform}${os.version} \
                --enable-mp \
                --with-pdbgen \
                --with-x \
                --x-includes=${prefix}/include \
                --x-libraries=${prefix}/lib \
                --disable-silent-rules \
                --disable-python \
                --without-alsa \
                --without-gvfs \
                --without-gudev \
                --without-webkit \
                --without-mac-twain

# --without-mac-twain:
#     Leopard's 64bit Carbon is missing QuitApplicationEventLoop
#     It doesn't build any more anyways:
#         https://trac.macports.org/ticket/35168

# update m4macros/intltool.m4 and autoreconf

post-patch {
    copy -force ${prefix}/share/aclocal/intltool.m4 ${worksrcpath}/m4macros
}

use_autoreconf  yes
autoreconf.args -fvi

variant python26 conflicts python27 description {Use python 2.6} {
    configure.args-delete     --disable-python
    configure.python          ${prefix}/bin/python2.6
    depends_lib-append        port:py26-pygtk
    set python_framework      ${frameworks_dir}/Python.framework/Versions/2.6
    configure.pkg_config_path ${python_framework}/lib/pkgconfig
    configure.env             PATH=${python_framework}/bin:$env(PATH)
}

variant python27 conflicts python26 description {Use 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             PATH=${python_framework}/bin:$env(PATH)
}

if {![variant_isset python26]} {
    default_variants +python27
}

variant gvfs description {Enable GIO/gvfs support} {
    depends_lib-append      port:gvfs
    configure.args-delete   --without-gvfs
}

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
    configure.args-delete --with-x \
                          --x-includes=${prefix}/include \
                          --x-libraries=${prefix}/lib
    configure.args-append --without-x
    configure.ldflags-append   -framework Carbon

#
# if variant is +quartz lcms plugin uses 32 bit only Carbon API
#

    if {${build_arch} eq "x86_64" || ${build_arch} eq "ppc64"} {
        configure.args-append --without-lcms
    }
}

#
# set default variants
#

variant help_browser description {Enable Gimp help browser} {
    configure.args-delete   --without-webkit

    if {${cxxstdlib} eq "libstdc++"} {
            depends_lib-append path:lib/pkgconfig/webkit-1.0.pc:webkit-gtk-2.0
    } else {
            depends_lib-append path:lib/pkgconfig/webkit-1.0.pc:webkit-gtk
    }
}

default_variants +help_browser

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   http://ftp.gtk.org/pub/gimp/v${branch}/
livecheck.regex "gimp-(${branch}(?:\\.\\d+)*)${extract.suffix}"
