# -*- 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.2 v
set git-commit      860c19d
# 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  d87948018051afae52a348a5c9a9523f751a4d4e \
                    sha256  a10551b6f319d90862980c1def9aff81d6dfdc3065e4df9b3534a1203b6d897f \
                    size    5411446

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.
    "
}
