pkgname=kitty
pkgbase=kitty
pkgver=0.44.0
pkgdesc="A modern, hackable, featureful, OpenGL-based terminal emulator"
arch=('x86_64')
homepage="https://github.com/kovidgoyal/kitty"
license=('GPL-3.0-only')
depends=(
    'cairo'
    'dbus'
    'freetype'
    'fontconfig'
    'harfbuzz'
    'lcms'
    'libpng'
    'libxkbcommon'
    'openssl'
    'python3'
    'wayland'
    'xxhash'
    'zlib'
)
makedepends=(
    'go'
    'simde'
    'symbols-nerd-font'
    'wayland-protocols'
)
sources=("${pkgbase}-${pkgver}.tar.xz")
urls=("https://github.com/kovidgoyal/${pkgbase}/releases/download/v${pkgver}/${sources[0]}")
md5sums=("c3c31e13371c08166cc2bc4ea34cdc74")

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1

  # sed unfortunately feels easier on maintenance than patches here
  local sedargs=(
    -e "/num_workers =/s/=.*/= ${PARALLEL_JOBS}/"
    -e "s/cflags.append.*-O3.*/pass/" -e 's/-O3//'
    -e "s/cflags.append(fortify_source)/pass/" # use toolchain's _
    -e "s/ld_flags.append('-[sw]')/pass/"
  )
  # kitty is often popular on wayland-only setups, try to allow this
  sedargs+=( -e '/gl_libs =/s/=.*/= []/' ) #857918
  sedargs+=( -e "s/'x11 wayland'/'wayland'/" )
  sed -i setup.py "${sedargs[@]}"

  local skiptests=(
    # broken with nspawn defaults, skip for convenience (bug #954176)
    kitty_tests/crypto.py
    # relies on 'who' command which doesn't detect users with pid-sandbox
    kitty_tests/utmp.py
    # may fail/hang depending on environment and shell initialization
    kitty_tests/{shell_integration,ssh}.py
    # relies on /proc/self/fd and gets confused when ran from here
    tools/utils/tpmfile_test.go
  )
  rm "${skiptests[@]}"
}

src_build() {
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw -p=${PARALLEL_JOBS} -v -x -buildvcs=false"
  local conf=(
    --disable-link-time-optimization
    --ignore-compiler-warnings
    --shell-integration="enabled no-sudo"
    --update-check-interval=0
    --verbose
  )
  python3 setup.py linux-package "${conf[@]}"

  rm -r linux-package/share/terminfo
  mv -v linux-package/share/doc/{${pkgname},${pkgname}-${pkgver}}

  linux-package/bin/kitty +runpy \
    'from kitty.config import *; print(commented_out_default_config())' \
    > linux-package/share/doc/${pkgname}-${pkgver}/kitty.conf

  # generate shell completions, shell-integration/ has some "old " pre-gen
  # ones that currently miss things (no bash, no kitten for zsh, etc...)
  linux-package/bin/kitten __complete__ setup bash > "${TMPDIR}"/kitty
  linux-package/bin/kitten __complete__ setup fish > "${TMPDIR}"/kitty.fish
  linux-package/bin/kitten __complete__ setup zsh > "${TMPDIR}"/_kitty
}

src_install() {
  mv -v linux-package "${pkgdir}/usr"

  ln -svrf "${pkgdir}/usr/share/fonts/symbols-nerd-font/SymbolsNerdFontMono-Regular.ttf" \
    "${pkgdir}/usr/lib/kitty/fonts/SymbolsNerdFontMono-Regular.ttf"

  install -vDm644 "${TMPDIR}/kitty" "${pkgdir}"/usr/share/bash-completion/completions/kitty
  install -vDm644 "${TMPDIR}/kitty.fish" "${pkgdir}"/usr/share/fish/vendor_completions.d/kitty.fish
  install -vDm644 "${TMPDIR}/_kitty" "${pkgdir}"/usr/share/zsh/site-functions/_kitty
  for _f in edit-in-kitty clone-in-kitty kitten; do
    ln -sv kitty "${pkgdir}"/usr/share/bash-completion/completions/${_f}
  done
}

# vim:ft=sh syn=sh et sw=2:
