#!/bin/bash

ICON_DIR="$HOME/.local/share/applications/icons"
DESKTOP_DIR="$HOME/.local/share/applications/"

if [ "$#" -ne 1 ]; then
  ~/.local/share/omarchy/bin/omarchy-show-logo

  # Find all web apps
  while IFS= read -r -d '' file; do
    if grep -q '^Exec=.*chromium.*--app' "$file"; then
      WEB_APPS+=("$(basename "${file%.desktop}")")
    fi
  done < <(find "$DESKTOP_DIR" -name '*.desktop' -print0)

  if ((${#WEB_APPS[@]})); then
    IFS=$'\n' SORTED_WEB_APPS=($(sort <<<"${WEB_APPS[*]}"))
    unset IFS
    APP_NAME=$(gum choose --header "Select web app to remove..." "${SORTED_WEB_APPS[@]}")
  else
    echo "No web apps to remove."
    exit 1
  fi
else
  APP_NAME="$1"
fi

if [[ -z "$APP_NAME" ]]; then
  echo "You must provide web app name."
  exit 1
fi

rm "$DESKTOP_DIR/$APP_NAME.desktop"
rm "$ICON_DIR/$APP_NAME.png"

if [ "$#" -ne 1 ]; then
  echo -e "Removed $APP_NAME\n"
  ~/.local/share/omarchy/bin/omarchy-show-done
fi
