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

if {${subport} eq "${name}-devel"} {
    github.setup    Cyxuan0311 PNANA a225f8a3b3af6c5ce1526fa8fc604f9f9852ce43
    version         0.0.4-20260129
    revision        0
    checksums       rmd160  4bec9f7e89af06fad43b2f999dbf3bea93bfa0fb \
                    sha256  456fb7e177b05aa530aff6195d36e6339f8459edff67b4334f3877712fdf799c \
                    size    20685474
    conflicts       ${name}
    github.livecheck.branch devel

    depends_lib-append \
        port:curl \
        port:tree-sitter-commonlisp \
        port:tree-sitter-llvm \
        port:tree-sitter-riscv \
        port:tree-sitter-sml
} else {
    # Stable release
    github.setup    Cyxuan0311 PNANA 0.0.4 v
    revision        3
    checksums       rmd160  750cf70b7a8493cb58a07108e44c278a61783cac \
                    sha256  c26074ade81e515f949432e81d22753fed114efe70e0edd906c4d143d651a7af \
                    size    10747925
    conflicts       ${name}-devel

    # https://github.com/Cyxuan0311/PNANA/issues/16
    post-destroot {
        delete -f ${destroot}${prefix}/install.sh
    }
}

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:FTXUI

compiler.cxx_standard   2017

configure.args-append \
        -DBUILD_GO=OFF \
        -DBUILD_IMAGE_PREVIEW=OFF \
        -DBUILD_LUA=OFF \
        -DBUILD_TREE_SITTER=OFF

# 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 FFMpeg" {
    depends_lib-append \
        path:lib/libavcodec.dylib:ffmpeg

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

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

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

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-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-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-ruby \
        port:tree-sitter-rust \
        port:tree-sitter-scala \
        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
}

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

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