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

# O_CLOEXEC
legacysupport.newest_darwin_requires_legacy 10

# 2026.08.06
github.setup        contour-terminal contour ca44f8de9cc8fad68c334d87837818a23e3c5faf
version             0.7.0-20260806
revision            0
categories          aqua sysutils
platforms           macosx
license             Apache-2
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         ${name} – a modern & actually fast Terminal Emulator
long_description    {*}${description} with native macOS GUI and support for multiple image protocols.
checksums           rmd160  e81e40c5ef9a6bf39ee31fa5dba865b4187df06b \
                    sha256  6eafaa42975eaf11f7c7868b1bb3a4160549632d79942d1e829af283d0cf10dc \
                    size    15724109
github.tarball_from archive

installs_libs       no

# hfs_format.h:765:9: error: 'uuid_string_t' does not name a type; did you mean 'io_string_t'?
conflicts_build     libuuid

depends_build-append \
                    port:boxed-cpp \
                    port:gslpp \
                    path:bin/pkg-config:pkgconfig \
                    port:reflection-cpp

depends_lib-append  path:lib/pkgconfig/cairo.pc:cairo \
                    path:lib/pkgconfig/freetype2.pc:freetype \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    port:libssh2 \
                    port:libunicode \
                    port:yaml-cpp

depends_test-append port:catch2

# aligned_alloc issue: https://github.com/contour-terminal/contour/issues/2005

patch.pre_args-replace  -p0 -p1

# Frontend patches (our own code: src/contour_macos/**, docs/macos-*.md; base 52cab92a,
# does not track upstream -- never needs rebasing).
patchfiles-append   0001-build-macos-add-CONTOUR_FRONTEND_MACOS-option-and-Qt.patch \
                    0002-feat-macos-native-frontend-skeleton-and-design-doc.patch \
                    0003-feat-macos-CPU-software-render-backend-headless-rend.patch \
                    0004-fix-macos-name-a-real-monospace-family-in-the-render.patch \
                    0005-fix-macos-drop-Windows-only-wFamilyName-guard-Wno-c2.patch \
                    0006-debug-macos-print-CoreText-locator-output-in-the-ren.patch \
                    0007-fix-macos-give-the-render-probe-a-page-margin-so-col.patch \
                    0008-fix-macos-force-monospace-font-spacing-in-the-render.patch \
                    0009-debug-macos-probe-build-fingerprint-first-row-cell-d.patch \
                    0010-debug-macos-log-every-renderTile-to-stderr-enabled-i.patch \
                    0011-debug-macos-dump-the-texture-atlas-from-the-probe-re.patch \
                    0012-debug-macos-log-uploadTile-locations-and-source-ink-.patch \
                    0013-debug-macos-log-atlas-sample-tint-alpha-per-blit-rev.patch \
                    0014-debug-macos-print-cursor-position-in-the-probe-rev-8.patch \
                    0015-debug-macos-hide-cursor-in-the-probe-to-isolate-the-.patch \
                    0016-fix-macos-give-the-render-probe-a-real-DPI-and-font-.patch \
                    0017-feat-macos-interactive-AppKit-frontend-session-view-.patch \
                    0018-fix-macos-declare-namespace-scope-ImageSize-alias-in.patch \
                    0019-fix-macos-isolate-engine-behind-an-opaque-bridge-so-.patch \
                    0020-fix-macos-link-ApplicationServices-not-CoreGraphics-.patch \
                    0021-fix-macos-make-the-view-first-responder-after-the-wi.patch \
                    0022-fix-macos-emit-NeXT-runtime-ObjC-fnext-runtime-for-A.patch \
                    0023-fix-macos-build-ObjC-frontend-at-C-98-with-an-option.patch \
                    0024-feat-macos-install-the-contour-binary-to-prefix-bin.patch \
                    0025-fix-macos-control-the-frontend-ObjC-std-via-a-target.patch \
                    0026-feat-macos-compile-the-frontend-.mm-with-a-separate-.patch \
                    0027-fix-macos-draw-the-frame-right-side-up-in-the-flippe.patch \
                    0028-feat-macos-standard-menu-bar-Shell-New-Close-and-Edi.patch \
                    0029-docs-macos-feature-roadmap-for-the-native-frontend.patch \
                    0030-docs-macos-roadmap-add-SSH-and-background-image-defe.patch \
                    0031-docs-macos-roadmap-window-transparency-Apple-Termina.patch \
                    0032-feat-macos-configure-summary-for-the-frontend-option.patch \
                    0033-fix-macos-pass-the-frontend-I-as-separate-tokens-gcc.patch \
                    0034-fix-macos-settle-the-cursor-no-motion-animation-foll.patch \
                    0035-fix-macos-mouse-selection-crash-drag-and-wheel-scrol.patch \
                    0036-fix-macos-set-the-engine-s-cell-pixel-size-sixel-fre.patch \
                    0037-fix-macos-flush-queued-VT-replies-after-each-parse-p.patch \
                    0038-fix-macos-scroll-aware-mouse-cell-mapping-reliable-s.patch \
                    0039-feat-macos-standard-Window-menu-open-windows-list-Mi.patch \
                    0040-feat-macos-GL-2.0-render-backend-10.5-baseline-imple.patch \
                    0041-feat-macos-select-GL-backend-at-runtime-NSOpenGL-pre.patch \
                    0042-fix-macos-make-GL-context-current-before-the-session.patch \
                    0043-feat-macos-default-window-title-to-the-working-direc.patch \
                    0044-feat-macos-show-the-render-backend-GL-in-the-window-.patch \
                    0045-fix-macos-tick-the-clock-on-mouse-events-reliable-cl.patch \
                    0046-feat-macos-color-themes-dark-light.patch \
                    0047-feat-macos-iTerm2-inline-images-frontend-PNG-decoder.patch \
                    0048-feat-macos-built-in-SSH-vtpty-SshSession-with-hostke.patch \
                    0049-fix-macos-GCC-build-errors-in-the-SSH-iTerm2-additio.patch \
                    0050-fix-macos-don-t-use-the-_rgb-literal-in-Themes-GCC-f.patch \
                    0051-fix-macos-selection-position-title-on-cd-trace-iTerm.patch \
                    0052-fix-macos-decode-any-image-format-for-inline-images-.patch \
                    0053-chore-macos-remove-image-decode-debug-traces-fronten.patch \
                    0054-fix-macos-enable-font-anti-aliasing-render-mode-was-.patch \
                    0055-debug-macos-trace-suppress-AppKit-auto-window-open-s.patch \
                    0056-fix-macos-suppress-AppKit-auto-window-open-drop-diag.patch \
                    0057-fix-macos-two-windows-at-launch-gate-the-untitled-wi.patch \
                    0058-feat-macos-bundle-icon-reuse-contour-s-.icns-for-the.patch \
                    0059-feat-macos-persistent-settings-via-NSUserDefaults-fo.patch \
                    0060-feat-macos-drag-drop-files-and-text-into-the-termina.patch \
                    0061-feat-macos-About-menu-item.patch \
                    0062-feat-macos-traditional-full-screen-View-Toggle-Full-.patch \
                    0063-feat-macos-lite-app-bundle-dock-icon-launch-separate.patch \
                    0064-feat-macos-libdispatch-codepath-for-main-thread-mars.patch \
                    0065-feat-macos-scrollbar-opt-in-right-side-classic-NSScr.patch \
                    0066-docs-macos-document-available-NSUserDefaults-setting.patch \
                    0067-fix-macos-spawn-the-local-shell-as-a-login-shell-l.patch \
                    0068-fix-macos-restore-dropped-glyphs-w-in-the-GL-2.0-bac.patch \
                    0069-feat-macos-inflate-kitty-o-z-payloads-with-system-zl.patch \
                    0070-fix-macos-adapt-frontend-to-upstream-engine-signatur.patch

# Vendor patches (upstream-owned files: src/vtbackend/, src/vtpty/, src/text_shaper/,
# src/crispy/, cmake/; these will need to be rebased.
patchfiles-append   1001-fix-vendor-portability-CoreText-groundwork-for-macOS.patch \
                    1002-fix-paste-scrolls-to-bottom.patch \
                    1003-fix-vendor-bold-italic-glyph-spacing-and-family-reso.patch \
                    1004-fix-vendor-force-std-c-23-explicitly-for-coretext_lo.patch \
                    1005-fix-vendor-decode-kitty-graphics-PNG-transmissions-b.patch \
                    1006-fix-vendor-implement-by-content-font-fallback-CJK-em.patch \
                    1007-feat-vendor-skip-recompositing-reblitting-unchanged-.patch \
                    1008-fix-vendor-verify-by-content-font-fallback-actually-.patch \
                    1009-fix-vendor-scroll-to-fit-a-kitty-image-taller-than-t.patch \
                    1010-fix-vendor-step-the-text-pen-by-cluster-not-shaping-.patch \
                    1011-feat-vendor-accept-kitty-o-z-payloads-and-evict-not-.patch \
                    1012-feat-vendor-kitty-Unicode-placeholders-U-10EEEE-virt.patch \
                    1013-fix-vendor-stop-search-match-highlighting-from-index.patch

compiler.cxx_standard   2023

configure.args-append \
                    -DCONTOUR_BUILD_NATIVE=OFF \
                    -DCONTOUR_BUILD_STATIC=OFF \
                    -DCONTOUR_BUILD_WITH_MIMALLOC=OFF \
                    -DCONTOUR_FRONTEND_MACOS=ON \
                    -DCONTOUR_MACOS_BUNDLE=OFF \
                    -DCONTOUR_TESTING=OFF \
                    -DCONTOUR_USE_CPM=OFF \
                    -DCONTOUR_USE_LIBDISPATCH=OFF

# If ObjC++ sources are build with modern gcc, the app segfaults.
# See also: https://github.com/fltk/fltk/issues/1286
# https://github.com/iains/gcc-14-branch/issues/25
if {${configure.cxx_stdlib} ne "libc++"} {
    configure.args-append \
                    -DCONTOUR_MACOS_OBJCXX_COMPILER=/usr/bin/g++-4.2
}

variant app description "Install as a macOS bundle" {
    configure.args-replace \
                    -DCONTOUR_MACOS_BUNDLE=OFF -DCONTOUR_MACOS_BUNDLE=ON
}

default_variants-append +app

if {${os.platform} eq "darwin" && ${os.major} >= 10} {
    variant dispatch description "Use libdispatch" {
        configure.args-replace \
                    -DCONTOUR_USE_LIBDISPATCH=OFF -DCONTOUR_USE_LIBDISPATCH=ON
    }
    default_variants-append +dispatch
}

variant mimalloc description "Use mimalloc" {
    depends_lib-append \
                    port:mimalloc

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

if {[variant_isset app]} {
    destroot {
        copy ${cmake.build_dir}/src/contour_macos/contour.app ${destroot}${applications_dir}/Contour.app
    }

    post-destroot {
        if {${configure.cxx_stdlib} ne "libc++"} {
            set exe ${applications_dir}/Contour.app/Contents/MacOS/${name}
            set exewrap ${prefix}/bin/${name}

            set  wrapper    [open "${destroot}${exewrap}" w 0755]
            puts ${wrapper} "#!/bin/sh"
            puts ${wrapper} ""
            puts ${wrapper} {if [ -n "$DYLD_LIBRARY_PATH" ]; then}
            puts ${wrapper} "   DYLD_LIBRARY_PATH=${prefix}/lib/libgcc:\$DYLD_LIBRARY_PATH"
            puts ${wrapper} {else}
            puts ${wrapper} "   DYLD_LIBRARY_PATH=${prefix}/lib/libgcc"
            puts ${wrapper} {fi}
            puts ${wrapper} {export DYLD_LIBRARY_PATH}
            puts ${wrapper} ""
            puts ${wrapper} "exec ${exe} \"\$@\""
            close $wrapper
        }
    }
} else {
    # contour(79667) malloc: *** error for object 0xb0aaa0: pointer being freed was not allocated
    legacysupport.redirect_bins ${name}
}

# FIXME: sixel/kitty VO from mpv has issues unrelated to this port as such, see:
# https://github.com/ismail-yilmaz/Bobcat/issues/64
# Passing --really-quiet helps.
