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

PortSystem          1.0
PortGroup           github 1.0
PortGroup           lua 1.0

name                lua-luarocks
github.setup        luarocks luarocks 3.9.2 v
revision            0
license             MIT
categories          lua devel
maintainers         {l2dy @l2dy} openmaintainer
supported_archs     noarch
platforms           any

description         Manager for rocks, lua extensions.
long_description    \
    LuaRocks is a deployment and management system for Lua modules that allows \
    you to install self-contained modules (\"rocks\") which also contain \
    version dependency information. It handles both install time dependencies \
    and runtime version check. LuaRocks supports local and remote \
    repositories, and multiple local rocks trees.

homepage            https://luarocks.org/

checksums           rmd160  4ddf475e1934d6c6de351b91d7787750b84afa9a \
                    sha256  1097a4c8394454b8777823a72e44e88e1bd8015f5022ac8101af6333a315864e \
                    size    5350213

patchfiles          patch-src_luarocks_core_cfg.diff
post-patch {
    reinplace       "s|%PREFIX%|${prefix}|g" ${worksrcpath}/src/luarocks/core/cfg.lua
}

use_parallel_build  no

foreach branch          ${lua.branches} {
    subport lua[join [split ${branch} .] ""]-luarocks {
        lua.version     ${branch}

        post-destroot {
            # only install the Lua module files
            foreach dir {bin etc} {
                delete  ${destroot}${prefix}/${dir}
            }
        }

        livecheck.type  none
    }
}

subport lua-luarocks {
    destroot.keepdirs   ${destroot}${prefix}/share/luarocks
    depends_run-append  port:lua[join [split ${lua.default_version} .] ""]-luarocks
    post-destroot {
        # delete the Lua module files since they are provided by a dependency
        delete          ${destroot}${prefix}/share/lua

        # rebuild with version specific values
        delete          ${build.dir}/build/config-${lua.default_version}.lua \
                        ${destroot}${prefix}/etc/luarocks/config-${lua.default_version}.lua

        # create config files for all Lua versions so that `luarocks --version` works properly
        foreach branch ${lua.branches} {
            lua.version ${branch} ; # this is dangerous because it is *not* correct for the rest of the build
            system      -W ${worksrcpath} \
                        "make -w install-config \
                        DESTDIR=${destroot} \
                        LUA_VERSION=${branch} \
                        LUA_BINDIR=[file dirname ${lua.bin}] \
                        LUA_DIR=${lua.dir} \
                        LUA_INCDIR=${lua.dir}/include \
                        LUA_LIBDIR=${lua.dir}/lib \
                        LUA_INTERPRETER=[file tail ${lua.bin}]"

            # this is for compatibly only
            # once all ports have been successfully moved to the new structure, this can be removed
            foreach bin {luarocks luarocks-admin} {
                ln -s   ${prefix}/bin/${bin} \
                        ${destroot}${prefix}/bin/${bin}-${branch}
            }
        }
    }
}

configure.args      --with-lua-bin=[file dirname ${lua.bin}] \
                    --with-lua=${lua.dir} \
                    --with-lua-include=${lua.dir}/include \
                    --with-lua-lib=${lua.dir}/lib \
                    --with-lua-interpreter=[file tail ${lua.bin}] \
                    --rocks-tree=${prefix}/share/luarocks