# Maintainer: aur.chaotic.cx

: ${_install_path:=usr/lib}

_pkgname="gpt4all-chat"
pkgname="$_pkgname-git"
pkgver=3.10.0.r4.gb666d16
pkgrel=1
pkgdesc="Qt-based frontend to run LLMs locally"
url="https://github.com/nomic-ai/gpt4all"
license=('MIT')
arch=('x86_64')

depends=(
  'qt6-base'
  'qt6-declarative'
  'qt6-httpserver'
  'qt6-svg'
  'libfmt.so'
  'qt6-webengine'
)
makedepends=(
  'cmake'
  'git'
  'ninja'
  'patchelf'
  'qt6-tools'
  'vulkan-headers'
)
optdepends=(
  'gpt4all-chat-data: embedding model for LocalDocs'
)

provides=("$_pkgname")
conflicts=("$_pkgname")

_pkgsrc="$_pkgname"
source=(
  "$_pkgsrc"::"git+$url.git"
  '0001-embedding-model-no-dl.patch'
)
sha256sums=(
  'SKIP'
  'b320bf086e77839d2efb6e697fed674581934605c5e5161fac5ef512ee4ce44c'
)

pkgver() {
  cd "$_pkgsrc"
  git describe --long --tags --abbrev=7 --exclude='*[a-zA-Z][a-zA-Z]*' \
    | sed -E 's/^[^0-9]*//;s/([^-]*-g)/r\1/;s/-/./g'
}

prepare() {
  cd "$_pkgsrc"
  git rm -r gpt4all-chat/deps/fmt
  git submodule update --init --recursive --depth=1

  # disable embedding model download
  patch -Np1 -F100 -i ../0001-embedding-model-no-dl.patch

  # use system fmt
  sed -E -e '/add_subdirectory\(fmt/s&^.*$&find_package(fmt)&' -i gpt4all-chat/deps/CMakeLists.txt

  # fix for Qt 6.10
  sed -E -e '/COMPONENTS/s&\b(Gui)\b&\1 \1Private&' -i gpt4all-chat/deps/QXlsx/QXlsx/CMakeLists.txt

  # remove duplicate declarations
  sed -E -e '/^extern /d' -i gpt4all-backend/deps/llama.cpp-mainline/ggml/src/kompute/external/bin/xxd.c

  # fix vulkan
  sed -E \
    -e 's&\bvk::(DynamicLoader)\b&vk::detail::\1&g' \
    -e 's&\bvk::(DispatchLoaderDynamic)\b&vk::detail::\1&g' \
    -i gpt4all-backend/deps/llama.cpp-mainline/ggml/src/kompute/src/Manager.cpp \
    gpt4all-backend/deps/llama.cpp-mainline/ggml/src/kompute/src/include/kompute/Manager.hpp
}

build() {
  local _cmake_options=(
    -B build
    -S "$_pkgsrc/gpt4all-chat"
    -G Ninja
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_INSTALL_PREFIX='/usr'
    -DBUILD_TESTING=OFF
    -Wno-dev

    -DKOMPUTE_OPT_BUILD_SHADERS=ON
    -DKOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK=ON
    -DKOMPUTE_OPT_USE_BUILT_IN_FMT=OFF
    -DKOMPUTE_OPT_USE_BUILT_IN_SPDLOG=OFF
    -DKOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER=OFF
    -DKOMPUTE_OPT_USE_SPDLOG=OFF
    -DLLMODEL_CUDA=OFF
  )

  cmake "${_cmake_options[@]}"
  cmake --build build
  DESTDIR="fakeinstall" cmake --install build
}

package() {
  # Note: install manually to prevent unwanted includes, static libs, etc

  # binary
  install -Dm755 fakeinstall/usr/bin/chat -t "$pkgdir/$_install_path/$_pkgname/"

  # libraries
  chmod a-x fakeinstall/usr/lib/*.so*
  cp -a fakeinstall/usr/lib/*.so* "$pkgdir/$_install_path/$_pkgname/"

  # rpath
  patchelf --set-rpath '$ORIGIN' "$pkgdir/$_install_path/$_pkgname"/*

  # icon
  install -Dm644 "$_pkgsrc/gpt4all-chat/icons/gpt4all.svg" "$pkgdir/usr/share/pixmaps/$_pkgname.svg"

  # license
  install -Dm644 "$_pkgsrc/LICENSE.txt" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  # launcher
  install -Dm644 /dev/stdin "$pkgdir/usr/share/applications/$_pkgname.desktop" << END
[Desktop Entry]
Type=Application
Name=GPT4All Chat
GenericName=GPT4All Chat
Comment=$pkgdesc
Exec=$_pkgname
Icon=$_pkgname
StartupWMClass=gpt4all-chat
Terminal=false
StartupNotify=true
Categories=Office;InstantMessaging;
Keywords=chat;gpt;ai;
END

  # script
  install -Dm755 /dev/stdin "$pkgdir/usr/bin/$_pkgname" << END
#!/usr/bin/env sh
exec "/$_install_path/$_pkgname/chat" "\$@"
END
}
