# -*- 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 90600 2012-03-09 19:14:12Z pixilla@macports.org $

PortSystem          1.0

name                levmar
version             2.6
revision            1
categories          devel
platforms           darwin
license             GPL

maintainers         fourpalms.org: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      -p1
patchfiles          patch-Makefile.so.diff

use_configure       no
#configure.args      --with-omniORB-config="${prefix}/etc/omniORB.cfg"

variant universal {}

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 ${worksrcpath}/Makefile.so]
    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
    }

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

build.args-append   -f Makefile.so
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+)*)"
