# $Id: Portfile 86240 2011-10-23 19:34:49Z adfernandes@macports.org $

PortSystem      1.0
PortGroup       archcheck 1.0
PortGroup       muniversal 1.0

name            glpk
version         4.47
license         GPL-3+
categories      math lang
platforms       darwin
maintainers     fastmail.fm:lutz.horn
description     GNU Linear Programming Kit

long_description \
    The GLPK (GNU Linear Programming Kit) package is intended for \
    solving large-scale linear programming (LP), mixed integer \
    programming (MIP), and other related problems. It is a set of \
    routines written in ANSI C and organized in the form of a \
    callable library.

homepage        http://www.gnu.org/software/${name}/
master_sites    gnu

checksums       sha1    35e16d3167389b6bc75eb51b4b48590db59f789c \
                rmd160  5a024e47b99392057cced2d91fb4bd42e4c5f6a3

depends_lib     port:gmp port:zlib

archcheck.files \
    lib/libgmp.dylib \
    lib/libz.dylib

configure.args  --with-gmp --with-zlib

if { [variant_isset odbc] || [variant_isset mysql] } {
    depends_lib-append port:libtool

    archcheck.files-append \
        lib/libltdl.dylib

    configure.args-append --enable-dl
}

use_parallel_build  yes

test.run        yes
test.target     check

post-patch {
    # glpk uses nmedit to make certain symbols local.
    # octave 3.0.3 relies on the obsolete __glp_lib_print_hook and __glp_lib_fault_hook symbols, so they must be kept global.
    # See http://www.nabble.com/octave-3.0.3-build-problem-due-to-glpk-td19945239.html for a discussion.
    reinplace \
        "s|export-symbols-regex '^(glp_\\|_glp_lpx_).*'|export-symbols-regex '^(glp_\\|_glp_lpx_\\|_glp_lib_fault_hook\\|_glp_lib_print_hook).*'|g" \
        ${worksrcpath}/src/Makefile.in
}

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

post-destroot {
    xinstall -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} AUTHORS COPYING ChangeLog NEWS README \
        doc/glpk.pdf doc/gmpl.pdf doc/glpk_faq.txt \
        ${destroot}${docdir}

    set examplesdir ${prefix}/share/examples/${name}
    xinstall -d ${destroot}${examplesdir}
    eval xinstall -m 644 \
        [glob -directory ${worksrcpath}/examples *.{c,mod,lp,mps,dat}] \
        ${destroot}${examplesdir}

    xinstall -d ${destroot}${examplesdir}/sql
    eval xinstall -m 644 \
        [glob -directory ${worksrcpath}/examples/sql *.{mod,sql}] \
        ${destroot}${examplesdir}/sql
}

variant odbc description {enable MathProg ODBC support} {
    depends_lib-append port:unixODBC

    archcheck.files-append \
        lib/libodbc.dylib

    configure.args-append --enable-odbc=unix
    post-patch {
        reinplace \
            "s|libodbc.so|${prefix}/lib/libodbc.dylib|g" \
            ${worksrcpath}/configure
    }
}

variant mysql description {enable MathProg MySQL support} {
    depends_lib-append path:bin/mysql_config5:mysql5

    archcheck.files-append \
        lib/mysql5/mysql/libmysqlclient.dylib

    configure.args-append --enable-mysql
    post-patch {
        reinplace \
            "s|/usr/include/mysql|${prefix}/include/mysql5/mysql|g" \
            ${worksrcpath}/configure
        reinplace \
            "s|libmysqlclient.so|${prefix}/lib/mysql5/mysql/libmysqlclient.dylib|g" \
            ${worksrcpath}/configure
    }
}
