# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           makefile 1.0
PortGroup           openssl 1.0
PortGroup           sourcehut 1.0

sourcehut.setup     bptato chawan 0.4.4 v
revision            0
categories          www
license             Unlicense
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

description         TUI web browser
long_description    {*}${description} with CSS, inline image and JavaScript support.

checksums           rmd160  e39fbe67e98dc87ea28ed13b3927b58cb9700330 \
                    sha256  e0a06e1504e10a51c6009751d79b798c98d8274e559fe195d4b4b7ddadf91bb8 \
                    size    3698813

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

depends_lib-append  port:brotli \
                    port:curl \
                    port:libssh2 \
                    port:ncurses

# Match Nim port here.
# This is an experimental version. Hardcoding compiler is suboptimal, it will be changed.
compiler.c_standard 2011
# Use either modern gcc or LLVM clang.
compiler.blacklist-append \
                    {clang}

set compilertype        gcc
if {(${os.platform} eq "darwin" && ${os.arch} ne "powerpc") || ${os.platform} eq "freebsd"} {
    set compilertype    clang
}

set compilerexe [
    string range ${configure.cc} [
        string length ${prefix}/bin/
    ] end
]

# https://codeberg.org/bptato/chawan/issues/28
# As of 0.4.3, passing FLAGS_FOR_BUILD does not solve the issue.
patchfiles-append   patch-fix-Makefile.diff
post-patch {
    reinplace "s|@CC@|${compilerexe}|g" ${worksrcpath}/Makefile
    reinplace "s|@COMP@|${compilertype}|g" ${worksrcpath}/Makefile
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/Makefile
}

build.cmd-prepend   CC=${configure.cc} \
                    NIM=${prefix}/bin/nim \
                    FLAGS="--cc:${compilertype} --${compilertype}.path=${prefix}/bin --${compilertype}.exe=${compilerexe} --${compilertype}.linkerexe=${compilerexe}" \
                    FLAGS_FOR_BUILD="--cc:${compilertype} --${compilertype}.path=${prefix}/bin --${compilertype}.exe=${compilerexe} --${compilertype}.linkerexe=${compilerexe}" \
                    CFLAGS="${configure.cflags} -fno-strict-aliasing"

test.run            yes
test.target         test
test.cmd-prepend    CC=${configure.cc} \
                    NIM=${prefix}/bin/nim \
                    FLAGS="--cc:${compilertype} --${compilertype}.path=${prefix}/bin --${compilertype}.exe=${compilerexe} --${compilertype}.linkerexe=${compilerexe} --exceptions:quirky" \
                    FLAGS_FOR_BUILD="--cc:${compilertype} --${compilertype}.path=${prefix}/bin --${compilertype}.exe=${compilerexe} --${compilertype}.linkerexe=${compilerexe} --exceptions:quirky" \
                    CFLAGS="${configure.cflags} -fno-strict-aliasing"

notes "
It is recommended to use a modern terminal emulator with ${name},\
on legacy macOS you may install contour or mlterm(-minimal) port.
Consult with documentation re config options, such as images display, JS etc.
https://chawan.net/doc/cha/config.html
You may place your config file in ~/.${name}/config.toml
"
