# -*- 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
# $Id: Portfile 67714 2010-05-16 17:17:30Z jann@macports.org $

PortSystem      1.0
PortGroup       archcheck 1.0
PortGroup       muniversal 1.0
PortGroup       xcodeversion 1.0

name            gtk2
version         2.20.1
set branch      [join [lrange [split ${version} .] 0 1] .]
categories      x11
maintainers     nox openmaintainer
platforms       darwin
description     Gimp ToolKit version 2

long_description \
    This is GTK+ version 2.x. GTK+, which stands for Gimp \
    ToolKit, is a library for creating GUIs for the X Windows \
    System.

homepage        http://www.gtk.org/
distname        gtk+-${version}
use_bzip2       yes

master_sites    gnome:sources/gtk+/${branch}/ \
                ftp://ftp.gtk.org/pub/gtk/${branch}/ \
                http://ftp.gtk.org/pub/gtk/${branch}/

minimum_xcodeversions {9 3.1}

checksums       md5     53e6f3a93bd22934878fc4a4a34c68aa \
                sha1    a80953b4e81c6a5bc2a986852f7fe60c8704cc02 \
                rmd160  bcd5f20f9e3f8d4e6f9bd91388de2fe9851ef361

patchfiles      patch-gtk-builder-convert.diff \
                patch-modules-gail.diff \
                patch-modules-printbackends.diff

depends_build   port:pkgconfig

depends_lib     path:lib/pkgconfig/cairo.pc:cairo \
                port:fontconfig \
                port:freetype \
                path:lib/pkgconfig/glib-2.0.pc:glib2 \
                port:jpeg \
                port:tiff \
                port:libiconv \
                port:libpng \
                port:jasper \
                port:atk \
                path:lib/pkgconfig/pango.pc:pango \
                port:gettext \
                port:zlib

depends_run     port:shared-mime-info

archcheck.files \
    lib/libatk-1.0.dylib \
    lib/libcairo.dylib \
    lib/libfontconfig.dylib \
    lib/libfreetype.dylib \
    lib/libglib-2.0.dylib \
    lib/libintl.dylib \
    lib/libjasper.dylib \
    lib/libjpeg.dylib \
    lib/libiconv.dylib \
    lib/libpango-1.0.dylib \
    lib/libpng.dylib \
    lib/libtiff.dylib \
    lib/libz.dylib

configure.args  --enable-static \
                --disable-glibtest \
                gio_can_sniff=yes

configure.ccache no
configure.cppflags-append   -DX_LOCALE
configure.cflags-append     -fstrict-aliasing

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

    if {[variant_isset quartz]} {
        if {![file exists ${prefix}/include/cairo/cairo-quartz.h]} {
            error "cairo must be built with the +quartz variant enabled."
        }
    } elseif {![file exists ${prefix}/include/cairo/cairo-xlib.h]} {
        error "cairo must be built without the +no_x11 variant."
    }
}

use_parallel_build  yes

test.run        yes
test.target     check

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} AUTHORS COPYING HACKING NEWS README \
        ${destroot}${docdir}

    ui_debug "Creating gtk.immodules..."
    system "DYLD_LIBRARY_PATH=${destroot}${prefix}/lib \
        ${destroot}${prefix}/bin/gtk-query-immodules-2.0 \
        ${destroot}${prefix}/lib/gtk-2.0/2.10.0/immodules/*.so \
        >${destroot}${prefix}/etc/gtk-2.0/gtk.immodules"
    reinplace "s|${destroot}||" ${destroot}${prefix}/etc/gtk-2.0/gtk.immodules
}

post-activate {
    ui_debug "Updating gdk-pixbuf.loaders..."
    system "${prefix}/bin/gdk-pixbuf-query-loaders \
        >${prefix}/etc/gtk-2.0/gdk-pixbuf.loaders"
}

platform darwin 8 {
    if {[variant_isset quartz] || [variant_isset no_x11]} {
        configure.ldflags-append  -framework Cocoa -framework Carbon
    }
}

if {[variant_isset no_x11]} {
    default_variants +quartz
} elseif {![variant_isset quartz]} {
    default_variants +x11
}

if {[variant_isset universal]} {
    set cups /usr/bin/cups-config
    if {[info exists universal_sysroot]} {
        set cups ${universal_sysroot}${cups}
    }
    configure.env-append CUPS_CONFIG=${cups}

    # cups is not 32/64-bit universal on Tiger.
    if {${os.major} == "8"} {
        set merger_configure_args(ppc64)   --disable-cups
        set merger_configure_args(x86_64)  --disable-cups
    }

    # Find architectures which will not run on build platform.
    set cpu64bit_capable [sysctl hw.cpu64bit_capable]
    if {${os.arch} eq "i386"} {
        set cross_archs ppc64
        if {${os.major} >= 10} {
            lappend cross_archs ppc
        }
        set host_archs "ppc ppc64"
        set run_archs i386
        if {${cpu64bit_capable}} {
            set run_archs "x86_64 ${run_archs}"
        }
    } else {
        set cross_archs "i386 x86_64"
        set host_archs "i386 x86_64"
        set run_archs ppc
        if {${cpu64bit_capable}} {
            set run_archs "ppc64 ${run_archs}"
        }
    }

    pre-configure {
        set run_arch ""
        foreach arch ${run_archs} {
            if {[lsearch ${universal_archs_to_use} ${arch}] != -1} {
                set run_arch ${arch}
                break
            }
        }

        if {${run_arch} eq ""} {
            ui_error ""
            ui_error "gtk2 requires at least one architecture runnable on this"
            ui_error "machine to be able to build universally. Please add at least"
            ui_error "one of the following architectures to `universal_archs` in"
            ui_error "\"${prefix}/etc/macports/macports.conf\":"
            ui_error ""
            ui_error "\t${run_archs}"
            ui_error ""
        }

        foreach arch ${host_archs} {
            lappend merger_configure_env(${arch}) \
                NM='/usr/bin/nm -p'
        }

        foreach arch ${cross_archs} {
            lappend merger_configure_env(${arch}) \
                GTK_UPDATE_ICON_CACHE=${worksrcpath}-${run_arch}/gtk/gtk-update-icon-cache \
                GDK_PIXBUF_CSOURCE=${worksrcpath}-${run_arch}/gdk-pixbuf/gdk-pixbuf-csource
            lappend merger_build_env(${arch}) \
                GDK_PIXBUF_MODULE_FILE=${worksrcpath}-${run_arch}/gdk-pixbuf/gdk-pixbuf.loaders
        }

        # Let gtk_host in .pc files be the same on all architectures
        set host powerpc
        if {${os.arch} eq "i386"} {
            set host i686
        }
        eval reinplace s/@host@/${host}-apple-darwin${os.version}/ \
            [glob -directory ${worksrcpath} *.pc.in]
    }
}

variant no_x11 {
    pre-fetch {
        if {[file exists ${prefix}/lib/libpangox-1.0.dylib]} {
            ui_error "Please uninstall or deactivate the pango port and reinstall it by running `port install pango +no_x11`."
            error "pango must be installed with the no_x11 variant enabled."
        }
    }
}

variant quartz requires no_x11 conflicts x11 {
    configure.args-append   --with-gdktarget=quartz
}

variant x11 conflicts quartz description {Enable rendering in X11} {
    depends_lib-append \
        port:xorg-libXi \
        port:xorg-libXrandr \
        port:xorg-libXcursor \
        port:xorg-libXinerama \
        port:xorg-libXdamage \
        port:xorg-libXcomposite \
        port:xorg-libXfixes

    configure.args-append      --with-xinput --enable-xinerama \
                               --x-include=${prefix}/include --x-lib=${prefix}/lib
}

livecheck.type  regex
livecheck.url   ftp://ftp.gnome.org/pub/gnome/sources/gtk+/${branch}/
livecheck.regex {LATEST-IS-(\d+(?:\.\d+)*)}
