# Enable or disable running the tests
%bcond_with tests

# The naming scheme in non-Mandriva-based distros
%if 0%{!?mklibname:1}
%define mklibname(ds)  lib%{1}%{?2:%{2}}%{-s:-static}%{-d:-devel}
%endif
%if 0%{!?mkrel:1}
%define mkrel(c) %{1}%{?dist}
%endif

Name:           kitty
Version:        0.40.0
Release:        %mkrel 1.3
Summary:        A cross-platform, fast, feature full, GPU based terminal emulator
Group:          Terminals
License:        GPLv3
URL:            https://sw.kovidgoyal.net/kitty/
Source0:        https://github.com/kovidgoyal/kitty/releases/download/v%{version}/%{name}-%{version}.tar.xz
# kitty.conf created by kekepower.
# Default is located in /etc/xdg/kitty/kitty.conf
Source1:        kitty.conf
# Go vendor source for kitty
Source2:        kitty-%{version}-go-vendor.tar.xz
Source3:        SymbolsNerdFontMono-Regular.ttf

Patch1:         kitty-0.27.0-extend-ldflags.patch
# Kitty 0.40.1 won't start in Wayland compositors not implementing
# xdg-toplevel-icon-v1 (which is most of them), so let's backport some patches.
Patch2:         kitty-0.40.0-fix-32-bit-build.diff
Patch3:         kitty-0.40.0-do-not-count-background-processes.diff
Patch4:         kitty-0.40.0-wire-up-lost-clipboard-events.diff
Patch5:         kitty-0.40.0-do-not-skip-empty-lines.diff
Patch6:         kitty-0.40.0-fix-osc-clipboard.diff
Patch7:         kitty-0.40.0-osc-single-clipboard-destination.diff

#
# Security patches
# P100 -> ...

BuildRequires:  pkgconfig(dbus-1)
BuildRequires:  pkgconfig(xcursor)
BuildRequires:  pkgconfig(xrandr)
BuildRequires:  pkgconfig(xi)
BuildRequires:  pkgconfig(xinerama)
BuildRequires:  pkgconfig(gl)
BuildRequires:  pkgconfig(xkbcommon-x11)
BuildRequires:  pkgconfig(fontconfig)
BuildRequires:  fontconfig
BuildRequires:  pkgconfig(python3) >= 3.10
BuildRequires:  pkgconfig(harfbuzz)
BuildRequires:  pkgconfig(zlib)
BuildRequires:  pkgconfig(freetype2)
BuildRequires:  python3dist(pillow)
BuildRequires:  wayland-devel >= 1.23.0
BuildRequires:  pkgconfig(wayland-protocols) >= 1.37
BuildRequires:  (wayland-tools or pkgconfig(wayland-scanner) or /usr/bin/wayland-scanner)
BuildRequires:  pkgconfig(lcms2)
BuildRequires:  (rsync-devel or librsync-devel)
BuildRequires:  pkgconfig(openssl)
BuildRequires:  git-core
BuildRequires:  golang >= 1.21
BuildRequires:  gcc
BuildRequires:  (ncurses or /usr/bin/tic)
BuildRequires:  pkgconfig(libxxhash)
BuildRequires:  pkgconfig(simde)
BuildRequires:  pkgconfig(cairo) >= 1.18
BuildRequires:  pkgconfig(cairo-fc)
BuildRequires:  pkgconfig(pixman-1)
%if %{with tests}
# This is required to run the tests
BuildRequires:  texlive-fontsextra
%endif
# ImageMagick is needed to resize the app icon to all sizes
BuildRequires:  (imagemagick or ImageMagick or %{_bindir}/convert)
# To convert README.asciidoc to README.html, so that it can be opened in a web
# browser.
BuildRequires:  asciidoc

Requires:       cairo
Requires:       harfbuzz
Requires:       freetype2
Requires:       fontconfig
Requires:       (%{_lib}canberra-gtk3_0 or /usr/%{_lib}/libcanberra-gtk3.so.0%{?isa})
# ImageMagick is needed by the images kitten.
Requires:       (imagemagick or ImageMagick or /usr/bin/convert)
Requires:       python3dist(pygments)
Requires:       %{name}-terminfo = %{version}-%{release}
Recommends:     %{name}-docs = %{version}-%{release}

%description
kitty is a cross-platform, fast, feature full, GPU based terminal emulator.

It is designed for power keyboard users. To that end all its controls work
with the keyboard (although it fully supports mouse interactions as well).
Its configuration is a simple, human editable, single file for easy
reproducibility (I like to store configuration in source control).

Finally, kitty is designed from the ground up to support all modern terminal
features, such as unicode, true color, bold/italic fonts, text formatting,
etc. It even extends existing text formatting escape codes, to add support
for features not available elsewhere, such as colored and styled (curly)
underlines. One of the design goals of kitty is to be easily extensible so
that new features can be added in the future with relatively little effort.

%package terminfo
Summary:       Terminfo for %{name}
Group:         Terminals
License:       GPLv3
BuildArch:     noarch

%description terminfo
Terminal info for %{name}.

You can install this package on, for example, servers you want to log on to
remotely to enable all the features available in %{name} without having to
install the whole program.

%package docs
Summary:       Documentation for %{name}
Group:         Terminals
License:       GPLv3
BuildArch:     noarch

%Description docs
%{summary}.

%prep
%autosetup -S git -a2

mkdir fonts
cp %{S:3} fonts

sed -i -e 's/^go 1.23/go 1.21/g' go.mod
asciidoc -b html4 -o README.html README.asciidoc

%build
# We want to control when to update this package and that is done with the
# option "--update-check-interval=0"
%{__python3} setup.py linux-package \
    --prefix %{_real_vendor}-%{_arch} \
    --update-check-interval=0 \
    --verbose \
    --debug \
    --libdir-name %{_lib}

# Fix the path to Python 3
%if 0%{?py3_shebang_fix:1}
%py3_shebang_fix %{_real_vendor}-%{_arch}/%{_lib}/kitty/
%endif

# Convert the app icon to all the sizes
mkdir -p %{_real_vendor}-%{_arch}/share/icons/hicolor/16x16/apps
mkdir -p %{_real_vendor}-%{_arch}/share/icons/hicolor/32x32/apps
mkdir -p %{_real_vendor}-%{_arch}/share/icons/hicolor/64x64/apps
mkdir -p %{_real_vendor}-%{_arch}/share/icons/hicolor/128x128/apps
mkdir -p %{_real_vendor}-%{_arch}/share/icons/hicolor/256x256/apps
mkdir -p %{_real_vendor}-%{_arch}/share/icons/hicolor/512x512/apps

%{_bindir}/convert logo/kitty.iconset/icon_512x512.png \
  -resize 64x64 logo/kitty.iconset/icon_64x64.png
cp logo/kitty.iconset/icon_16x16.png \
  %{_real_vendor}-%{_arch}/share/icons/hicolor/16x16/apps/%{name}.png
cp logo/kitty.iconset/icon_32x32.png \
  %{_real_vendor}-%{_arch}/share/icons/hicolor/32x32/apps/%{name}.png
cp logo/kitty.iconset/icon_64x64.png \
  %{_real_vendor}-%{_arch}/share/icons/hicolor/64x64/apps/%{name}.png
cp logo/kitty.iconset/icon_128x128.png \
  %{_real_vendor}-%{_arch}/share/icons/hicolor/128x128/apps/%{name}.png
cp logo/kitty.iconset/icon_256x256.png \
  %{_real_vendor}-%{_arch}/share/icons/hicolor/256x256/apps/%{name}.png
cp logo/kitty.iconset/icon_512x512.png \
  %{_real_vendor}-%{_arch}/share/icons/hicolor/512x512/apps/%{name}.png

%install
mkdir -p %{buildroot}%{_prefix}
cp -af %{_real_vendor}-%{_arch}/* %{buildroot}%{_prefix}

mkdir -p %{buildroot}%{_sysconfdir}/xdg/%{name}
# Use a config file with the default setting uncommented and with the
# same text rendering as in pre-0.28 versions.
cat docs/_build/html/_downloads/433dadebd0bf504f8b008985378086ce/kitty.conf | \
  sed -e 's,^#\s\+\(\S\+\s\),\1,g' \
  -e 's/^\(text_composition_strategy\).*$/\1 legacy/g' >  \
  %{buildroot}%{_sysconfdir}/xdg/%{name}/%{name}.conf

%if %{with tests}
%check
%{__python3} setup.py test
%endif

%files
%license LICENSE
%doc README.html
%{_bindir}/%{name}
%{_bindir}/kitten
%{_datadir}/applications/%{name}*.desktop
%{_datadir}/icons/hicolor/*/apps/%{name}*
%{_libdir}/%{name}/
%config(noreplace) %{_sysconfdir}/xdg/%{name}/%{name}.conf
%{_mandir}/man1/%{name}.1*
%{_mandir}/man1/kitten*
%{_mandir}/man5/%{name}*
%exclude %{_datadir}/doc/%{name}/html/
%exclude %{_datadir}/terminfo/x/xterm-kitty

%files docs
%{_datadir}/doc/%{name}/html/


%files terminfo
%{_datadir}/terminfo/x/xterm-kitty