#!/usr/bin/env bash

# omarchy:summary=Install VS Code extensions into every VS Code-family editor that is installed
# omarchy:args=<extension-id...>
# omarchy:examples=omarchy code extensions redhat.ansible ms-azuretools.vscode-docker
# omarchy:hidden=true

# Based: a development row names the editor extensions that go with it and
# this installs them into whichever of Cursor, VS Code and VSCodium is on the
# machine, through each editor's own CLI. Cursor reads Open VSX, so an
# extension only Microsoft's marketplace carries is reported, not failed:
# the row that asked for it has already installed its tool.
if (($# == 0)); then
  echo "Usage: omarchy-code-extensions <extension-id...>" >&2
  exit 1
fi

editors=()
if omarchy-cmd-present cursor; then
  editors+=("cursor")
fi
if omarchy-pkg-flatpak-present com.visualstudio.code 2>/dev/null; then
  editors+=("flatpak run com.visualstudio.code")
elif omarchy-cmd-present code; then
  editors+=("code")
fi
if omarchy-pkg-flatpak-present com.vscodium.codium 2>/dev/null; then
  editors+=("flatpak run com.vscodium.codium")
elif omarchy-cmd-present codium; then
  editors+=("codium")
fi

if ((${#editors[@]} == 0)); then
  echo "No VS Code-family editor is installed; skipping extensions: $*"
  exit 0
fi

for editor in "${editors[@]}"; do
  for extension in "$@"; do
    if ! $editor --install-extension "$extension" >/dev/null 2>&1; then
      echo "Could not install the $extension extension with ${editor%% *}" >&2
    fi
  done
done
