# -*- 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.43.1 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  2bfd8658b12a545c391c245ef50ac273d35f4934 \
                    sha256  44a875e34e6a5f9b8f599b25b0796c07a1506fec2b2310573e03077ef1ae159f \
                    size    12756968

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
patchfiles-append   patch-kitty-incorrect-prefix-in-arm-neon-vshrn-intrinsic.diff

python.default_version \
                    313

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

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

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 {
    ln -s ${prefix}/share/fonts/ttf-nerd-fonts-symbols/ \
        ${worksrcpath}/fonts
    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
}

post-destroot {
    platform darwin {
        if {${os.major} >= 22} {
            system -W ${destroot}${applications_dir} "/usr/bin/codesign --force -s - kitty.app/Contents/MacOS/kitten"
            system -W ${destroot}${applications_dir} "/usr/bin/codesign --force -s - kitty.app/Contents/kitty-quick-access.app/Contents/MacOS/kitty-quick-access"
            system -W ${destroot}${applications_dir} "/usr/bin/codesign --force -s - kitty.app/Contents/MacOS/kitty"
            system -W ${destroot}${applications_dir} "/usr/bin/codesign --force -s - kitty.app"
        }
    }
}

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