# -*- 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           github 1.0
PortGroup           python 1.0

# TODO: Test port on the macOS 10.12 - macOS 10.15. Because kitty replaces the
# librsync with libxxhash, new versions can work
# correctly again.
github.setup        kovidgoyal kitty 0.32.0 v
github.tarball_from releases
revision            0

categories          aqua
platforms           macosx
license             GPL-3
maintainers         {judaew @judaew} openmaintainer

description         A cross-platform, fast, feature full, GPU based terminal emulator
long_description    ${description}
homepage            https://sw.kovidgoyal.net/kitty/

use_xz              yes

checksums           ${distfiles} \
                    rmd160  0ae4fd998fa67c75ac541a27165113c16e8f7a2e \
                    sha256  7881a95c1a43d03b230b6ff817e4b5cfec267bac3dbd6dcbbf6c095d476d776d \
                    size    8122660

if {${os.major} <= 15} {
    # See https://github.com/kovidgoyal/kitty/issues/2700
    known_fail      yes
    pre-fetch {
        ui_error "${name} @${version} requires macOS 10.12 or later."
        return -code error "incompatible OS X version"
    }
} elseif {${os.major} <= 19} {
    # See https://github.com/kovidgoyal/kitty/issues/5556
    github.setup        kovidgoyal kitty 0.26.2 v
    epoch               1

    checksums           rmd160  61bd4525e03026b834b417c6ca6920d7ae78b526 \
                        sha256  586ff599c5a3f31831b2a32dd9faafacb6c3581f2561f8ddcab0ba815bc7dab4 \
                        size    4716932
}

patchfiles-append   patch-kitty-no-deprecated-declarations.diff

python.default_version \
                    311

depends_build-append \
                    path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                    port:go \
                    port:ncurses \
                    port:optipng \
                    port:pkgconfig

depends_lib-append  \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    port:lcms2 \
                    port:libpng \
                    port:zlib \
                    port:xxhashlib

build.cmd           "${python.bin} setup.py"
build.target        kitty.app
build.args-append   --ignore-compiler-warnings \
                    --update-check-interval=0 --verbose

compiler.c_standard 2011

pre-build {
    system -W ${worksrcpath} "${python.bin} logo/make.py"
}

destroot {
    copy ${worksrcpath}/kitty.app ${destroot}${applications_dir}

    # Add terminfo to $prefix for is handled correctly when executing
    # commands with sudo.
    set terminfo_path ${prefix}/share/terminfo/78
    xinstall -d ${destroot}${terminfo_path}
    xinstall -m 0644 ${worksrcpath}/terminfo/x/xterm-kitty \
        ${destroot}${terminfo_path}

    # add shell completion files
    set shell-integration \
        ${applications_dir}/kitty.app/Contents/Resources/kitty/shell-integration

    set bash_complete ${prefix}/share/bash-completion/completions
    xinstall -d ${destroot}${bash_complete}
    ln -s ${shell-integration}/bash/kitty.bash \
        ${destroot}${bash_complete}/kitty

    set zsh_complete ${prefix}/share/zsh/site-functions
    xinstall -d ${destroot}${zsh_complete}
    ln -s ${shell-integration}/zsh/completions/_kitty \
        ${destroot}${zsh_complete}/_kitty

    set fish_complete ${prefix}/share/fish/vendor_completions.d
    xinstall -d ${destroot}${fish_complete}
    ln -s ${shell-integration}/fish/vendor_completions.d/kitty.fish \
        ${destroot}${fish_complete}/kitty.fish
    ln -s ${shell-integration}/fish/vendor_completions.d/kitten.fish \
        ${destroot}${fish_complete}/kitten.fish
}

# SSL error on running "kitty +kitten themes"
#
# See https://trac.macports.org/ticket/64604 and https://lists.macports.org/pipermail/macports-dev/2022-February/044129.html
notes "
Optional ports:

- ImageMagick port to use the \"kitty +kitten icat\" tool to display
images in the terminal
- py${python.version}-pygments port for syntax highlighting in \"kitty +kitten diff\"
- curl-ca-bundle or certsync port to use \"kitty +kitten themes\" tool to
display themes in the terminal

Since 0.24.0 kitty has a shell integration that writes extra lines to
enable it in your init shell file. See
https://sw.kovidgoyal.net/kitty/shell-integration/#shell-integration
"
