# -*- 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 107423 2013-06-28 12:23:26Z mojca@macports.org $

PortSystem          1.0

name                root
version             5.34.09
categories          science
maintainers         gmail.com:mattiafrancescomoro hep.phy.cam.ac.uk:jonesc
license             LGPL-2.1+
description         ROOT is a data analysis framework from CERN
long_description    The ROOT system provides a set of frameworks with all \
                    the functionality needed to handle and analyze large amounts \
                    of data in a very efficient way
homepage            http://root.cern.ch/
platforms           darwin
distname            ${name}_v${version}.source
master_sites        http://root.cern.ch/download/ \
                    ftp://root.cern.ch/root/

checksums           rmd160  73a25b1abc5265f658c4da2c9efd7b1df7a5932a \
                    sha256  a99cd7f34bf25a52e63228f3f7c245afa9e55420bfb4ce7e78d20c6475a4ea86

worksrcdir          root

depends_lib         port:freetype \
                    port:xz \
                    port:pcre \
                    port:Xft2 \
                    port:xorg-libX11 \
                    port:zlib \
                    port:jpeg \
                    port:libpng \
                    port:giflib \
                    port:tiff \
                    port:gmp \
                    port:xpm \
                    port:expat

post-patch {
    reinplace "s|-lfreetype| \`freetype-config --libs\`|g" ${worksrcpath}/config/root-config.in
    reinplace "s|\"/usr/include\"|\"${prefix}/include/\" \"/usr/include\"|g" ${worksrcpath}/configure
    reinplace "s|/usr/lib |${prefix}/lib /usr/lib |g" ${worksrcpath}/configure
    reinplace "s|/usr/include |${prefix}/include /usr/include |g" ${worksrcpath}/configure
    reinplace "s|\"/usr/lib\"|\"${prefix}/lib/\" \"/usr/lib\"g|" ${worksrcpath}/configure
}

pre-configure {
    configure.args-append \
        --with-cc=${configure.cc} \
        --with-cxx=${configure.cxx} \
        --with-ld=${configure.cxx}
    if { ${configure.f77} != "" } {
        configure.args-append --with-f77=${configure.f77}
    }
    # the build type (32 or 64 bit) -must- be for first argument
    if {${build_arch} == "i386" || ${build_arch} == "ppc"} {
        configure.pre_args macosx --prefix=${prefix}
    } else {
        configure.pre_args macosx64 --prefix=${prefix}
    }
}

configure.args      --docdir=${prefix}/share/doc/${name} \
                    --libdir=${prefix}/lib/root \
                    --testdir=${prefix}/share/root/test \
                    --tutdir=${prefix}/share/root/tutorials \
                    --etcdir=${prefix}/etc/root \
                    --disable-builtin-freetype \
                    --disable-builtin-glew \
                    --disable-builtin-pcre \
                    --disable-builtin-zlib \
                    --disable-builtin-lzma \
                    --disable-fftw3 \
                    --disable-krb5 \
                    --disable-ldap \
                    --disable-mysql \
                    --disable-odbc \
                    --disable-opengl \
                    --disable-pythia6 \
                    --disable-pythia8 \
                    --disable-roofit \
                    --disable-ssl \
                    --disable-xml \
                    --disable-python \
                    --disable-fitsio \
                    --disable-gsl-shared \
                    --disable-pgsql \
                    --disable-dcache \
                    --disable-chirp \
                    --disable-hdfs \
                    --disable-ruby \
                    --disable-minuit2 \
                    --disable-tmva \
                    --disable-qt \
                    --disable-qtgsi \
                    --disable-bonjour \
                    --disable-gviz \
                    --disable-soversion \
                    --disable-c++11 \
                    --disable-cocoa \
                    --disable-xrootd \
                    --enable-builtin-ftgl \
                    --enable-mathmore \
                    --enable-genvector \
                    --enable-memstat \
                    --enable-unuran \
                    --enable-reflex \
                    --enable-table \
                    --enable-fink

universal_variant   no

default_variants    +ssl +xml +gsl +minuit2 +tmva +roofit +graphviz +opengl +soversion

variant xrootd description {Build with XRootD support} {
    configure.args-delete   --disable-xrootd
    configure.args-append   --enable-xrootd \
                            --with-xrootd-incdir="${prefix}/include/xrootd" \
                            --with-xrootd-libdir="${prefix}/lib"
    depends_lib-append      port:xrootd
}

variant soversion description {Build with soversion support} {
    configure.args-delete   --disable-soversion
    configure.args-append   --enable-soversion
}

variant graphviz description {Build with graphviz support} {
    configure.args-delete   --disable-gviz
    configure.args-append   --enable-gviz \
                            --with-gviz-incdir="${prefix}/include/graphviz" \
                            --with-gviz-libdir="${prefix}/lib"
    depends_lib-append      path:bin/dot:graphviz
}

variant avahi description {Build with avahi support} {
    configure.args-delete   --disable-bonjour
    configure.args-append   --enable-bonjour \
                            --with-avahi-incdir="${prefix}/include" \
                            --with-avahi-libdir="${prefix}/lib"
    depends_lib-append      port:avahi
}

variant fftw3 description {Build with fftw3 support} {
    configure.args-delete   --disable-fftw3
    configure.args-append   --enable-fftw3 \
                            --with-fftw3-incdir="${prefix}/include" \
                            --with-fftw3-libdir="${prefix}/lib"
    depends_lib-append      port:fftw-3
}

variant gsl description {Build with gsl support} {
    configure.args-delete   --disable-gsl-shared
    configure.args-append   --enable-gsl-shared \
                            --with-gsl-incdir="${prefix}/include" \
                            --with-gsl-libdir="${prefix}/lib"
    depends_lib-append      port:gsl
}

variant fitsio description {Build with fitsio support} {
    configure.args-delete   --disable-fitsio
    configure.args-append   --enable-fitsio \
                            --with-cfitsio-incdir="${prefix}/include" \
                            --with-cfitsio-libdir="${prefix}/lib"
    depends_lib-append      port:cfitsio
}

variant odbc description {Build with odbc support} {
    configure.args-delete   --disable-odbc
    configure.args-append   --enable-odbc \
                            --with-odbc-incdir="${prefix}/include" \
                            --with-odbc-libdir="${prefix}/lib"
    depends_lib-append      port:libiodbc
}

variant ldap description {Build with ldap support} {
    configure.args-delete   --disable-ldap
    configure.args-append   --enable-ldap \
                            --with-ldap-incdir="${prefix}/include" \
                            --with-ldap-libdir="${prefix}/lib"
    depends_lib-append      port:openldap
}

variant roofit description {Build the RooFit advanced fitting package} {
    configure.args-delete   --disable-roofit
    configure.args-append   --enable-roofit
}

variant tmva description {Build the TMVA package} {
    configure.args-delete   --disable-tmva
    configure.args-append   --enable-tmva
}

variant minuit2 description {Build with minuit2 support} {
    configure.args-delete   --disable-minuit2
    configure.args-append   --enable-minuit2
}

variant opengl description {Build with opengl support} {
    configure.args-delete   --disable-opengl
    configure.args-delete   --disable-builtin-glew
    configure.args-append   --enable-opengl --enable-builtin-glew
}

variant python26 conflicts python32 python31 python27 description {Build with python 2.6 support} {
    set mypydir ${prefix}/Library/Frameworks/Python.framework/Versions/2.6
    configure.args-delete   --disable-python
    configure.args-append   --enable-python --with-python-incdir="${mypydir}/Headers" --with-python-libdir="${mypydir}"
    depends_lib-append      port:python26
    post-destroot {
        set mypylibdir  ${prefix}/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
        set mypysitedir ${mypylibdir}/site-packages/
        file mkdir ${destroot}${mypysitedir}
        foreach pyfile [glob ${destroot}${prefix}/lib/root/*.py*] { move $pyfile ${destroot}${mypysitedir} }
        move ${destroot}${prefix}/lib/root/python/genreflex ${destroot}${mypysitedir}
        reinplace "s|/opt/local/lib/root|${mypysitedir}|g" ${destroot}${prefix}/bin/genreflex
        reinplace "s|python/genreflex/genreflex.py|genreflex/genreflex.py|g" ${destroot}${prefix}/bin/genreflex
        ln -sf ${prefix}/lib/root/libPyROOT.so ${destroot}${mypylibdir}/
    }
}

variant python27 conflicts python32 python31 python26 description {Build with python 2.7 support} {
    set mypydir ${prefix}/Library/Frameworks/Python.framework/Versions/2.7
    configure.args-delete   --disable-python
    configure.args-append   --enable-python \
                            --with-python-incdir="${mypydir}/Headers" \
                            --with-python-libdir="${mypydir}"
    depends_lib-append      port:python27
    post-destroot {
        set mypylibdir  ${prefix}/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
        set mypysitedir ${mypylibdir}/site-packages/
        file mkdir ${destroot}${mypysitedir}
        foreach pyfile [glob ${destroot}${prefix}/lib/root/*.py*] { move $pyfile ${destroot}${mypysitedir} }
        move ${destroot}${prefix}/lib/root/python/genreflex ${destroot}${mypysitedir}
        reinplace "s|/opt/local/lib/root|${mypysitedir}|g" ${destroot}${prefix}/bin/genreflex
        reinplace "s|python/genreflex/genreflex.py|genreflex/genreflex.py|g" ${destroot}${prefix}/bin/genreflex
        ln -sf ${prefix}/lib/root/libPyROOT.so ${destroot}${mypylibdir}/
    }
}

variant python31 conflicts python32 python27 python26 description {Build with python 3.1 support} {
    set mypydir ${prefix}/Library/Frameworks/Python.framework/Versions/3.1
    configure.args-delete   --disable-python
    configure.args-append   --enable-python --with-python-incdir="${mypydir}/Headers" --with-python-libdir="${mypydir}"
    depends_lib-append      port:python31
    post-destroot {
        set mypylibdir  ${prefix}/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1
        set mypysitedir ${mypylibdir}/site-packages/
        file mkdir ${destroot}${mypysitedir}
        foreach pyfile [glob ${destroot}${prefix}/lib/root/*.py*] { move $pyfile ${destroot}${mypysitedir} }
        move ${destroot}${prefix}/lib/root/python/genreflex ${destroot}${mypysitedir}
        reinplace "s|/opt/local/lib/root|${mypysitedir}|g" ${destroot}${prefix}/bin/genreflex
        reinplace "s|python/genreflex/genreflex.py|genreflex/genreflex.py|g" ${destroot}${prefix}/bin/genreflex
        ln -sf ${prefix}/lib/root/libPyROOT.so ${destroot}${mypylibdir}/
    }
}

variant python32 conflicts python31 python27 python26 description {Build with python 3.2 support} {
    set mypydir ${prefix}/Library/Frameworks/Python.framework/Versions/3.2
    configure.args-delete   --disable-python
    configure.args-append   --enable-python \
                            --with-python-incdir="${mypydir}/Headers" \
                            --with-python-libdir="${mypydir}"
    depends_lib-append      port:python32
    post-destroot {
        set mypylibdir  ${prefix}/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2
        set mypysitedir ${mypylibdir}/site-packages/
        file mkdir ${destroot}${mypysitedir}
        foreach pyfile [glob ${destroot}${prefix}/lib/root/*.py*] { move $pyfile ${destroot}${mypysitedir} }
        move ${destroot}${prefix}/lib/root/python/genreflex ${destroot}${mypysitedir}
        reinplace "s|/opt/local/lib/root|${mypysitedir}|g" ${destroot}${prefix}/bin/genreflex
        reinplace "s|python/genreflex/genreflex.py|genreflex/genreflex.py|g" ${destroot}${prefix}/bin/genreflex
        ln -sf ${prefix}/lib/root/libPyROOT.so ${destroot}${mypylibdir}/
    }
}

variant ruby description {Build with ruby support} {
    configure.args-delete   --disable-ruby
    configure.args-append   --enable-ruby 
    depends_lib-append      port:ruby
}

variant ssl description {Build with ssl support} {
    configure.args-delete   --disable-ssl
    configure.args-append   --enable-ssl \
                            --with-ssl-shared=yes \
                            --with-ssl-incdir="${prefix}/include" \
                            --with-ssl-libdir="${prefix}/lib"
    depends_lib-append      port:openssl
}

variant xml description {Build with xml support} {
    configure.args-delete   --disable-xml
    configure.args-append   --enable-xml \
                            --with-xml-incdir="${prefix}/include/libxml2" \
                            --with-xml-libdir="${prefix}/lib"
    depends_lib-append      port:libxml2
}

variant mysql conflicts mysql51 mysql55 mariadb percona description {Build with mysql5 support} {
    configure.args-delete   --disable-mysql
    configure.args-append   --enable-mysql \
                            --with-mysql-incdir="${prefix}/include/mysql5/mysql" \
                            --with-mysql-libdir="${prefix}/lib/mysql5/mysql"
    depends_lib-append      port:mysql5
}

variant mysql51 conflicts mysql mysql55 mariadb percona description {Build with mysql51 support} {
    configure.args-delete   --disable-mysql
    configure.args-append   --enable-mysql \
                            --with-mysql-incdir="${prefix}/include/mysql51/mysql" \
                            --with-mysql-libdir="${prefix}/lib/mysql51/mysql"
    depends_lib-append      port:mysql51
}

variant mysql55 conflicts mysql mysql51 mariadb percona description {Build with mysql55 support} {
    configure.args-delete   --disable-mysql
    configure.args-append   --enable-mysql \
                            --with-mysql-incdir="${prefix}/include/mysql55/mysql" \
                            --with-mysql-libdir="${prefix}/lib/mysql55/mysql"
    depends_lib-append      port:mysql55
}

variant mariadb conflicts mysql mysql51 mysql55 percona description {Build with mariadb support} {
    configure.args-delete   --disable-mysql
    configure.args-append   --enable-mysql \
                            --with-mysql-incdir="${prefix}/include/mariadb/mysql" \
                            --with-mysql-libdir="${prefix}/lib/mariadb/mysql"
    depends_lib-append      port:mariadb
}

variant percona conflicts mysql mysql51 mysql55 mariadb description {Build with percona support} {
    configure.args-delete   --disable-mysql
    configure.args-append   --enable-mysql \
                            --with-mysql-incdir="${prefix}/include/percona/mysql" \
                            --with-mysql-libdir="${prefix}/lib/percona/mysql"
    depends_lib-append      port:percona
}

variant postgresql90 conflicts postgresql92 description {Build with PostgreSQL 9.0 support} {
    configure.args-delete   --disable-pgsql
    configure.args-append   --enable-pgsql \
                            --with-pgsql-incdir="${prefix}/include/postgresql90" \
                            --with-pgsql-libdir="${prefix}/lib/postgresql90"
    depends_lib-append      port:libpqxx port:postgresql90
}

variant postgresql92 conflicts postgresql90 description {Build with PostgreSQL 9.2 support} {
    configure.args-delete   --disable-pgsql
    configure.args-append   --enable-pgsql \
                            --with-pgsql-incdir="${prefix}/include/postgresql92" \
                            --with-pgsql-libdir="${prefix}/lib/postgresql92"
    depends_lib-append      port:libpqxx port:postgresql92
}

variant pythia description {Pythia 8 support for root} {
    configure.args-delete   --disable-pythia8
    configure.args-append   --enable-pythia8 \
                            --with-pythia8-incdir=${prefix}/include/pythia \
                            --with-pythia8-libdir=${prefix}/lib
    depends_lib-append      port:pythia
}

variant cocoa description {Enables native OSX graphical backend instead of X11} {
    # Check we are using a clang compiler
    pre-fetch {
        if { ![string match "*clang*" ${configure.compiler}] || [string match "macports-clang-3.3" ${configure.compiler}] } {
            ui_error "The cocoa variant is experimental and can currently only be built"
            ui_error "using a clang(<3.3) compiler. Your current compiler is ${configure.compiler}."
            ui_error "Please try again using either of the clang31 or clang32 variants,"
            ui_error "or the system clang compiler using configure.compiler=clang"
            return -code error "Unsupported compiler for cocoa support"
        }
    }
    # Enable cocoa support in configure
    configure.args-delete --disable-cocoa
    configure.args-append --enable-cocoa
    # patch afterimage build to make sure TIFF suport is disabled. Causes problems with cocoa...
    patchfiles-append patch-builtin-afterimage-disabletiff.diff
}

variant gcc45 conflicts gcc46 gcc47 gcc48 clang31 clang32 clang33 description {Compile using MacPorts gcc 4.5} {
    configure.compiler      macports-gcc-4.5
    depends_lib-append      port:gcc45
}

variant gcc46 conflicts gcc45 gcc47 gcc48 clang31 clang32 clang33 description {Compile using MacPorts gcc 4.6} {
    configure.compiler      macports-gcc-4.6
    depends_lib-append      port:gcc46
}

variant gcc47 conflicts gcc45 gcc46 gcc48 clang31 clang32 clang33 description {Compile using MacPorts gcc 4.7} {
    configure.compiler      macports-gcc-4.7
    depends_lib-append      port:gcc47
}

variant gcc48 conflicts gcc45 gcc46 gcc47 clang31 clang32 clang33 description {Compile using MacPorts gcc 4.8} {
    configure.compiler      macports-gcc-4.8
    depends_lib-append      port:gcc48
}

variant clang31 conflicts gcc45 gcc46 gcc47 gcc48 clang32 clang33 description {Compile using MacPorts clang 3.1} {
    configure.compiler      macports-clang-3.1
    depends_lib-append      port:clang-3.1
    configure.args-append   --with-llvm-config="${prefix}/bin/llvm-config-mp-3.1"
}

variant clang32 conflicts gcc45 gcc46 gcc47 gcc48 clang31 clang33 description {Compile using MacPorts clang 3.2} {
    configure.compiler      macports-clang-3.2
    depends_lib-append      port:clang-3.2
    configure.args-append   --with-llvm-config="${prefix}/bin/llvm-config-mp-3.2"
}

variant clang33 conflicts gcc45 gcc46 gcc47 gcc48 clang31 clang32 description {Compile using MacPorts clang 3.3} {
    configure.compiler      macports-clang-3.3
    depends_lib-append      port:clang-3.3
    configure.args-append   --with-llvm-config="${prefix}/bin/llvm-config-mp-3.3"
}

if {[variant_isset qt_mac]} {
    PortGroup qt4 1.0
}

variant qt_mac description {Build with Qt support via Mac UI} {
    post-patch {
        # fix where "qglobal.h" is found in all files that use or reference it
        foreach tpf { configure graf2d/qt/src/TGQt.cxx } {
            reinplace "s@Qt/qglobal\\.h@QtCore/qglobal\\.h@g" \
                ${worksrcpath}/${tpf}
        }
        reinplace "/qttestdir/s@Qt@QtCore@g" ${worksrcpath}/configure

        # fix library search extensions to include ".la"
        # for Qt framework libraries (only)
        reinplace "s@\\(\\.so \\.sl \\.dylib \\.dll\\.a\\)@\\1 \\.la@g" \
            ${worksrcpath}/configure
    }

    configure.args-delete   --disable-qt
    configure.args-delete   --disable-qtgsi
    configure.args-append   --enable-qt --enable-qtgsi \
                            --with-qt-incdir="${prefix}/include" \
                            --with-qt-libdir="${prefix}/lib"
    depends_lib-append      port:qt4-mac

    post-configure {
        # patch up the Makefile.config to handle the QTLIB frameworks correctly
        reinplace "/QTLIB\[ \]/s@:= @:= -F${qt_libs_dir} @" \
            ${worksrcpath}/config/Makefile.config
        reinplace "/QTLIB\[ \]/s@\\(Qt\[^ \]*\\).la@-framework \\1@g" \
            ${worksrcpath}/config/Makefile.config
    }
}

livecheck.type              regex
livecheck.url               [lindex ${master_sites} 0]
livecheck.regex             ${name}_v(\[0-9a-z.\]+)\\.source

# ========================================================================================
# The following have build issues, are obsolete or experimental.
# ========================================================================================

# Intefers with builtin freetype build (enforced by cocoa variant)
# Also do not seem to be actually needed any more
#pre-build {
#    build.args      CC=${configure.cc} \
#                    CXX=${configure.cxx} \
#                    F77=${configure.f77}
#}

# variant kerberos5 description {Build with kerberos5 support} {
#     configure.args-delete   --disable-krb5
#     configure.args-append   --enable-krb5 \
#                             --with-krb5-incdir="${prefix}/include" \
#                             --with-krb5-libdir="${prefix}/lib"
#      depends_lib-append      port:kerberos5
# }

# variant ftgl description {Build with ftgl support} {
#     configure.args-delete   --enable-builtin-ftgl
#     configure.args-append   --disable-builtin-ftgl \
#     configure.args-append   --with-ftgl-incdir="${prefix}/include" \
#                             --with-ftgl-libdir="${prefix}/lib"
#     depends_lib-append      port:ftgl
# }

# variant qt_x11 conflicts qt_mac description {Build with Qt support via X11 UI} {
#     # fix where "qglobal.h" is found in all files that use or reference it
#     post-patch {
#         foreach tpf { configure graf2d/qt/src/TGQt.cxx } {
#             reinplace "s@Qt/qglobal\\.h@QtCore/qglobal\\.h@g" \
#                 ${worksrcpath}/${tpf}
#         }
#         reinplace "/qttestdir/s@Qt@QtCore@g" ${worksrcpath}/configure
#     }

#     configure.env-append    QTDIR=${prefix}/libexec/qt4-x11
#     configure.args-delete   --disable-qt
#     configure.args-delete   --disable-qtgsi
#     configure.args-append   --enable-qt --enable-qtgsi \
#                             --with-qt-incdir="${prefix}/include" \
#                             --with-qt-libdir="${prefix}/lib"
#     depends_lib-append      port:qt4-x11
# }
