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

PortSystem          1.0

name                lua
conflicts           lua50
version             5.3.3
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/
master_sites        ${homepage}ftp/

checksums           rmd160  c4989f7ae83c61f745fc174f7953c4c601fdc5de \
                    sha256  5113c06884f7de453ce57702abaac1d618307f33f6789fa870e87a59d772aca2

patchfiles          patch-Makefile.diff \
                    patch-src-Makefile.diff \
                    patch-src-luaconf.h.diff

# TODO: also add lua50 and lua51 here
subport             lua52 {
    version         5.2.3
    revision        0
    checksums       rmd160  b8714669c8c0f8272104effd8a9675921ddb2fa5 \
                    sha256  13c2fb97961381f7d06d5b5cea55b743c163800896fd5c5e2356201d3619002d

    patchfiles      patch-Makefile-52.diff \
                    patch-src-Makefile-52.diff \
                    patch-src-luaconf.h-52.diff
}

set branch          [join [lrange [split ${version} .] 0 1] .]

depends_lib         port:readline
build.target        macosx

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

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g"   ${worksrcpath}/lua.pc ${worksrcpath}/Makefile ${worksrcpath}/src/Makefile ${worksrcpath}/src/luaconf.h
    reinplace "s|@VERSION@|${version}|g" ${worksrcpath}/lua.pc ${worksrcpath}/Makefile ${worksrcpath}/src/Makefile
    reinplace "s|@BRANCH@|${branch}|g"   ${worksrcpath}/lua.pc ${worksrcpath}/Makefile ${worksrcpath}/src/Makefile
}

configure {
    reinplace -E "/^MYLDFLAGS=/s|\$| ${configure.ldflags} [get_canonical_archflags]|" ${worksrcpath}/src/Makefile
    reinplace -E "/^CFLAGS=/s|\$| ${configure.cflags} ${configure.cppflags} [get_canonical_archflags]|" ${worksrcpath}/src/Makefile
    reinplace "s|CC= .*\$|CC= ${configure.cc}|" ${worksrcpath}/src/Makefile
}

use_parallel_build  no

destroot.target install INSTALL_TOP=${destroot}${prefix}
post-destroot {
    # documentation
    set docdir ${prefix}/share/doc/${subport}
    xinstall -d -m 0755 ${destroot}${docdir}/html
    xinstall -m 0644 -W ${worksrcpath} COPYRIGHT README ${destroot}${docdir}
    foreach html [glob ${worksrcpath}/doc/*.html ${worksrcpath}/doc/*.css ${worksrcpath}/doc/*.gif ${worksrcpath}/doc/*.png] {
        xinstall -m 0644 ${html} ${destroot}${docdir}/html
    }
}

if {$subport eq $name} {
    post-extract {
       xinstall -m 0644 -W ${filespath} COPYRIGHT lua.pc ${worksrcpath}
    }

    post-destroot {
        xinstall -W ${worksrcpath} -m 0644 lua.pc ${destroot}${prefix}/lib/pkgconfig/
    }
} else {
    post-extract {
       xinstall -m 0644 -W ${filespath}/${branch} COPYRIGHT lua.pc ${worksrcpath}
    }

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

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

# TODO: fixme (a different livecheck is needed for different subports)
livecheck.type  regex
livecheck.url   ${master_sites}
livecheck.regex {lua-(\d+(?:\.\d+)*)}