# $Id: Portfile 106226 2013-05-19 20:49:58Z and.damore@macports.org $

PortSystem          1.0

name                luajit
version             2.0.1
revision            6
categories          lang
platforms           darwin
license             BSD
maintainers         and.damore openmaintainer
description         a Just-In-Time Compiler for Lua
long_description    LuaJIT is a Just-In-Time Compiler for the Lua programming language.
homepage            http://luajit.org
master_sites        ${homepage}/download
distname            LuaJIT-${version}

checksums           md5     85e406e8829602988eb1233a82e29f1f \
                    sha1    330492aa5366e4e60afeec72f15e44df8a794db5 \
                    rmd160  99b6ef067a3f09865ff312baeb1ceb012bb9acb3

patchfiles          patch-luajit-v2.0.1_hotfix1.diff
patch.pre_args      -p1

use_configure       no

macosx_deployment_target  10.6

build.target        amalg
build.args-append   CC="${configure.cc}"  \
                    CFLAGS="${configure.cflags} ${configure.cppflags} [get_canonical_archflags]" \
                    LDFLAGS="${configure.ldflags} [get_canonical_archflags]" \
                    PREFIX="${prefix}" \
                    Q=""

post-patch {
    reinplace "s|/usr/local|${prefix}|"    ${worksrcpath}/etc/luajit.pc

    # luajit doesn't depends on lua but if lua 5.2 is installed we're going to change the conf file so we can use 5.2 extensions
    if {    ![catch {set lua_version [string range [lindex [lindex [registry_active lua] 0] 1] 0 2]}]
        &&  [vercmp $lua_version 5.2] >= 0} {

        reinplace "s|5\\.1|5.2|g" ${worksrcpath}/src/luaconf.h
        destroot.args-append    ABIVER=$lua_version
        build.args-delete       CFLAGS="${configure.cflags} ${configure.cppflags} [get_canonical_archflags]"
        build.args-append       CFLAGS="${configure.cflags} ${configure.cppflags} [get_canonical_archflags] -DLUAJIT_ENABLE_LUA52COMPAT"
    }
}

destroot.args-append   PREFIX="${prefix}"

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/img
    xinstall -m 644  ${worksrcpath}/COPYRIGHT ${worksrcpath}/README ${destroot}${prefix}/share/doc/${name}
    eval xinstall -m 644 [glob ${worksrcpath}/doc/*.html] ${destroot}${prefix}/share/doc/${name}
    eval xinstall -m 644 [glob ${worksrcpath}/doc/*.css] ${destroot}${prefix}/share/doc/${name}
    eval xinstall -m 644 [glob ${worksrcpath}/doc/img/*] ${destroot}${prefix}/share/doc/${name}/img

    xinstall -d -m 755 ${destroot}${prefix}/share/pkgconfig
    xinstall -m 644 ${worksrcpath}/etc/luajit.pc ${destroot}${prefix}/share/pkgconfig
    xinstall -m 644 ${worksrcpath}/etc/luajit.1 ${destroot}${prefix}/share/man/man1
}

livecheck.type  regex
livecheck.url   ${master_sites}.html
livecheck.regex {LuaJIT-(\d+(?:\.\d+)*(?:-beta\d+)?).tar.gz}
