# Maintainer: FWS <maintainers@nocode-var.tech>
#
# Arch package for the FWS kitty fork. Built with makepkg from this directory:
#
#     cd packaging && makepkg -f
#
# The resulting fws-kitty-<version>-<rel>-<arch>.pkg.tar.zst is ready to be
# added to a pacman repository (see README.md in this directory).
#
# kitty installs its own binary under /usr/bin and cannot coexist with the
# upstream package, hence provides/conflicts.

pkgname=fws-kitty
# Fork version from the version file: it names the release and the package.
# The upstream kitty release built inside is tracked by _kittyver below.
pkgver=0.1.16
pkgrel=1
pkgdesc='Fast, feature-rich, GPU based terminal (FWS fork with the file-manager kitten)'
arch=('x86_64' 'aarch64')
url='https://gitea.nocode-var.tech/FWS/FWS-Kitty'
license=('GPL-3.0-only')

# Upstream kitty release shipped by this package. It is independent from pkgver
# and must match kitty/constants.py (checked by check-version.sh).
_kittyver=0.50.0

# Replace main with the tag or commit to ship once the release is cut.
_branch=main
_repo='https://gitea.nocode-var.tech/FWS/FWS-Kitty.git'
# The CI sets FWS_SOURCE_REF=commit=<sha> so the package is built from the exact
# commit being released instead of whatever main points at by the time makepkg
# clones. A local build keeps following _branch.
_source_ref=${FWS_SOURCE_REF:-branch=$_branch}

depends=(
    python harfbuzz zlib libpng lcms2 xxhash openssl
    pixman cairo freetype2 fontconfig
    libx11 libxcb libxrandr libxinerama libxcursor libxi libxrender
    libxkbcommon libxkbcommon-x11 mesa wayland dbus
    libcanberra systemd-libs
    # Arch ships these files in separate packages; shipping them here would
    # make pacman abort on file conflicts with the installed split packages.
    kitty-shell-integration kitty-terminfo
)
makedepends=(
    git go python pkgconf ncurses rsync which file simde shader-slang
)
optdepends=(
    'imagemagick: display uncommon image formats'
    'shader-slang: custom shaders'
)
provides=('kitty')
conflicts=('kitty')

# The release publishes only the main package, so the separate debug package is
# built and thrown away. Skipping it also avoids makepkg running gdb-add-index
# over binaries that were already stripped, which only prints errors.
options=('!debug')

source=(
    "$pkgname::git+$_repo#$_source_ref"
    "nerdfonts-symbols.tar.xz::https://github.com/ryanoasis/nerd-fonts/releases/download/v3.5.1/NerdFontsSymbolsOnly.tar.xz"
)
sha256sums=('SKIP' '01172f37db8543edb102e5cb5c64101c9f4686630804d49b419aa07b23a69996')

prepare() {
    # kitty embeds the mono variant of the Nerd font at build time, but Arch
    # only packages the proportional one, so ship the font with the build.
    mkdir -p "$pkgname/fonts"
    cp "$srcdir/SymbolsNerdFontMono-Regular.ttf" "$pkgname/fonts/"

    cd "$pkgname"
    # linux-package regenerates the man pages and the HTML docs, and some of
    # their dependencies (furo, sphinx-design) are not packaged for Arch, so
    # keep a local throwaway environment instead of requiring the system ones.
    # Do not reuse the system packages: pip would then skip installing sphinx
    # itself and the venv would have no sphinx-build entry point.
    python -m venv .docs-venv
    .docs-venv/bin/pip install --disable-pip-version-check -q -r docs/requirements.txt
}

package() {
    cd "$pkgname"
    # kitty's linux-package target stages a full self-contained install, so
    # pointing --prefix at the package root is enough; no manual file copying.
    PATH="$srcdir/$pkgname/.docs-venv/bin:$PATH" python setup.py linux-package --prefix "$pkgdir/usr"
    # These paths are owned by kitty-shell-integration and kitty-terminfo on
    # Arch. Remove them here so upgrading from upstream kitty does not fail
    # with pacman file conflicts; the dependencies above provide them.
    rm -rf "$pkgdir/usr/lib/kitty/shell-integration"
    rm -f "$pkgdir/usr/share/terminfo/x/xterm-kitty"
    rmdir --ignore-fail-on-non-empty "$pkgdir/usr/share/terminfo/x" "$pkgdir/usr/share/terminfo" 2>/dev/null || true
    install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
