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

go.setup            github.com/filebrowser/filebrowser 2.62.1 v
set git-commit      cbcf708  
# This line is for displaying commit in CLI only
revision            0
categories          net sysutils
maintainers         {i0ntempest @i0ntempest} openmaintainer
license             Apache-2

description         Web File Browser
long_description    ${name} provides a file managing interface within a specified directory\
                    and it can be used to upload, delete, preview, rename and edit your files.\
                    It allows the creation of multiple users and each user can have its own directory.\
                    It can be used as a standalone app or as a middleware.

homepage            https://filebrowser.org

checksums           rmd160  7ffda33db055067e92ad6cf9ca93677fe13680ff \
                    sha256  b16f43a8d3f6264a611a3507455b362e75742770b796299d9710ab4638e84e6b \
                    size    5407197

go.offline_build    no

# Notes for updating this port:
# Remember to update git commit hash

depends_build-append \
                    path:bin/node:nodejs22 \
                    port:pnpm

platform darwin {
    if {${os.major} < 20} {
        # 10.15-
        depends_build-replace \
                    path:bin/node:nodejs22 \
                    path:bin/node:nodejs18
    }
}

pre-build {
    # See ${worksrcpath}/Makefile for frontend build commands
    system -W ${worksrcpath}/frontend "${prefix}/bin/pnpm --verbose install --frozen-lockfile && \
                                       ${prefix}/bin/pnpm --verbose run build"
}

build.args-append   -ldflags=\"-X 'github.com/filebrowser/filebrowser/version.Version=${version}' -X 'github.com/filebrowser/filebrowser/version.CommitSHA=${git-commit}'\"

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/
    # All supported shells: bash, fish, powershell, zsh
    set comp_path(bash) ${destroot}${prefix}/share/bash-completion/completions/${name}
    set comp_path(fish) ${destroot}${prefix}/share/fish/completions/${name}.fish
    set comp_path(zsh)  ${destroot}${prefix}/share/zsh/site-functions/_${name}
    foreach shell {bash fish zsh} {
        xinstall -d [file dirname $comp_path(${shell})]
        system -W ${worksrcpath} "./${name} completion ${shell} > $comp_path(${shell})"
    }
    file mkdir ${destroot}${prefix}/etc/filebrowser/
    copy ${filespath}/config.json ${destroot}${prefix}/etc/filebrowser/config.json.example
    reinplace "s|@@PREFIX@@|${prefix}|g" ${destroot}${prefix}/etc/filebrowser/config.json.example
}

post-activate {
    if {![file exists ${prefix}/etc/filebrowser/config.json]} {
        copy ${prefix}/etc/filebrowser/config.json.example ${prefix}/etc/filebrowser/config.json
    }
}

startupitem.create          yes
startupitem.executable      ${prefix}/bin/filebrowser --config ${prefix}/etc/filebrowser/config.json

notes-append "
    The example config file is copied to ${prefix}/etc/filebrowser/config.json and its content\
    will be preserved across upgrades and reinstalls. This config file configures filebrowser\
    to use /var/empty as root by default for safety reasons, so modify it before using. The\
    startup item will use this config file by default.\

    The default login and password for the Web UI is admin and admin. Change this as soon as\
    you can.
"

variant command_runner description {Force enable the potentially vulnerable command runner disabled in a previous version} {
    startupitem.executable-append \
                            --disable-exec=false
    notes-append            "
        You have enabled the potentially vulnerable command runner functionality in the startup script.\
        Only use this if you are aware of the risks. See https://github.com/filebrowser/filebrowser/issues/5199\
        for details.
    "
}
