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

name                levmar
version             2.6
revision            3
categories          math
platforms           darwin
license             GPL

maintainers         {gmail.com:tlockhart1976 @lockhart} openmaintainer

description         Levenberg-Marquardt nonlinear least squares algorithm
long_description    levmar is a native ANSI C implementation of the Levenberg-Marquardt \
                    optimization algorithm.  Both unconstrained and constrained (under linear \
                    equations, inequality and box constraints) Levenberg-Marquardt variants are \
                    included.  The LM algorithm is an iterative technique that finds a local \
                    minimum of a function that is expressed as the sum of squares of nonlinear \
                    functions.  It has become a standard technique for nonlinear least-squares \
                    problems and can be thought of as a combination of steepest descent and the \
                    Gauss-Newton method.  When the current solution is far from the correct on, \
                    the algorithm behaves like a steepest descent method: slow, but guaranteed \
                    to converge.  When the current solution is close to the correct solution, it \
                    becomes a Gauss-Newton method.

homepage            http://www.ics.forth.gr/~lourakis/levmar/
master_sites        http://www.ics.forth.gr/~lourakis/levmar/

extract.suffix      .tgz
checksums           rmd160  2b29daea12804050c8d3939b8c142d27d36bffc8 \
                    sha256  3bf4ef1ea4475ded5315e8d8fc992a725f2e7940a74ca3b0f9029d9e6e94bad7

# Patch taken from levmar RPM by Eric Smith <eric@brouhaha.com>
patch.pre_args-replace  -p0 -p1
patchfiles          patch-Makefile.so.diff

default_variants +atlas

variant universal {}

variant atlas description {Use MacPorts' ATLAS instead of Accelerate Framework} {
    depends_lib-append  port:atlas
}

if {! [variant_isset atlas]} {
    conflicts       atlas
}

global makefile
set makefile ${worksrcpath}/Makefile.so

use_configure       no

set ver_major [lindex [split ${version} "."] 0]
set ver_minor [lindex [split ${version} "."] 1]
set abi_major 2
set abi_minor 2

post-patch {
    set fl [open ${makefile}]
    set lines [read $fl]
    close $fl
    foreach line [split $lines \n] {
        if {[regexp {(MAJ|MIN)[[:space:]]*=[[:space:]]*([[:digit:]]+)} ${line} matched key value] == 1} {
            if {${key} == "MAJ"} {
                ui_debug "Found ABI major field ${value}"
                set abi_major ${value}
            } else {
                ui_debug "Found ABI minor field ${value}"
                set abi_minor ${value}
            }
        }
    }

    if {${abi_major} != ${ver_major} || ${abi_minor} > ${ver_minor}} {
        ui_error "levmar version ${ver_major} is inconsistent with ABI version ${abi_major}.${abi_minor}"
        return -code error
    }

    if {[variant_isset atlas]} {
        reinplace "s|^LAPACKLIBS=.*$|LAPACKLIBS= -L${prefix}/lib -lsatlas|g" ${makefile}
    }

    reinplace "s|\r||g" ${worksrcpath}/README.txt
    file mkdir ${worksrcpath}/sobj
}

build.args-append   -f ${makefile}
build.args-append   CC="${configure.cc} [get_canonical_archflags cc]"
build.args-append   LDFLAGS+=-Wl,-rpath,${prefix}/lib
# Add this local rpath to allow port test to run successfully
build.args-append   LDFLAGS+=-Wl,-rpath,${worksrcpath}/sobj

test.run            yes
test.cmd            ${worksrcpath}/lmdemo
test.target         {}

destroot {
    # library
    xinstall -m 755 ${worksrcpath}/sobj/liblevmar.${abi_major}.${abi_minor}.dylib ${destroot}${prefix}/lib/liblevmar.${abi_major}.${abi_minor}.dylib

    # test program
    xinstall -m 755 ${worksrcpath}/lmdemo ${destroot}${prefix}/bin/lmdemo
    # Remove the local rpath which was present to allow port test to run successfully
    system "install_name_tool -delete_rpath ${worksrcpath}/sobj ${destroot}${prefix}/bin/lmdemo"

    # header files
    xinstall -m 755 ${worksrcpath}/levmar.h ${destroot}${prefix}/include

    # documentation
    xinstall -m 755 -d ${destroot}${prefix}/share/${name}
    xinstall -m 644 ${worksrcpath}/LICENSE ${destroot}${prefix}/share/${name}
    xinstall -m 644 ${worksrcpath}/README.txt ${destroot}${prefix}/share/${name}
}

post-destroot {
    ln -s liblevmar.${abi_major}.${abi_minor}.dylib ${destroot}${prefix}/lib/liblevmar.${ver_major}.dylib
    ln -s liblevmar.${abi_major}.${abi_minor}.dylib ${destroot}${prefix}/lib/liblevmar.dylib
}

livecheck.type      regex
livecheck.url       http://www.ics.forth.gr/~lourakis/levmar/
livecheck.regex     "${name}-(\\d+(?:\\.\\d+)*)"
