#!/usr/bin/env bash

# omarchy:summary=Install Cursor, the AI code editor
# omarchy:requires-sudo=true

# Based: Cursor ships no apt repository or Flatpak, so the vendor's deb for
# this architecture is fetched and installed, as the former dev tooling
# did; macOS takes the cask. Then the editor defaults land where this OS's
# Cursor reads them: settings.json and keybindings.json, and the extensions
# that go with them.

set -eo pipefail

echo "Installing Cursor..."

case $(omarchy-os) in
darwin)
  omarchy-pkg-add cursor cask=cursor
  ;;
*)
  if omarchy-cmd-missing cursor; then
    case $(dpkg --print-architecture) in
    amd64) arch=x64 ;;
    arm64) arch=arm64 ;;
    *)
      echo "Cursor has no build for $(dpkg --print-architecture); nothing to install"
      exit 0
      ;;
    esac
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    curl -fL --progress-bar "https://api2.cursor.sh/updates/download/golden/linux-$arch-deb/cursor/1.7" -o "$tmp/cursor.deb"
    sudo apt-get install -y "$tmp/cursor.deb"
  fi
  ;;
esac

user_dir=$(omarchy-code-config-dir cursor)
mkdir -p "$user_dir"
[[ -e $user_dir/keybindings.json ]] || cp "$OMARCHY_PATH/config/code/User/keybindings.json" "$user_dir/keybindings.json"
[[ -e $user_dir/settings.json ]] || cp "$OMARCHY_PATH/config/cursor/User/settings.json" "$user_dir/settings.json"

omarchy-code-extensions ahmadalli.vscode-nginx-conf anysphere.remote-ssh eamodio.gitlens editorconfig.editorconfig \
  gruntfuggly.todo-tree kisstkondoros.vscode-gutter-preview mechatroner.rainbow-csv mikestead.dotenv mkhl.direnv \
  ms-vscode.hexeditor ms-vscode.makefile-tools naumovs.color-highlight psioniq.psi-header redhat.vscode-xml \
  redhat.vscode-yaml streetsidesoftware.code-spell-checker ultram4rine.vscode-choosealicense vspacecode.vspacecode \
  vspacecode.whichkey zawys.vscode-as-git-mergetool
