# Maintainer: noureddinex
pkgname=lmstudio-bin
pkgver=0.4.26
pkgrel=1
_build=4
_pkgver=${pkgver}-${_build}
pkgdesc="LM Studio - A desktop app for exploring and running large language models locally"
arch=('x86_64' 'aarch64')
url="https://lmstudio.ai"
license=('EULA')
depends=('fuse2' 'zlib' 'hicolor-icon-theme' 'gtk3' 'nss' 'libxcrypt-compat')
options=('!strip')
install=lmstudio-bin.install
provides=(lmstudio)
conflicts=(lmstudio)
source=("lmstudio.png"
        "lmstudio.desktop")
source_x86_64=("https://installers.lmstudio.ai/linux/x64/${_pkgver}/LM-Studio-${_pkgver}-x64.AppImage")
source_aarch64=("https://installers.lmstudio.ai/linux/arm64/${_pkgver}/LM-Studio-${_pkgver}-arm64.AppImage")
sha256sums=('9f791789c959a11316328692807737a5f1bc1c170ae99ec04c56bfd8ee8263e5' '16b67b6cd672a05b9a0e012c8b9a91187ceda8f85b3391db471298c54e002bcd')
sha256sums_x86_64=('2c62a38a93ad03ca5daaef28804880be167566783ffafc173ce2e1cb10b7552e')
sha256sums_aarch64=('8de222cd9d332496494e28f9863cddc9509802ad213c41e7422f15b9f63416d8')

# LM Studio names its AppImages by a vendor architecture suffix. Resolve it in the
# functions so sourcing the PKGBUILD without CARCH still reads the version.
_appimage() {
  case "$CARCH" in
    x86_64) echo "LM-Studio-${_pkgver}-x64.AppImage" ;;
    aarch64) echo "LM-Studio-${_pkgver}-arm64.AppImage" ;;
  esac
}

prepare() {
  chmod +x "${srcdir}/$(_appimage)"
}

package() {
  # Create installation directory
  install -dm755 "$pkgdir/opt/lm-studio"
  
  # Install AppImage
  install -Dm755 "${srcdir}/$(_appimage)" "$pkgdir/opt/lm-studio/lm-studio.AppImage"
  
  # Install static icon
  install -Dm644 "${srcdir}/lmstudio.png" "${pkgdir}/usr/share/icons/hicolor/512x512/apps/lmstudio-bin.png"
  install -Dm644 "${srcdir}/lmstudio.png" "${pkgdir}/usr/share/pixmaps/lmstudio-bin.png"
  
  # Desktop entry, under LM Studio's own desktop ID, which matches its window class
  install -Dm644 "$srcdir/lmstudio.desktop" "$pkgdir/usr/share/applications/ai.elementlabs.lmstudio.desktop"
  
  # Symlink to binary
  install -dm755 "$pkgdir/usr/bin"
  ln -s /opt/lm-studio/lm-studio.AppImage "$pkgdir/usr/bin/lm-studio"
}
