#!/bin/bash

# Remove all web apps installed via nomarchy-webapp-install.
# Identifies web apps by their Exec pattern (nomarchy-launch-webapp or nomarchy-webapp-handler).

set -e

APP_DIR="${1:-$HOME/.local/share/applications}"
ICON_DIR="$HOME/.local/share/applications/icons"

echo "Scanning for web apps in $APP_DIR..."

webapp_desktop_files=()
while IFS= read -r -d '' file; do
  if grep -q "Exec=nomarchy-launch-webapp\|Exec=nomarchy-webapp-handler" "$file" 2>/dev/null; then
    webapp_desktop_files+=("$file")
  fi
done < <(find "$APP_DIR" -maxdepth 1 -name "*.desktop" -print0 2>/dev/null)

if (( ${#webapp_desktop_files[@]} == 0 )); then
  echo "No web apps found."
  exit 0
fi

for file in "${webapp_desktop_files[@]}"; do
  app_name=$(basename "$file" .desktop)
  echo "Removing web app: $app_name"
  rm -f "$file"
  rm -f "$ICON_DIR/$app_name.png"
done

if command -v update-desktop-database &>/dev/null; then
  update-desktop-database "$APP_DIR" &>/dev/null || true
fi

echo "Web apps removed successfully."
