# -*- 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           codeberg 1.0
PortGroup           legacysupport 1.1
PortGroup           meson 1.0

legacysupport.newest_darwin_requires_legacy 10

codeberg.setup      dnkl foot 1.27.0
revision            1
categories          sysutils wayland
license             GPL-3
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer

description         Fast, lightweight and minimalistic Wayland terminal emulator
long_description    {*}${description}

set fcft_hash       43da4da7c8136e2fcecb9310693ef2d5d16fb453
set tllist_hash     05b463da2ee4a81903126756689282f99f88cc30

master_sites-append https://codeberg.org/dnkl/fcft/archive/:fcft \
                    https://codeberg.org/dnkl/tllist/archive/:tllist
distfiles-append    ${fcft_hash}${extract.suffix}:fcft \
                    ${tllist_hash}${extract.suffix}:tllist

checksums           ${distname}${extract.suffix} \
                    rmd160  c58eedcaeca636115c6fff298acb2ad3164e4f15 \
                    sha256  f5917cad2d7b723b99873e53d78fd10ea202923d189aed5086591fc53b70b7e3 \
                    size    1194144 \
                    ${fcft_hash}${extract.suffix} \
                    rmd160  50909f5024f0c89eb4906dd5e2b8e32a59f2749f \
                    sha256  82dd9355ef7f7166dc18dc3a174e233e732050f25ec0675ee213a77d02a0f7ec \
                    size    741843 \
                    ${tllist_hash}${extract.suffix} \
                    rmd160  5671d33a61f363401fa54128caa49d7ccfee07d2 \
                    sha256  4dbd4e11c0015b79935509360ddb68db10ab5bf2627537c86aebbbeaa2344e3a \
                    size    7507

meson.wrap_mode     nodownload

extract.only        ${distname}${extract.suffix}

post-extract {
    set tar [findBinary tar ${portutil::autoconf::tar_command}]

    file mkdir ${workpath}/fcft-extract
    system -W ${workpath}/fcft-extract "${tar} -zxf ${distpath}/${fcft_hash}${extract.suffix}"
    move ${workpath}/fcft-extract/fcft ${worksrcpath}/subprojects/fcft

    file mkdir ${workpath}/tllist-extract
    system -W ${workpath}/tllist-extract "${tar} -zxf ${distpath}/${tllist_hash}${extract.suffix}"
    move ${workpath}/tllist-extract/tllist ${worksrcpath}/subprojects/tllist
}

compiler.c_standard 2023

set py_ver          3.14
set py_ver_nodot    [string map {. {}} ${py_ver}]

configure.python        ${prefix}/bin/python${py_ver}
meson.native.binaries   python3=${configure.python}

# Upstream not interested to fix anything:
# https://codeberg.org/dnkl/foot/issues/2211

patch.pre_args-replace  -p0 -p1
patchfiles-append       0001-build-use-sed-E-instead-of-r-in-generate-version.sh.patch \
                        0002-build-exempt-Apple-from-__STDC_UTF_32__-__STDC_ISO_1.patch \
                        0003-xmalloc-avoid-using-reallocarray-3-to-implement-xrea.patch \
                        0004-build-provide-portable-BTN_-KEY_-fallbacks-for-non-L.patch \
                        0005-build-real-macOS-pipe2-sem_init-replacements-no-comp.patch \
                        0006-fdm-use-NSIG-instead-of-SIGRTMAX-for-signal-array-si.patch \
                        0007-main-use-NSIG-instead-of-SIGRTMAX-in-sanitize_signal.patch \
                        0008-render-handle-Darwin-s-single-argument-pthread_setna.patch \
                        0009-server-replace-accept4-SOCK_CLOEXEC-SOCK_NONBLOCK-SO.patch \
                        0010-client-use-SO_NOSIGPIPE-instead-of-MSG_NOSIGNAL-on-D.patch \
                        0011-build-replace-bool-N-bitfields-with-uint8_t-N-across.patch \
                        0012-util-add-mkostemp-polyfill-for-Darwin.patch \
                        0013-terminal-open-PTY-slave-once-before-first-master-ioc.patch \
                        0014-util-treat-zero-O_CLOEXEC-as-always-requested-in-pip.patch
# fcft
patchfiles-append       1001-build-avoid-glibc-only-byteswap.h-for-the-COLR-versi.patch \
                        1002-build-expose-Darwin-locale_t-newlocale-uselocale-fre.patch \
                        1003-build-exempt-Apple-from-__STDC_UTF_32__-check-in-tes.patch

post-patch {
    reinplace "s|\'python3\'|\'${configure.python}\'|" ${worksrcpath}/meson.build
}

# Yes, ncurses is a build-time dependency:
depends_build-append \
                    port:gettext \
                    port:ncurses \
                    path:bin/pkg-config:pkgconfig \
                    port:python${py_ver_nodot} \
                    port:wayland-protocols

depends_lib-append  path:lib/pkgconfig/fontconfig.pc:fontconfig \
                    port:gettext-runtime \
                    port:libpixman \
                    port:libxkbcommon \
                    port:wayland

depends_run-append  port:owl

# Apple's libSystem has never shipped the C11 <threads.h> (thrd_*,
# mtx_*, cnd_*) or <uchar.h> (char32_t, mbrtoc32, c32rtomb) headers,
# on any macOS version foot targets. foot's own meson.build already
# anticipates the former via `cc.find_library('stdthreads', required:
# false)` (the name used by FreeBSD's pthreads-backed C11-threads
# library); we satisfy that probe here with our own minimal
# Darwin-native shim, since FreeBSD's implementation assumes
# pthread_mutex_t/pthread_cond_t are themselves opaque pointers, which
# is not true on Darwin. See files/darwin-c11-threads.{c,h} and
# files/darwin-uchar.{c,h} for the (deliberately partial, matching
# only what foot actually calls) implementations.
set darwin_shim_dir     ${workpath}/darwin-shim
set darwin_shim_incdir  ${darwin_shim_dir}/include
set darwin_shim_libdir  ${darwin_shim_dir}/lib

pre-configure {
    file mkdir ${darwin_shim_incdir}
    file mkdir ${darwin_shim_libdir}

    file copy -force ${filespath}/darwin-c11-threads.h ${darwin_shim_incdir}/threads.h
    file copy -force ${filespath}/darwin-uchar.h ${darwin_shim_incdir}/uchar.h

    system -W ${darwin_shim_dir} \
        "${configure.cc} ${configure.cflags} -I${darwin_shim_incdir} -c ${filespath}/darwin-c11-threads.c -o darwin-c11-threads.o"
    system -W ${darwin_shim_dir} \
        "${configure.cc} ${configure.cflags} -I${darwin_shim_incdir} -c ${filespath}/darwin-uchar.c -o darwin-uchar.o"
    system -W ${darwin_shim_dir} \
        "ar rcs ${darwin_shim_libdir}/libstdthreads.a darwin-c11-threads.o darwin-uchar.o"
}

configure.cppflags-prepend -I${darwin_shim_incdir}
configure.ldflags-append   -L${darwin_shim_libdir}

configure.args-append \
                    -Dcustom-terminfo-install-location=share/foot/terminfo \
                    -Ddocs=disabled \
                    -Dime=false \
                    -Dgrapheme-clustering=disabled \
                    -Dterminfo=disabled \
                    -Dtests=false \
                    -Dthemes=true \
                    -Dutmp-backend=none

# -Dterminfo stays disabled: foot's meson declares tic's output as
# the letter directory 'f', but tic on case-insensitive filesystems (macOS)
# writes hex directory names ('66'), so `meson install` cannot find it.
post-destroot {
    system -W ${worksrcpath} "sed -e 's|@default_terminfo@|foot|g' \
        -e 's|pairs#0x10000|pairs#32767|' foot.info > ${workpath}/foot.info.preprocessed"
    xinstall -d ${destroot}${prefix}/share/foot/terminfo
    system "${prefix}/bin/tic -x -o ${destroot}${prefix}/share/foot/terminfo \
        -e foot,foot-direct ${workpath}/foot.info.preprocessed"
}
