#!/bin/bash

# omarchy:summary=Remove a development environment that was previously installed via omarchy-install-dev-env.
# omarchy:args=<ruby|node|bun|deno|go|php|laravel|symfony|python|elixir|phoenix|zig|rust|java|dotnet|ocaml|clojure|scala>
# omarchy:requires-sudo=true

if [[ -z $1 ]]; then
  echo "Usage: omarchy-remove-dev-env <ruby|node|bun|deno|go|php|laravel|symfony|python|elixir|phoenix|zig|rust|java|dotnet|ocaml|clojure|scala>" >&2
  exit 1
fi

cleanup_ocaml_sudo_credentials() {
  local status=$?
  trap - EXIT HUP INT TERM

  if ! /usr/bin/sudo -k >/dev/null 2>&1; then
    echo "Failed to invalidate sudo credentials after OCaml removal." >&2
    (( status != 0 )) || status=1
  fi

  exit "$status"
}

begin_ocaml_sudo_boundary() {
  trap cleanup_ocaml_sudo_credentials EXIT
  trap 'exit 129' HUP
  trap 'exit 130' INT
  trap 'exit 143' TERM

  /usr/bin/sudo -k >/dev/null || {
    echo "Unable to start OCaml removal with a cold sudo credential state." >&2
    exit 1
  }

  if ! /usr/bin/sudo -N -V >/dev/null 2>&1; then
    echo "This sudo does not support --no-update; refusing to remove OCaml." >&2
    exit 1
  fi
}

remove_php() {
  mise uninstall php --all
  mise rm -g php
  mise tool-alias unset php

  # PHP from before it moved to mise
  omarchy-pkg-drop php composer php-sqlite xdebug
}

case "$1" in
ruby)
  echo -e "Removing Ruby...\n"
  mise uninstall ruby --all
  mise rm -g ruby
  rm -f ~/.gemrc
  ;;
node)
  echo -e "Removing Node.js...\n"
  mise uninstall node --all
  mise rm -g node
  ;;
bun)
  echo -e "Removing Bun...\n"
  mise uninstall bun --all
  mise rm -g bun
  ;;
deno)
  echo -e "Removing Deno...\n"
  mise uninstall deno --all
  mise rm -g deno
  ;;
go)
  echo -e "Removing Go...\n"
  mise uninstall go --all
  mise rm -g go
  ;;
php)
  echo -e "Removing PHP...\n"
  remove_php
  ;;
laravel)
  echo -e "Removing Laravel...\n"
  mise x php -- composer global remove laravel/installer 2>/dev/null || true

  rm -f "$HOME/.local/bin/laravel" "$HOME/.config/composer/vendor/bin/laravel"
  ;;
symfony)
  echo -e "Removing Symfony CLI...\n"
  omarchy-pkg-drop symfony-cli
  ;;
python)
  echo -e "Removing Python...\n"
  mise uninstall python --all
  mise rm -g python
  rm -rf ~/.local/bin/uv ~/.local/bin/uvx ~/.cargo/bin/uv 2>/dev/null || true
  ;;
elixir|phoenix)
  echo -e "Removing Elixir/Erlang...\n"
  mise uninstall elixir --all
  mise uninstall erlang --all
  mise rm -g elixir
  mise rm -g erlang
  ;;
zig)
  echo -e "Removing Zig...\n"
  mise uninstall zig --all
  mise uninstall zls --all
  mise rm -g zig
  mise rm -g zls
  ;;
rust)
  echo -e "Removing Rust...\n"
  rustup self uninstall -y 2>/dev/null || true
  ;;
java)
  echo -e "Removing Java...\n"
  mise uninstall java --all
  mise rm -g java
  ;;
dotnet)
  echo -e "Removing .NET...\n"
  mise uninstall dotnet --all
  mise rm -g dotnet
  ;;
ocaml)
  begin_ocaml_sudo_boundary

  echo -e "Removing OCaml...\n"
  opam switch remove default -y 2>/dev/null || true
  rm -rf ~/.opam 2>/dev/null || true
  if ! /usr/bin/sudo -N -- /usr/bin/rm -f /usr/local/bin/opam; then
    echo "Failed to remove the system opam launcher." >&2
    exit 1
  fi
  ;;
clojure)
  echo -e "Removing Clojure...\n"
  mise uninstall clojure --all
  mise rm -g clojure
  ;;
scala)
  echo -e "Removing Scala...\n"
  mise uninstall scala --all
  mise uninstall scala-cli --all
  mise rm -g scala
  mise rm -g scala-cli
  ;;
*)
  echo "Unknown environment: $1"
  exit 1
  ;;
esac

echo -e "\nDone!"
