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

github.setup        mathworks MATLAB-language-server 1.3.13 v
name                [string tolower ${github.project}]
revision            0
categories          lang
platforms           any
maintainers         nomaintainer
license             MIT
supported_archs     noarch
github.tarball_from archive

description         Language Server Protocol (LSP) implementation for MATLAB
long_description    matlab-language-server is an implementation of the Language Server Protocol (LSP) \
                    for MATLAB, maintained by MathWorks. It provides language support \
                    features such as code completion, syntax diagnostics, code formatting, \
                    and symbol navigation for MATLAB code in LSP-compatible editors.

checksums           rmd160  cd23e055690ff98765db75dfd7794156fb093e9d \
                    sha256  ae751c1cbedba62bf395c3a07f9a964706afc39a76046fb733ffaf2a275081ea \
                    size    987929

depends_build-append \
                    path:bin/npm:npm11

depends_lib-append  path:bin/node:nodejs24

configure {
    system -W ${worksrcpath} "npm ci"
    system -W ${worksrcpath}/src/licensing/gui "npm ci"
}

build {
    system -W ${worksrcpath} "npm run compile"
}

destroot {
    system -W ${worksrcpath} "npm run package"

    # webpack's build only needs devDependencies (webpack, ts-loader,
    # eslint, ...); prune them now, in place, so what's left in
    # node_modules is just the runtime deps (chokidar and its own
    # transitive deps) that webpack's `externals` left unbundled.
    # npm prune only removes already-installed packages, so this
    # needs no network access.
    system -W ${worksrcpath} "npm prune --omit=dev"

    set libexecdir ${destroot}${prefix}/libexec/matlab-ls
    xinstall -m 0755 -d ${libexecdir}

    # matlab/ must stay a sibling of out/: MatlabSession.ts computes
    # fullfile(__dirname, '..', 'matlab') at runtime, where __dirname
    # is out/'s own installed location.
    copy ${worksrcpath}/out          ${libexecdir}/out
    copy ${worksrcpath}/matlab       ${libexecdir}/matlab
    copy ${worksrcpath}/node_modules ${libexecdir}/node_modules
    xinstall -m 0644 ${worksrcpath}/LICENSE ${libexecdir}/LICENSE

    # out/index.js is a plain webpack bundle with no shebang, so it
    # can't just be chmod +x'd — wrap it instead.
    xinstall -m 0755 -d ${destroot}${prefix}/bin
    set launcher [open ${destroot}${prefix}/bin/matlab-language-server w]
    puts ${launcher} "#!/bin/sh"
    puts ${launcher} "exec \"${prefix}/bin/node\" \"${prefix}/libexec/matlab-ls/out/index.js\" \"\$@\""
    close ${launcher}
    file attributes ${destroot}${prefix}/bin/matlab-language-server -permissions 0755
}
