# -*- 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 90820 2012-03-15 03:13:40Z jmr@macports.org $

PortSystem          1.0

categories          math science
name                shogun
version             0.9.3
set branch          [join [lrange [split ${version} .] 0 1] .]

maintainers         jameskyle

platforms           darwin

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           md5     99afbb304d524335fc181c553047cb51 \
                    sha1    d559dff3e11f777a23f00278d78d259ad896b829 \
                    rmd160  9638a6b747a1177b048720b8999c60f33c7df5ef

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

patchfiles          swig_version.patch

depends_build       port:bzip2 \
                    port:gsed

depends_lib         port:swig-python \
                    port:atlas \
                    port:readline \
                    port:glpk \
                    port:python26 \
                    port:py26-numpy \
                    port:hdf5-18

universal_variant   no

set python_prefix   ${prefix}/Library/Frameworks/Python.framework/Versions/2.6
configure.args      --disable-svm-light \
                    --libs=${prefix}/lib \
                    --install-path=${prefix} \
                    --includes=${prefix}/include \
                    --destdir=${destroot} \
                    --includes=${python_prefix}/include/python2.6 \
                    --python=${python_prefix}/bin/python2.6 \
                    --pydir=${python_prefix}/lib/python2.6/site-packages \
                    --disable-doxygen \
                    --disable-hdf5

pre-fetch {
  if {[ regexp {^[2-8]} ${os.major}]} {
    return -code error "Shogun is only supported on leopard or above"
  }
}

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 "libshogun libshogunui cmdline python python_modular"

if {[variant_isset r]} {lappend interfaces "r"}
if {[variant_isset octave]} {lappend interfaces "octave,octave_modular"}
if {[variant_isset elwms]  && !([variant_isset python] &&
    [variant_isset octave] &&   [variant_isset r])} {
  return -code error "Must set python and both r and octave variants to build elwms interface"
} elseif {[variant_isset elwms]} {
  lappend interfaces "elwms"
}

# remove python interface if necessary before appending to args
if {[variant_isset no_python26] && ![variant_isset python25]} {
    set interfaces [lremove ${interfaces} "python"]
    set interfaces [lremove ${interfaces} "python_modular"]
    depends_lib-delete port:swig-python
}

#if {[variant_isset matlab]} {lappend interfaces "matlab"}
set interfaces [join $interfaces ","]

configure.args-append --interfaces=$interfaces

variant elwms description {Build elwms interface. requires python plus  one other interface} {}

variant python25 requires no_python26 description {Build Python 2.5 API} {
    configure.args-append \
        --includes=${prefix}/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 \
        --python=${prefix}/Library/Frameworks/Python.framework/Versions/2.5/bin/python \
        --pydir=${prefix}/Library/Frameworks/Python.framework/Versions/2.5/site-packages
    depends_lib-append port:python25 \
                       port:py25-numpy
}

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

variant no_python26 description {Do not build Python 2.6 API} {
  configure.args-delete \
  --includes=${python_prefix}/include/python2.6 \
  --python=${python_prefix}/bin/python2.6 \
  --pydir=${python_prefix}/lib/python2.6/site-packages


  depends_lib-delete port:python26 \
                     port:py26-numpy

}

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 no_glpk description {Disable glpk support} {
    configure.args-append --disable-glpk
     depends_lib-delete port:glpk
}

#variant matlab description {Build the Matlab API} {}
# variant doc description {Install the documentation for shogun} {
#   depends_build-append port:texlive \
#                        path:bin/dot:graphviz \
#                        port:doxygen
#
#   configure.args-delete --disable-doxygen
# }

variant gcc43 conflicts gcc44 gcc45 description {build with gcc 4.3} {}
variant gcc44 conflicts gcc43 gcc45 description {build with gcc 4.4} {}
variant gcc45 conflicts gcc43 gcc44 description {build with gcc 4.5} {}

if {[variant_isset gcc43]} {
    set gccversion 4.3
} elseif {[variant_isset gcc44]} {
    set gccversion 4.4
} else {
    default_variants +gcc45
    set gccversion 4.5
}

set gccnumber [join [split ${gccversion} .] ""]

depends_lib-append      port:gcc${gccnumber}
configure.cc            ${prefix}/bin/gcc-mp-${gccversion}
configure.compiler      macports-gcc-${gccversion}
configure.args-append   --cc=${prefix}/bin/gcc-mp-${gccversion} \
                        --cxx=${prefix}/bin/g++-mp-${gccversion}


pre-extract {
  # Before doing anything, verify the correct swig bindings are present for
  # our variants
  if {[file exists ${prefix}/bin/swig]} {
      ui_debug "Attempting to find swig version"
      set swig_version [exec ${prefix}/bin/swig -version]
      regexp {(\d.?)+} $swig_version sversion
      set sversion [string trimright $sversion \n]

  } else {
      set sversion 0.0.0
  }
  ui_debug "Found swig version ${sversion}"
  if {[variant_isset python]} {

  }
  if {[variant_isset python] || [variant_isset python26]} {
      ui_debug "Looking for swig python interface at ${prefix}/share/swig/${sversion}/python/python.swg"
    if {![file exists ${prefix}/share/swig/${sversion}/python/python.swg]} {
      ui_error "To install shogun with the python variant, swig must be installed with the python variant as well."
      return -code error "incompatible swig installation"
    }
  }
}

pre-configure {
  reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/configure
  reinplace "s|= $\{_libdir\}/$\{_pydir\}|= $\{_pydir\}|g" ${worksrcpath}/configure
}
post-destroot {
  if {[variant_isset doc]} {
    system "cd ${worksrcpath}/../doc && make"
    file mkdir ${destroot}${prefix}/share/doc/${name}
    file copy ${worksrcpath}/../doc ${destroot}${prefix}/share/doc/${name}/doc

  }
}

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