# -*- 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                       xcodeversion 1.0

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

name                            graphviz-devel
set my_name                     graphviz
version                         12.2.1
checksums                       rmd160  75d7edc5d7fc12f75eb17d4ab85855d334bf78f5 \
                                sha256  242bc18942eebda6db4039f108f387ec97856fc91ba47f21e89341c34b554df8 \
                                size    26813453

set thisbranch                  [strsed ${name} "g/^${my_name}//"]
set otherbranch                 [expr {${thisbranch} eq {} ? {-devel} : {}}]
categories                      graphics
maintainers                     {ryandesign @ryandesign} {mascguy @mascguy}
license                         EPL-1
homepage                        https://www.graphviz.org/
master_sites                    https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/${version}/
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.

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

compiler.cxx_standard           2017

if {${name} eq ${subport}} {
PortGroup                       legacysupport 1.1

# strndup
legacysupport.newest_darwin_requires_legacy 10

revision                        0

conflicts                       graphviz${otherbranch}

platform darwin {
    # FIXME: Temporary fix for 10.6 builds; need to follow-up with upstream
    # See: https://trac.macports.org/ticket/60962
    if {${os.major} == 10 && ${build_arch} ne "ppc"} {
        compiler.whitelist          macports-clang-9.0
   }
}

depends_build \
                                port:autoconf-archive \
                                port:gettext \
                                path:bin/pkg-config:pkgconfig

depends_lib \
                                port:expat \
                                port:fontconfig \
                                port:freetype \
                                port:gd2 \
                                port:gettext-runtime \
                                port:gts \
                                path:include/turbojpeg.h:libjpeg-turbo \
                                port:libpng \
                                port:libtool \
                                port:webp \
                                port:zlib

depends_run \
                                port:urw-fonts

patchfiles-append               smyrna.patch

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

# Teach glibtool about -stdlib=libc++
# Also we are patching Makefile.am.
use_autoreconf  yes
autoreconf.args -fvi
configure.checks.implicit_function_declaration.whitelist-append strchr

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

variant guile description {Include Guile 2.2 language bindings} {
    depends_lib-append          port:guile-2.2
    depends_build-append        port:swig-guile
    depends_build-append        port:gsed

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-guile --enable-guile
    configure.env-append        GUILE_CONFIG=${prefix}/bin/guile-config-2.2
    configure.env-append        GUILE=${prefix}/bin/guile-2.2

    post-patch {
        reinplace "s/\\(GUILE_VERSION=.* | \\)sed/\\1\$SED/" ${worksrcpath}/configure.ac
    }
}

variant lua description {Include Lua language bindings} {
    depends_lib-append          port:lua
    depends_build-append        port:swig-lua

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-lua --enable-lua

    post-patch {
        # TODO: I think this is broken.  It patches configure, but
        #       use_autoreconf is yes.
        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-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-ocaml --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-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-perl --enable-perl
    configure.perl              ${prefix}/bin/perl
}

# TODO: support current PHP versions
# https://trac.macports.org/ticket/45149

#variant php description {Include PHP language bindings} {
#    depends_lib-append          path:bin/php:php5
#    depends_build-append        port:swig-php5
#
#    configure.args-replace      --disable-swig --enable-swig
#    configure.args-replace      --disable-php --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 python27 description {Include Python 2.7 language bindings} {
    depends_lib-append          port:python27
    depends_build-append        port:swig-python

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-python --enable-python
    configure.python            ${prefix}/bin/python2.7
}

variant ruby description {Include Ruby language bindings} {
    depends_lib-append          port:ruby
    depends_build-append        port:swig-ruby

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-ruby --enable-ruby
}

variant tcl description {Include Tcl language bindings} {
    depends_lib-append          port:tcl
    depends_build-append        port:swig-tcl

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-tcl --enable-tcl
}

variant java description {Include Java language bindings} {
    depends_build-append        port:swig-java

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-java --enable-java
}

variant smyrna description {Include the Smyrna large graph viewer} {
    depends_lib-append          port:freeglut \
                                path:lib/pkgconfig/gtk+-2.0.pc:gtk2 \
                                port:gtkglext \
                                port:libglade2

    configure.args-replace      --without-gdk --with-gdk
    configure.args-replace      --without-glade --with-glade
    configure.args-replace      --without-gtk --with-gtk
    configure.args-replace      --without-gtkglext --with-gtkglext
    configure.args-replace      --without-smyrna --with-smyrna
}

variant r description {Include R language bindings} {
    depends_build-append        port:swig-r

    configure.args-replace      --disable-swig --enable-swig
    configure.args-replace      --disable-r --enable-r
}

variant rsvg description {enable the rsvg plugin} {
    depends_lib-append          path:lib/pkgconfig/librsvg-2.0.pc:librsvg

    configure.args-replace      --without-rsvg --with-rsvg
}

variant gdk_pixbuf description {enable the gdk_pixbuf plugin} {
    depends_lib-append          path:lib/pkgconfig/gtk+-2.0.pc:gtk2

    configure.args-replace      --without-gdk-pixbuf --with-gdk-pixbuf
}

variant glitz description {enable the incomplete glitz plugin} {
    depends_lib-append          port:glitz

    configure.args-replace      --without-glitz --with-glitz
}

variant pangocairo description {Add pangocairo support (antialiased bitmapped output; PDF output)} {
    depends_lib-append          path:lib/pkgconfig/cairo.pc:cairo \
                                port:libLASi \
                                path:lib/pkgconfig/pango.pc:pango

    configure.args-replace      --without-lasi --with-lasi
    configure.args-replace      --without-pangocairo --with-pangocairo
}

# This is not a default variant because its license would make Graphviz undistributable.
variant poppler description {Add poppler support} {
    depends_lib-append          path:lib/pkgconfig/poppler.pc:poppler
    configure.args-replace      --without-poppler --with-poppler
}

variant x11 {
    depends_lib-append          port:xorg-libXaw
    configure.args-delete       --without-x
}

default_variants                +pangocairo +x11

post-destroot {
    if {![variant_isset smyrna]} {
        delete ${destroot}${prefix}/share/graphviz/doc/pdf/smyrna.pdf
    }

    # 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                   https://gitlab.com/api/v4/projects/4207231/releases
livecheck.regex                 \"name\":\s*\"(\[0-9\]+\\.\[0-9\]+\\.\[0-9\]+)*\"
} else {
    livecheck.type              none
}

subport graphviz-gui${thisbranch} {
    PortGroup                   xcode 1.0

    revision                    0

    conflicts                   graphviz-gui${otherbranch} graphviz-oldgui

    categories-append           aqua

    description                 macOS Aqua GUI for viewing and exporting \
                                Graphviz graphs

    long_description            ${description}.

    depends_lib                 port:graphviz${thisbranch}

    if {${os.major} < 9} {
        known_fail              yes
        pre-fetch {
            ui_error "${subport} requires Mac OS X 10.5 or greater. Try graphviz-oldgui instead."
            return -code error "incompatible Mac OS X version"
        }
    }

    extract.post_args-append    ${worksrcdir}/macosx ${worksrcdir}/doc

    patchfiles-append           patch-project.pbxproj.diff

    if {[vercmp ${xcodeversion} 10.0] >= 0} {
        # This port does not build with the new Xcode build system at present
        build.pre_args          -UseNewBuildSystem=NO
        destroot.pre_args       -UseNewBuildSystem=NO
    }

    # When building for 10.11, with the 10.12 SDK, builds fail due to:
    # - Unknown type name 'NSToolbarItemIdentifier'
    # That identifier must be replaced with 'NSString *' for this scenario.
    # See: https://trac.macports.org/ticket/63597
    if { (${os.major} == 15) && [vercmp ${xcodeversion} 8] >= 0} {
        ui_debug "Building for 10.11 with 10.12 SDK; enabling patch for NSToolbarItemIdentifer"
        pre-patch {
            reinplace "s|NSToolbarItemIdentifier|NSString *|g" macosx/GVAttributeInspectorController.h
        }
    }

    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                   qt5 1.0

    revision                    1

    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 {
        set short_version_string [get_short_version_string]
        if {${short_version_string} eq ""} {
            return -code error "cannot find ${app.name} short version string"
        } else {
            ui_debug "${app.name} short version string: ${short_version_string}"
        }
    }

    patchfiles-append           patch-cmd-gvedit-Makefile.in.diff

    configure.args              --disable-silent-rules \
                                --with-qt \
                                ac_cv_prog_AWK=/usr/bin/awk \
                                ac_cv_prog_QMAKE=${prefix}/libexec/qt5/bin/qmake

    build.dir                   ${worksrcpath}/cmd/gvedit

    app.name                    GVEdit
    app.executable              gvedit
    app.icon                    ${build.dir}/images/icon.png
    app.retina                  yes

    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:\]\]*\"\[\[:space:\]\]*\[Vv\]ersion:\[\[:space:\]\]*(\[0-9.\]+).*/\\1/p ${build.dir}/mainwindow.cpp]
    }
}