#!/usr/bin/env bash

# omarchy:summary=Install LM Studio, the local model runner
# omarchy:requires-sudo=true

# Based: LM Studio ships no deb or Flatpak; the vendor's AppImage lands in
# ~/Applications, as the former dev tooling did. The macOS cask is built
# for Apple silicon only.

set -eo pipefail

version=0.3.39-1
appimage="$HOME/Applications/LM-Studio.AppImage"

echo "Installing LM Studio..."

case $(omarchy-os) in
darwin)
  if [[ $(uname -m) != arm64 ]]; then
    echo "LM Studio's macOS build is Apple silicon only; nothing to install on this Mac"
    exit 0
  fi
  omarchy-pkg-add lm-studio cask=lm-studio
  ;;
*)
  case $(dpkg --print-architecture) in
  amd64) arch=x64 ;;
  arm64) arch=arm64 ;;
  *)
    echo "LM Studio has no build for $(dpkg --print-architecture); nothing to install"
    exit 0
    ;;
  esac
  if [[ ! -x $appimage ]]; then
    mkdir -p "$HOME/Applications"
    curl -fL --progress-bar "https://installers.lmstudio.ai/linux/$arch/$version/LM-Studio-$version-$arch.AppImage" -o "$appimage"
    chmod +x "$appimage"
  fi
  ;;
esac
