#!/usr/bin/env bash

# omarchy:summary=List the development environments installed through the current backend
# omarchy:hidden=true

# Based: one line naming what Install and Remove > Development find installed,
# so the menu's guards read it once per batch. Under mise these are upstream's
# own checks. Under asdf an environment counts once asdf holds a version of
# every plugin it needs.
installed=()

found() {
  local name=$1
  shift
  "$@" && installed+=("$name")
  return 0
}

has_entries() { compgen -G "$1/*" >/dev/null; }
phoenix_archive() { compgen -G "$HOME/.mix/archives/phx_new*" >/dev/null; }

if [[ $(omarchy-dev-env-backend) == asdf ]]; then
  asdf_installs=${ASDF_DATA_DIR:-$HOME/.asdf}/installs
  asdf_has() {
    local plugin
    for plugin in "$@"; do
      has_entries "$asdf_installs/$plugin" || return 1
    done
  }

  found ruby asdf_has ruby
  found node asdf_has nodejs
  found bun asdf_has bun
  found deno asdf_has deno
  found go asdf_has golang
  found python asdf_has python
  found elixir asdf_has erlang elixir
  found java asdf_has java
  found zig asdf_has zig
  found dotnet asdf_has dotnet
  found clojure asdf_has clojure
  found scala asdf_has scala
  found rust asdf_has rust
  found php asdf_has php
  found symfony asdf_has symfony-cli

  # the rows the retired dev tooling used to install, each an asdf plugin or
  # a set of them; socket is the shims omarchy-install-development-socket lays
  found julia asdf_has julia
  found solana asdf_has solana
  found solidity asdf_has foundry
  found sui asdf_has sui
  found javascript asdf_has nodejs bun deno yarn pnpm
  found socket test -x "$HOME/.local/share/omarchy/sfw-shims/npm"
  found mongosh asdf_has mongosh
  found mysql asdf_has mysql
  found postgres asdf_has postgres
  found aws asdf_has awscli
  found hcloud asdf_has hcloud
  found kubernetes asdf_has kubectl helm kops krew kubectx k9s
  found opencloud asdf_has cloudmonkey
  found opentofu asdf_has opentofu
  found ovh asdf_has ovhcloud
else
  mise_has() { [[ -d $HOME/.local/share/mise/installs/$1 ]]; }

  found ruby mise_has ruby
  found node mise_has node
  found bun mise_has bun
  found deno mise_has deno
  found go mise_has go
  found python mise_has python
  found elixir mise_has elixir
  found java mise_has java
  found zig mise_has zig
  found dotnet mise_has dotnet
  found clojure mise_has clojure
  found scala mise_has scala
  found rust test -d "$HOME/.rustup"
  found php omarchy-pkg-present php
  found symfony omarchy-pkg-present symfony-cli
fi

found ocaml test -d "$HOME/.opam"
found laravel test -x "$HOME/.config/composer/vendor/bin/laravel"
found phoenix phoenix_archive

echo "${installed[*]}"
