#!/bin/bash

# omarchy:summary=Remove a supported browser and clean up Omarchy browser defaults
# omarchy:args=<chrome|brave|brave-origin|edge|firefox|zen>
# omarchy:examples=omarchy remove browser firefox | omarchy remove browser brave
# omarchy:requires-sudo=true

set_fallback_default_browser() {
  local current_browser
  current_browser=$(env -u BROWSER xdg-settings get default-web-browser)

  if [[ $current_browser != $1 ]]; then
    return
  fi

  if omarchy-cmd-present chromium; then
    env -u BROWSER xdg-settings set default-web-browser chromium.desktop || true
  fi
}

case $1 in
chrome)
  echo "Removing Chrome..."
  set_fallback_default_browser google-chrome.desktop
  omarchy-pkg-drop google-chrome
  rm -f ~/.config/chrome-flags.conf
  sudo rm -f /etc/opt/chrome/policies/managed/color.json
  ;;
edge)
  echo "Removing Edge..."
  set_fallback_default_browser microsoft-edge.desktop
  omarchy-pkg-drop microsoft-edge-stable-bin
  rm -f ~/.config/microsoft-edge-stable-flags.conf
  sudo rm -f /etc/opt/edge/policies/managed/color.json
  ;;
brave)
  echo "Removing Brave..."
  set_fallback_default_browser brave-browser.desktop
  omarchy-pkg-drop brave-bin
  rm -f ~/.config/brave-flags.conf

  if omarchy-pkg-missing brave-origin-bin; then
    sudo rm -rf /etc/brave
  fi
  ;;
brave-origin)
  echo "Removing Brave Origin..."
  set_fallback_default_browser brave-origin.desktop
  omarchy-pkg-drop brave-origin-bin
  rm -f ~/.config/brave-origin-flags.conf

  if omarchy-pkg-missing brave-bin; then
    sudo rm -rf /etc/brave
  fi
  ;;
firefox)
  echo "Removing Firefox..."
  set_fallback_default_browser firefox.desktop
  omarchy-pkg-drop firefox
  ;;
zen)
  echo "Removing Zen..."
  set_fallback_default_browser zen.desktop
  omarchy-pkg-drop zen-browser-bin
  ;;
*)
  echo "Usage: omarchy-remove-browser <chrome|brave|brave-origin|edge|firefox|zen>"
  exit 1
  ;;
esac
