# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1

# For now only for wrappers:
# pnana(22787) malloc: *** error for object 0x25d9aa8: pointer being freed was not allocated
legacysupport.newest_darwin_requires_legacy 0

name                pnana
epoch               2
categories          editors
license             MIT
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         PNANA is a modern terminal text editor
long_description    {*}${description} built with FTXUI

subport ${name}-devel {}

github.tarball_from archive

# Devel port exists here only for testing, it may not be updated regularly,
# and its usage is generally discouraged.
if {${subport} eq "${name}-devel"} {
    github.setup    Cyxuan0311 PNANA 7168cffe6b3496cb3e0de8c9623347dc5cd11e81
    version         0.0.5-20260312
    revision        1
    checksums       rmd160  ad422853f155e9bf40041881dd250525c083fa35 \
                    sha256  bc8b0925715bcfab7debfa62c1777d93bca3119b045beebb06838083b4a5ae66 \
                    size    22517134
    conflicts       ${name}
    github.livecheck.branch devel
} else {
    # Stable release
    github.setup    Cyxuan0311 PNANA 0.0.6 v
    revision        1
    checksums       rmd160  0eae76d7460798fef82c78db54cf81a5e6d822cb \
                    sha256  e5b1dddaa65e3948c548bccdd1308cf72d8d5d94f420c721dd2100f0ffd2506a \
                    size    17244425
    conflicts       ${name}-devel
}

depends_build-append \
                    path:bin/pkg-config:pkgconfig

# At the moment a bundled copy of nlohmann-json is used.
# https://github.com/Cyxuan0311/PNANA/commit/27c4489
# depends_build-append \
                    port:nlohmann-json

# Currently it uses static libs, so FTXUI can be made a build dependency.
# However, that is potentially fragile, so keep as a lib one.
depends_lib-append  port:curl \
                    port:FTXUI

compiler.cxx_standard   2017

configure.args-append \
        -DBUILD_AI_CLIENT=OFF \
        -DBUILD_GO=OFF \
        -DBUILD_IMAGE_PREVIEW=OFF \
        -DBUILD_LIBVTERM=OFF \
        -DBUILD_LUA=OFF \
        -DBUILD_SSH_MODE="NONE" \
        -DBUILD_TREE_SITTER=OFF

if {${os.platform} eq "darwin" && ${subport} ne "${name}-standalone"} {
    depends_lib-append \
                    port:libiconv
    configure.args-append \
        -DICONV_INCLUDE_DIR=${prefix}/include \
        -DICONV_LIBRARY=${prefix}/lib/libiconv.dylib
}

# TODO: should be fixed by now in upstream.
# https://github.com/Cyxuan0311/PNANA/issues/15
if {${build_arch} eq "riscv64" && [string match *gcc* ${configure.compiler}]} {
    configure.ldflags-append \
        -latomic
}

variant img description "Enable image previews via chafa" {
    depends_lib-append \
        port:chafa

    configure.args-replace \
        -DBUILD_IMAGE_PREVIEW=OFF -DBUILD_IMAGE_PREVIEW=ON
}

variant ai description "Enable AI client" {
    configure.args-replace \
        -DBUILD_AI_CLIENT=OFF -DBUILD_AI_CLIENT=ON
}

variant lua description "Enable Lua" {
    depends_lib-append \
        port:lua54

    configure.args-replace \
        -DBUILD_LUA=OFF -DBUILD_LUA=ON
}

variant ssh description "Enable ssh mode" {
    depends_lib-append \
        port:libssh2

    configure.args-replace \
        -DBUILD_SSH_MODE="NONE" -DBUILD_SSH_MODE="CPP"
}

variant treesitter description "Build with tree-sitter support" {
    depends_lib-append \
        port:tree-sitter \
        port:tree-sitter-bash \
        port:tree-sitter-c \
        port:tree-sitter-cmake \
        port:tree-sitter-commonlisp \
        port:tree-sitter-cpp \
        port:tree-sitter-css \
        port:tree-sitter-dockerfile \
        port:tree-sitter-fortran \
        port:tree-sitter-go \
        port:tree-sitter-haskell \
        port:tree-sitter-java \
        port:tree-sitter-javascript \
        port:tree-sitter-json \
        port:tree-sitter-llvm \
        port:tree-sitter-lua \
        port:tree-sitter-markdown \
        port:tree-sitter-meson \
        port:tree-sitter-nim \
        port:tree-sitter-perl \
        port:tree-sitter-php \
        port:tree-sitter-python \
        port:tree-sitter-r \
        port:tree-sitter-riscv \
        port:tree-sitter-ruby \
        port:tree-sitter-rust \
        port:tree-sitter-scala \
        port:tree-sitter-sml \
        port:tree-sitter-tcl \
        port:tree-sitter-toml \
        port:tree-sitter-typescript \
        port:tree-sitter-vim \
        port:tree-sitter-xml \
        port:tree-sitter-yaml

    configure.args-replace \
        -DBUILD_TREE_SITTER=OFF -DBUILD_TREE_SITTER=ON
}

variant vterm description "Enable libvterm for full terminal emulation" {
    depends_lib-append \
        port:libvterm

    configure.args-replace \
        -DBUILD_LIBVTERM=OFF -DBUILD_LIBVTERM=ON
}

# Lua fails to compile on riscv64. Tree-sitter will work, but our portgroup is Apple-only
# at the moment: https://github.com/RJVB/lnxports/issues/34
if {${subport} ne "${name}-standalone" && (${build_arch} ne "riscv64")} {
    default_variants-append \
        +lua +treesitter
}

# Enable extra variants for -devel:
if {${subport} eq "${name}-devel"} {
    default_variants-append \
        +ai +img
}

if {${subport} ne "${name}-standalone"} {
    legacysupport.redirect_bins pnana
}

# Not everything works correctly in Apple Terminal:
# https://github.com/Cyxuan0311/PNANA/issues/3
# https://github.com/Cyxuan0311/PNANA/issues/7
notes-append "
It is recommended to avoid Apple Terminal and instead use\
contour, iTerm2, mlterm(-minimal) or MacTerm.
"

# This subport is meant for building a zero-dependency executable which can be
# installed without PPCPorts, in a drag & drop manner. We do not want it to be
# installable directly as a port. If you want to use it, run `sudo port build`
# and pick the binary from the workdir.
subport ${name}-standalone {
    installs_libs   no
    configure.compiler.add_deps \
                    no
    configure.ldflags-append \
                    -static-libstdc++ -static-libgcc

    macosx_deployment_target 10.5

    # Prevent direct installation:
    known_fail yes
    pre-activate {
        ui_error "Do not install this port directly."
        ui_error "It is used to build the installer."
        error "Installation aborted"
    }
}
