# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4
# $Id: Portfile 64569 2010-03-09 15:49:27Z jameskyle@macports.org $

PortSystem          1.0

categories          math science
name                shogun
version             0.9.1
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
distfiles            shogun-${version}${extract.suffix}

checksums           md5     322ea08520d5c0fb2427f44ed654e4d1 \
                    sha1    6826a9937abe24830647cea867351e4eeff689ab \
                    rmd160  332a9b32e476cd2ab0193a58edc8667e91b95633

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

depends_build       port:bzip2 \
                    port:gsed \
                    port:gcc43

depends_lib         port:swig-python \
                    port:atlas \
                    port:readline \
                    port:glpk \
                    port:python26 \
                    port:py26-numpy
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} \
                    --cc=${prefix}/bin/gcc-mp-4.3 \
                    --cxx=${prefix}/bin/g++-mp-4.3 \
                    --includes=${python_prefix}/include/python2.6 \
                    --python=${python_prefix}/bin/python2.6 \
                    --pydir=${python_prefix}/lib/python2.6/site-packages \
                    --disable-doxygen

configure.cc        ${prefix}/bin/gcc-mp-4.3
configure.compiler  macports-gcc-4.3

pre-fetch {
  if {[ regexp {^[2-8]} os.version]} {
    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 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:R port:swig-r
}

variant octave description {Build the Octave API} {
  depends_lib-append port:octave 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
# }

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.]+).*}
