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

github.setup        LuaLS lua-language-server 3.17.1
github.tarball_from releases
revision            0

categories          lua devel
license             MIT
maintainers         {judaew @judaew} openmaintainer

description         A language server that offers Lua language support
long_description    {*}${description}

use_zip             yes
extract.mkdir       yes
distname            ${name}-${version}-submodules

checksums           rmd160  9a1c4c01e4b86d9da883a688420bc8d996d62005 \
                    sha256  76b1b04ce582a26a4d7e3db0b8e55545e3fce202e5afb6be3141804cdcb3cc18 \
                    size    32018139

compiler.c_standard 2011
compiler.cxx_standard 2017

configure {
    # see https://trac.macports.org/wiki/UsingTheRightCompiler
    set makefile    [open "${worksrcpath}/make.lua" "a"]
    puts $makefile  ""
    puts $makefile  "lm.cc = '${configure.cc}'"
    close $makefile
}

pre-build {
    copy ${filespath}/wrapper ${worksrcpath}
    reinplace "s,@@PREFIX@@,${prefix},g" ${worksrcpath}/wrapper
}

# FIXME: Using luamake from source tree instead of system package
#
# Normally, we would declare luamake as a build dependency:
#   depends_build    port:luamake
#   build.cmd       ${prefix}/bin/luamake rebuild
#
# However, the lua-language-server project uses a specific, unversioned
# development snapshot of luamake bundled in its 3rd/ directory.
# The system-installed luamake package (which is versioned) is incompatible
# with the build system expectations.
build {
    system -W ${worksrcpath}/3rd/luamake "./compile/build.sh"
    system -W ${worksrcpath} "3rd/luamake/luamake rebuild"
}

destroot {
    xinstall -m 0755 ${worksrcpath}/wrapper ${destroot}${prefix}/bin/${name}

    xinstall -d ${destroot}${prefix}/lib/${name}
    move ${worksrcpath}/bin ${destroot}${prefix}/lib/${name}

    move \
        ${worksrcpath}/debugger.lua \
        ${worksrcpath}/main.lua \
        ${worksrcpath}/locale \
        ${worksrcpath}/meta \
        ${worksrcpath}/script \
        ${destroot}${prefix}/lib/${name}
}

notes "
    To use in Neovim, it is sufficient to specify cmd in lspconfig:

        require'lspconfig'.lua_ls.setup \{
            cmd = \{\"${prefix}/bin/${name}\"\};
        \}

    See more https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#sumneko_lua
"
