PortSystem          1.0

name                lua51
version             5.1.5
revision            4
set abi             [join [lrange [split $version "."] 0 end-1] "."]
categories          lang
license             MIT
platforms           darwin
maintainers         nomaintainer
description         powerful, lightweight programming language
long_description    \
    Lua is a powerful, light-weight programming language designed for \
    extending applications. Lua is also frequently used as a general-purpose, \
    standalone language.

homepage            http://www.lua.org
distname            lua-${version}
master_sites        ${homepage}/ftp/

checksums           sha1    b3882111ad02ecc6b972f8c1241647905cb2e3fc \
                    rmd160  b0c2234675335a8bf158a125b6778cf800c754a0

depends_lib         port:readline

test.run            yes
test.env            DYLD_LIBRARY_PATH=./lib

patchfiles          patch-Makefile.diff \
                    patch-src_Makefile.diff \
                    patch-etc_lua.pc.diff

post-patch {
    reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/src/luaconf.h
    
    # reinplace %PREFIX% to fix @executable_path, cf. ticket no. 28726
    reinplace "s|%PREFIX%|${prefix}|g" \
        ${worksrcpath}/Makefile \
        ${worksrcpath}/src/Makefile \
        ${worksrcpath}/etc/lua.pc
}

use_configure       no

build.target        macosx
build.args-append   V=${abi} \
                    R=${version} \
                    PREFIX=${prefix} \
                    CC="${configure.cc}" \
                    LDFLAGS="${configure.ldflags} [get_canonical_archflags ld]" \
                    CFLAGS="${configure.cflags} ${configure.cppflags} [get_canonical_archflags cc]"

use_parallel_build  no

destroot.post_args-append   INSTALL_TOP=${destroot}${prefix}
post-destroot {
    #  install docs
    xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 -W ${worksrcpath} COPYRIGHT HISTORY README \
             ${destroot}${prefix}/share/doc/${name}
    foreach f [glob ${worksrcpath}/doc/*] {
        xinstall -m 0644 $f ${destroot}${prefix}/share/doc/${name}
    }
        
    #  install test and etc stuff 
    xinstall -d -m 0755 ${destroot}${prefix}/share/examples/${name}
    copy ${worksrcpath}/test ${destroot}${prefix}/share/examples/${name}/test
    copy ${worksrcpath}/etc ${destroot}${prefix}/share/examples/${name}/etc

    #  version executable and man pages files
    foreach f [glob ${destroot}${prefix}/bin/*] {
        move $f ${f}${abi}
    }
    foreach f [glob ${destroot}${prefix}/share/man/man1/*] {
        move $f [file rootname $f]-${abi}[file extension $f]
    }

    #  install versioned pkgconfig file
    xinstall -d -m 0755 ${destroot}${prefix}/lib/pkgconfig
    xinstall -m 0644 ${worksrcpath}/etc/lua.pc ${destroot}${prefix}/lib/pkgconfig/lua-${abi}.pc
}

livecheck.type  regex
livecheck.url   ${master_sites}
livecheck.regex {lua-(\d+(?:\.\d+)*)}