# -*- 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 107818 2013-07-06 07:49:51Z ryandesign@macports.org $

PortSystem                      1.0
PortGroup                       conflicts_build 1.0
PortGroup                       xcodeversion 1.0

# Please keep the graphviz and graphviz-devel ports as similar as possible.

name                            graphviz
set my_name                     graphviz
version                         2.30.1
set thisbranch                  [strsed ${name} "g/^${my_name}//"]
set otherbranch                 [expr {${thisbranch} eq {} ? {-devel} : {}}]
categories                      graphics
maintainers                     ryandesign
license                         EPL-1
homepage                        http://www.graphviz.org/
master_sites                    ${homepage}pub/graphviz/stable/SOURCES/
platforms                       darwin
use_parallel_build              yes
dist_subdir                     ${my_name}
distname                        ${my_name}-${version}

description                     graph visualization software

long_description                Graphviz is ${description}. Graph visualization \
                                is a way of representing structural information \
                                as diagrams of abstract graphs and networks. The \
                                Graphviz layout programs dot, neato, fdp, sfdp, \
                                twopi and circo take descriptions of graphs in a \
                                simple text language, and produce output in a \
                                variety of visual and text formats.

checksums                       rmd160  7de6e5e60458208a74ce6033b1a063a04f9dfc48 \
                                sha256  775fb996987032d2eca2f1502c6f913e0b02fe640c124723e45db11daf46aec8

# graphviz needs Xcode 3.1+ to avoid the libGL error when building the smyrna variant
# graphviz-gui needs Xcode 3.1.2+; see #18811
minimum_xcodeversions           {9 3.1.2}

if {${name} eq ${subport}} {
revision                        1

conflicts                       graphviz${otherbranch}

depends_build                   port:pkgconfig

depends_lib                     port:jpeg \
                                port:libpng \
                                port:webp \
                                port:fontconfig \
                                port:freetype \
                                port:expat \
                                port:gd2 \
                                port:gts \
                                port:libtool \
                                port:zlib \
                                port:gettext

depends_run                     port:urw-fonts

patchfiles                      patch-lib-gvc-libgvc.pc.in.diff

# nawk: illegal jump type 339
conflicts_build                 nawk

configure.args                  --disable-silent-rules \
                                --with-codegens \
                                --without-x \
                                --without-devil \
                                --without-smyrna \
                                --with-digcola \
                                --with-ipsepcola \
                                --without-rsvg \
                                --without-pangocairo \
                                --without-lasi \
                                --with-webp \
                                --without-glitz \
                                --with-freetype2 \
                                --with-fontconfig \
                                --without-gdk-pixbuf \
                                --without-ghostscript \
                                --without-gtk \
                                --without-gtkgl \
                                --without-gtkglext \
                                --with-gts \
                                --without-glade \
                                --without-gnomeui \
                                --without-ming \
                                --without-qt \
                                --without-quartz \
                                --disable-swig \
                                --disable-sharp \
                                --disable-guile \
                                --disable-io \
                                --disable-java \
                                --disable-lua \
                                --disable-ocaml \
                                --disable-perl \
                                --disable-php \
                                --disable-python \
                                --disable-python23 \
                                --disable-python24 \
                                --disable-python25 \
                                --disable-r \
                                --disable-ruby \
                                --disable-tcl

platform macosx {
    if {${os.major} > 8} {
        configure.args-delete   --without-quartz
        configure.args-append   --with-quartz
    }
}

variant guile description {Include Guile language bindings} {
    depends_lib-append          port:guile
    depends_build-append        port:swig-guile
    configure.args-delete       --disable-swig \
                                --disable-guile
    configure.args-append       --enable-guile
}

variant lua description {Include Lua language bindings} {
    depends_lib-append          port:lua
    depends_build-append        port:swig-lua
    configure.args-delete       --disable-swig \
                                --disable-lua
    configure.args-append       --enable-lua
    post-patch {
        reinplace "s|/usr/lib\$LIBPOSTFIX/lua|${prefix}/lib\$LIBPOSTFIX/lua|g" ${worksrcpath}/configure
    }
}

variant ocaml description {Include Objective Caml language bindings} {
    depends_lib-append          port:ocaml
    depends_build-append        port:swig-ocaml
    configure.args-delete       --disable-swig \
                                --disable-ocaml
    configure.args-append       --enable-ocaml
    configure.cppflags-append   -I${prefix}/lib/ocaml
}

variant perl description {Include PERL 5 language bindings} {
    depends_lib-append          path:bin/perl:perl5
    depends_build-append        port:swig-perl
    configure.args-delete       --disable-swig \
                                --disable-perl
    configure.args-append       --enable-perl
    configure.perl              ${prefix}/bin/perl
}

variant php description {Include PHP language bindings} {
    depends_lib-append          path:bin/php:php5
    depends_build-append        port:swig-php5
    configure.args-delete       --disable-swig \
                                --disable-php
    configure.args-append       --enable-php
    post-patch {
        reinplace "s|/usr/include/php|${prefix}/include/php|g" ${worksrcpath}/configure
        reinplace "s|/usr/lib\${LIBPOSTFIX}/php|${prefix}/lib\${LIBPOSTFIX}/php|g" ${worksrcpath}/configure
        reinplace "s|/usr/share/php|${prefix}/share/php|g" ${worksrcpath}/configure
    }
}

variant python24 description {Include Python 2.4 language bindings} conflicts python25 python26 {
    depends_lib-append          port:python24
    depends_build-append        port:swig-python
    configure.args-delete       --disable-swig \
                                --disable-python
    configure.args-append       --enable-python
    configure.python            ${prefix}/bin/python2.4
    # The configure script asks python where to install
    # This doesn't work for 2.4 and 2.5 (see #16334)
    post-patch {
        reinplace "s|PYTHON_INSTALL_DIR=.*|PYTHON_INSTALL_DIR=${prefix}/lib/python2.4|" ${worksrcpath}/configure
    }
}

variant python25 description {Include Python 2.5 language bindings} conflicts python24 python26 {
    depends_lib-append          port:python25
    depends_build-append        port:swig-python
    configure.args-delete       --disable-swig \
                                --disable-python
    configure.args-append       --enable-python
    configure.python            ${prefix}/bin/python2.5
    # The configure script asks python where to install
    # This doesn't work for 2.4 and 2.5 (see #16334)
    post-patch {
        reinplace "s|PYTHON_INSTALL_DIR=.*|PYTHON_INSTALL_DIR=${prefix}/lib/python2.5|" ${worksrcpath}/configure
    }
}

variant python26 description {Include Python 2.6 language bindings} conflicts python24 python25 {
    depends_lib-append          port:python26
    depends_build-append        port:swig-python
    configure.args-delete       --disable-swig \
                                --disable-python
    configure.args-append       --enable-python
    configure.python            ${prefix}/bin/python2.6
}

variant ruby description {Include Ruby language bindings} {
    depends_lib-append          port:ruby
    depends_build-append        port:swig-ruby
    configure.args-delete       --disable-swig \
                                --disable-ruby
    configure.args-append       --enable-ruby
}

variant tcl description {Include Tcl language bindings} {
    depends_lib-append          port:tcl
    depends_build-append        port:swig-tcl
    configure.args-delete       --disable-swig \
                                --disable-tcl
    configure.args-append       --enable-tcl
}

variant java description {Include Java language bindings} {
    depends_build-append        port:swig-java
    configure.args-delete       --disable-swig \
                                --disable-java
    configure.args-append       --enable-java
}

variant smyrna description {Include the Smyrna large graph viewer} {
    configure.args-delete       --without-smyrna \
                                --without-gtk \
                                --without-gtkglext \
                                --without-glade
    configure.args-append       --with-smyrna \
                                --with-gtk \
                                --with-gtkglext \
                                --with-glade
    depends_lib-append          port:freeglut \
                                port:gtk2 \
                                port:gtkglext \
                                port:libglade2
}

variant r description {Include R language bindings} {
    depends_build-append        port:swig-r
    configure.args-delete       --disable-swig \
                                --disable-r
    configure.args-append       --enable-r
}

variant rsvg description {enable the rsvg plugin} {
    depends_lib-append          port:librsvg
    configure.args-delete       --without-rsvg
    configure.args-append       --with-rsvg
}

variant gdk_pixbuf description {enable the gdk_pixbuf plugin} {
    depends_lib-append          port:gtk2
    configure.args-delete       --without-gdk-pixbuf
    configure.args-append       --with-gdk-pixbuf
}

variant glitz description {enable the incomplete glitz plugin} {
    depends_lib-append          port:glitz
    configure.args-delete       --without-glitz
    configure.args-append       --with-glitz
}

variant ming description {enable the incomplete ming plugin} {
    depends_lib-append          port:ming
    configure.args-delete       --without-ming
    configure.args-append       --with-ming
}

# Legacy no_pangocairo variant to be removed after February 2014
variant no_pangocairo conflicts pangocairo description {Legacy compatibility variant} {}
variant pangocairo conflicts no_pangocairo description {Add pangocairo support (antialiased bitmapped output; PDF output)} {
    depends_lib-append          path:lib/pkgconfig/pango.pc:pango \
                                port:libLASi
    configure.args-append       --with-pangocairo \
                                --with-lasi
    configure.args-delete       --without-pangocairo \
                                --without-lasi
}

if {[variant_isset no_pangocairo]} {
    default_variants -pangocairo
} else {
    default_variants +pangocairo
}

# Legacy no_x11 variant to be removed after February 2014
variant no_x11 conflicts x11 description {Legacy compatibility variant} {}
variant x11 conflicts no_x11 {
    depends_lib-append          port:xorg-libXaw
    configure.args-delete       --without-x
}

if {[variant_isset no_x11]} {
    default_variants -x11
} else {
    default_variants +x11
}

post-destroot {
    if {![variant_isset smyrna]} {
        foreach f "${prefix}/share/graphviz/doc/pdf/smyrna.1.pdf ${prefix}/share/graphviz/doc/pdf/smyrna.pdf ${prefix}/share/man/man1/smyrna.1" {
            delete ${destroot}${f}
        }
    }
    
    # Make the configuration file that makes the plugins work.
    system "GVBINDIR=${destroot}${prefix}/lib/graphviz DYLD_LIBRARY_PATH=${destroot}${prefix}/lib ${destroot}${prefix}/bin/dot -c"
}

livecheck.type                  regex
livecheck.url                   ${homepage}Download_source.php
livecheck.regex                 ${my_name}-(\[0-9\]+\\.\[0-9\]*\[02468\](\\.\[0-9\]+)*)\\.tar
} else {
    livecheck.type              none
}

subport graphviz-gui${thisbranch} {
    PortGroup                   xcode 1.0
    
    conflicts                   graphviz-gui${otherbranch} graphviz-oldgui
    
    categories-append           aqua
    
    description                 OS X Aqua GUI for viewing and exporting \
                                Graphviz graphs
    
    long_description            ${description}.
    
    depends_lib                 port:graphviz${thisbranch}
    
    pre-fetch {
        if {${os.platform} ne "darwin"} {
            ui_error "${subport} requires OS X."
            return -code error "incompatible operating system"
        }
        if {${os.major} < 9} {
            ui_error "${subport} requires OS X 10.5 or greater. Try graphviz-oldgui instead."
            return -code error "incompatible OS X version"
        }
    }
    
    extract.post_args-append    ${worksrcdir}/macosx ${worksrcdir}/doc
    
    patchfiles                  patch-project.pbxproj.diff
    
    xcode.configuration         Release
    
    build.dir                   ${worksrcpath}/macosx
    xcode.build.settings        PREFIX=${prefix}
    
    xcode.destroot.settings     ${xcode.build.settings}
}

subport gvedit${thisbranch} {
    PortGroup                   app 1.0
    PortGroup                   qt4 1.0
    
    conflicts                   gvedit${otherbranch}
    
    description                 Qt-based GUI for Graphviz
    
    long_description            gvedit is a ${description}. You may prefer \
                                the Mac-native GUI in the graphviz-gui${thisbranch} port.
    
    depends_lib-append          port:graphviz${thisbranch}
    
    post-extract {
        if {[get_short_version_string] eq ""} {
            return -code error "cannot find ${app.name} short version string"
        }
    }
    
    patchfiles                  patch-cmd-gvedit-Makefile.in.diff
    
    # nawk: illegal jump type 339
    conflicts_build             nawk
    
    configure.args              --disable-silent-rules \
                                --with-qt
    
    build.dir                   ${worksrcpath}/cmd/gvedit
    
    app.name                    GVEdit
    app.executable              gvedit
    app.icon                    ${build.dir}/images/icon.png
    
    pre-destroot {
        app.short_version_string [get_short_version_string]
    }
    
    proc get_short_version_string {} {
        global build.dir
        return [exec /usr/bin/sed -E -n s/^\[\[:space:\]\]*"Version:\[\[:space:\]\]*(.+)".*/\\1/p ${build.dir}/mainwindow.cpp]
    }
}
