# -*- 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 106050 2013-05-14 02:25:37Z ryandesign@macports.org $

PortSystem          1.0

name                shogun
version             2.1.0
set branch          [join [lrange [split ${version} .] 0 1] .]
revision            1
categories-append   science
platforms           darwin
license             GPL-3

maintainers         jameskyle openmaintainer

description         The machine learning toolbox's focus is on large scale \
                    kernel methods

long_description    ${description} and especially on Support Vector Machines \
                    (SVM). It provides a generic SVM object interfacing to \
                    several different SVM implementations, among them the \
                    state of the art OCAS, LibSVM, SVMLight, SVMLin and GPDT.

homepage            http://shogun-toolbox.org
master_sites        ${homepage}/archives/shogun/releases/${branch}/sources/

use_bzip2           yes

checksums           rmd160  5fece569be7d95bbe9ad406f1b5e10f06f4ac20e \
                    sha256  6c47941a98d506d7b1e21d5bfd511f8684a2e1f371accd4756cdc8821916a63b

worksrcdir          ${name}-${version}/src

patchfiles          patch-src_configure.diff

post-patch {
    reinplace "s|@@MAKEDEPEND@@|${configure.cc} -MM|g" ${worksrcpath}/configure
}

depends_build-append port:pkgconfig

depends_lib-append  port:hdf5-18 \
                    port:json-c \
                    port:libxml2 \
                    port:readline \
                    port:atlas \
                    port:arpack \
                    port:eigen3 \
                    port:lp_solve \
                    port:glpk \
                    port:lzo2 \
                    port:snappy \
                    port:gzip \
                    port:bzip2 \
                    port:xz

universal_variant   no

pre-fetch {
    if {${os.major} <= 8} {
        return -code error "Shogun is only supported on Leopard or above"
    }
}

# Define compilers variants
variant gcc45 conflicts gcc46 gcc47 gcc48 clang33 description {Compile with gcc 4.5} {
    configure.compiler      macports-gcc-4.5
    depends_lib-append      port:gcc45
    configure.args-append   --ldflags=-lgfortran
}

variant gcc46 conflicts gcc45 gcc47 gcc48 clang33 description {Compile with gcc 4.6} {
    configure.compiler      macports-gcc-4.6
    depends_lib-append      port:gcc46
    configure.args-append   --ldflags=-lgfortran
}

variant gcc47 conflicts gcc45 gcc46 gcc48 clang33 description {Compile with gcc 4.7} {
    configure.compiler      macports-gcc-4.7
    depends_lib-append      port:gcc47
    configure.args-append   --ldflags=-lgfortran
}

variant gcc48 conflicts gcc45 gcc46 gcc47 clang33 description {Compile with gcc 4.8} {
    configure.compiler      macports-gcc-4.8
    depends_lib-append      port:gcc48
    configure.args-append   --ldflags=-lgfortran
}

variant clang33 conflicts gcc45 gcc46 gcc47 gcc48 description {Compile with clang 3.3} {
    configure.compiler      macports-clang-3.3
    depends_lib-append      port:clang-3.3 \
                            port:gcc47
    configure.args-delete   --disable-cpudetection
    configure.args-append   --enable-cpudetection \
                            --ldflags=-lgfortran
    configure.env-append    POSTLINKFLAGS="-L${prefix}/lib/gcc47"
}

if {![variant_isset gcc45] && ![variant_isset gcc46] &&
    ![variant_isset gcc48] && ![variant_isset clang33]
} then {
    default_variants +gcc47
}

# Define python variants
set pythons_suffixes {26 27}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

proc python_prefix {} {
    global prefix pythons_suffixes
    foreach s ${pythons_suffixes} {
        if {[variant_isset python${s}]} {
            set p ${prefix}/bin/python[string index ${s} 0].[string index ${s} 1]
            return [file normalize [exec ${p} -c "import sys; print(sys.prefix)"]]
        }
    }
    error "Python support not enabled."
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    eval [subst {
        variant ${p} description "Use Python ${v} for python bindings" conflicts ${c} {
            depends_lib-append      port:${p} \
                                    port:py${s}-numpy \
                                    port:swig-python
            set py_prefix \[python_prefix\]
            configure.args-append   --python=${prefix}/bin/python${v} \
                                    --pydir=\${py_prefix}/lib/python${v}/site-packages \
                                    --includes=\${py_prefix}/include/python${v}
            post-patch {
                reinplace "s|^PYTHON=python$|PYTHON=${prefix}/bin/python${v}|g" ${worksrcpath}/configure
            }
        }
    }]
}

variant perl description {Build the Perl API} {
    depends_lib-append  port:swig-perl
}

variant r description {Build the R API} {
    depends_lib-append  port:swig-r
}

variant octave description {Build the Octave API} {
    depends_lib-append  port:swig-octave
}

variant java description {Build the Java API} {
    depends_lib-append  port:swig-java
}

variant csharp description {Build the C# API} {
    depends_lib-append  port:swig-csharp
}

variant lua description {Build the Lua API} {
    depends_lib-append  port:swig-lua
}

variant ruby description {Build the Ruby API} {
    depends_lib-append  port:swig-ruby
}

variant elwms description {Build the elwms (eierlegende wollmilchsau) interface. Requires python plus one other interface} {
}

variant hmm_parallel description {Enable parallel structures in hmm training} {
    configure.args-delete --disable-hmm-parallel
    configure.args-append --enable-hmm-parallel
}

proc lremove {list elem} {
    set index [lsearch -exact $list $elem]
    return [lreplace $list $index $index]
}

# Interfaces are added by a comma delimited argument list to --interfaces.
# We check for variants and then build this list accordingly
set interfaces "cmdline_static"

if {[variant_isset python26] || [variant_isset python27]} {
    lappend interfaces "python_modular"
}

if {[variant_isset perl]} {
    lappend interfaces "perl_modular"
}

if {[variant_isset r]} {
    lappend interfaces "r_modular"
}

if {[variant_isset octave]} {
    lappend interfaces "octave_modular"
}

if {[variant_isset java]} {
    lappend interfaces "java_modular"
}

if {[variant_isset csharp]} {
    lappend interfaces "csharp_modular"
}

if {[variant_isset lua]} {
    lappend interfaces "lua_modular"
}

if {[variant_isset ruby]} {
    lappend interfaces "ruby_modular"
}

if {[variant_isset elwms] && !(([variant_isset python26] || [variant_isset python27]) && [variant_isset octave] && [variant_isset r])} {
    return -code error "Must set python2x and both r and octave variants to build elwms interface"
} elseif {[variant_isset elwms]} {
    lappend interfaces "elwms_static"
}

configure.args-append \
                    --enable-hdf5 \
                    --enable-json \
                    --enable-xml \
                    --enable-readline \
                    --enable-largefile \
                    --disable-spinlocks \
                    --enable-lapack \
                    --enable-arpack \
                    --disable-mosek \
                    --disable-superlu \
                    --disable-nlopt \
                    --enable-eigen3 \
                    --disable-cplex \
                    --enable-lpsolve \
                    --enable-glpk \
                    --enable-lzo \
                    --enable-snappy \
                    --enable-gzip \
                    --enable-bzip2 \
                    --enable-lzma \
                    --enable-bigstates \
                    --enable-hmmcache \
                    --enable-svm-light \
                    --disable-logcache \
                    --enable-shortrealkernelcache \
                    --enable-logsum-array \
                    --disable-hmm-parallel \
                    --disable-doxygen \
                    --enable-optimization \
                    --disable-cpudetection \
                    --includes=${prefix}/include \
                    --libs=${prefix}/lib \
                    --destdir=${destroot} \
                    --prefix=${prefix}

set interfaces [join $interfaces ","]

configure.args-append --interfaces=$interfaces

livecheck.type      regex
livecheck.url       http://www.shogun-toolbox.org
livecheck.regex     {SHOGUN ([0-9.]+)}
