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

name                libsvm
epoch               3
version             3.36
revision            0

categories          math
maintainers         nomaintainer
license             BSD

description         A free Support Vector Machine implementation

long_description    {*}${description} By Chih-Chung Chang and Chih-Jen Lin of    \
                    National Taiwan University. \"LIBSVM is an integrated        \
                    software for support vector classification, (C-SVC, nu-SVC), \
                    regression (epsilon-SVR, nu-SVR) and distribution estimation \
                    (one-class SVM). It supports multi-class classification.\"

homepage            https://www.csie.ntu.edu.tw/~cjlin/libsvm/
master_sites        ${homepage}
checksums           rmd160  e6141c1a8042cb5d26c2238b9c4ae14ba2a6da61 \
                    sha256  bc92901fbb928c44bb6d0c38189624c7443bcdbf1dd8350b4914e58e57b93c11 \
                    size    943316

patchfiles          patch-Makefile.diff

variant universal {}

set libver          ${version}.0
build.target        all
build.env-append    VERSION=${libver}
destroot.env-append VERSION=${libver}

set docdir          ${prefix}/share/doc/${name}

# create Python subports
set python_versions {310 311 312 313}
foreach v ${python_versions} {
    subport py${v}-${name} {
        set python_branch   [string index ${v} 0].[string range ${v} 1 end]
        set python_prefix   ${frameworks_dir}/Python.framework/Versions/${python_branch}
        set python_bindir   ${python_prefix}/bin
        set python_bin      ${python_prefix}/bin/python${python_branch}
        set python_pkgd     ${python_prefix}/lib/python${python_branch}/site-packages

        description         Python ${python_branch} bindings for {*}${name}
        long_description    This package provides Python ${python_branch} modules for {*}${name}.

        categories          math python
        platforms           {darwin any}
        supported_archs     noarch

        patchfiles-append   patch-tools.diff

        depends_lib-append  port:${name}
        depends_lib-append  port:py${v}-numpy \
                            port:py${v}-scipy
    }
}

if {${subport} eq ${name}} {
    pre-destroot {
        xinstall -m 0755 -d ${destroot}${docdir}
        xinstall -m 0755 -d ${destroot}${docdir}/example
    }

    post-destroot {
        xinstall -m 0644 -W ${worksrcpath} COPYRIGHT README FAQ.html \
            ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} heart_scale \
            ${destroot}${docdir}/example
    }
} elseif {[string match "py*" ${subport}]} {
    build {}
    pre-destroot {
        xinstall -m 0755 -d ${destroot}${python_pkgd}/libsvm
        xinstall -m 0755 -d ${destroot}${prefix}/share/doc/${subport}
        xinstall -m 0755 -d ${destroot}${python_bindir}
    }

    destroot {
        xinstall -m 0644 {*}[glob ${worksrcpath}/python/libsvm/*.py] \
                     ${destroot}${python_pkgd}/libsvm
        xinstall -m 0644 -W ${worksrcpath}/python README \
                     ${destroot}${prefix}/share/doc/${subport}/
        fs-traverse f ${destroot}${python_pkgd}/libsvm {
            if {[file extension ${f}] eq ".py"} {
                reinplace -q "s|#!/usr/bin/env python||" ${f}
            }
        }

        foreach tool {checkdata.py easy.py grid.py subset.py} {
            set new_tool ${destroot}${python_bindir}/svm-${tool}
            xinstall -m 0755 -W ${worksrcpath}/tools $tool ${new_tool}
            reinplace -q "s|/usr/bin/env python|${python_bin}|" ${new_tool}
            reinplace -q "s|@PREFIX@|${prefix}|" ${new_tool}
        }
    }

    post-destroot {
        foreach bin [glob -nocomplain -tails -directory "${destroot}${python_bindir}" *] {
            if {[catch {file type "${destroot}${prefix}/bin/${bin}${python.link_binaries_suffix}"}]} {
                ln -s "${python_prefix}/bin/${bin}" "${destroot}${prefix}/bin/${bin}-${python_branch}"
            }
        }
    }
    notes-append    "The tool scrips are renamed with the added prefix 'svm-',\
                    eg. 'svm-checkdata.py'."
}

# TODO: consider adding java and matlab support (preferably as subports),
#       and 'svm-toy' (as non-default variant).

livecheck.type      regex
livecheck.regex     {Version ([0-9.]+) }
