#!/usr/bin/env sh
set -eu

BASE_URL="${E2E_SECURITY_BASE_URL:-https://e2esecurity.de}"
TMP_DIR="$(mktemp -d)"
VSIX_PATH="$TMP_DIR/e2e-security.vsix"
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM

printf '%s\n' 'Downloading E2E Security...'
curl -fL "$BASE_URL/downloads/e2e-security-0.5.2.vsix" -o "$VSIX_PATH"

installed=0
for editor in code cursor agy-ide; do
  if command -v "$editor" >/dev/null 2>&1; then
    printf 'Installing in %s...\n' "$editor"
    "$editor" --install-extension "$VSIX_PATH" --force
    installed=1
  fi
done

if [ "$installed" -eq 0 ]; then
  printf '%s\n' 'No compatible editor CLI was found. Install the VSIX manually from your editor.' >&2
  exit 1
fi

printf '%s\n' 'E2E Security installed. Reload your editor and open the lock icon.'
