#!/usr/bin/env bash

# omarchy:summary=Install a supported development environment
# omarchy:name=dev-env
# omarchy:args=<ruby|node|bun|deno|go|laravel|symfony|php|python|elixir|phoenix|rust|java|zig|ocaml|dotnet|clojure|scala>
# omarchy:examples=omarchy install dev-env ruby | omarchy install dev-env node
# omarchy:requires-sudo=true

# Based: when the development backend is asdf rather than mise (see
# omarchy-dev-env-backend), the asdf port takes over.
if [[ $(omarchy-dev-env-backend) == asdf ]]; then
  exec omarchy-install-dev-env-asdf "$@"
fi

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

install_php() {
  # Based: Debian's names. The extensions upstream enables in Arch's php.ini
  # are packages here, and Debian enables each one as it installs.
  omarchy-pkg-add php-cli composer php-sqlite3 php-xdebug php-bcmath php-intl php-mysql

  # Install Path for Composer
  if [[ :$PATH: != *:$HOME/.config/composer/vendor/bin:* ]]; then
    echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >>"$HOME/.bashrc"
    source "$HOME/.bashrc"
    echo "Added Composer global bin directory to PATH."
  else
    echo "Composer global bin directory already in PATH."
  fi

  # Enable Xdebug's step debugger, as upstream does in Arch's xdebug.ini
  local xdebug_ini
  for xdebug_ini in /etc/php/*/mods-available/xdebug.ini; do
    [[ -f $xdebug_ini ]] || continue
    grep -q '^xdebug.mode=' "$xdebug_ini" || echo "xdebug.mode=debug" | sudo tee -a "$xdebug_ini" >/dev/null
  done
}

install_node() {
  echo -e "Installing Node.js...\n"
  mise use --global node
}

case "$1" in
ruby)
  echo -e "Installing Ruby on Rails...\n"
  omarchy-pkg-add libyaml-dev
  mise settings add ruby.compile false
  mise settings add idiomatic_version_file_enable_tools ruby
  mise use --global ruby@latest
  echo "gem: --no-document" >~/.gemrc
  mise x ruby -- gem install rails --no-document
  echo -e "\nYou can now run: rails new myproject"
  ;;
node)
  install_node
  ;;
bun)
  echo -e "Installing Bun...\n"
  mise use -g bun@latest
  ;;
deno)
  echo -e "Installing Deno...\n"
  mise use -g deno@latest
  ;;
go)
  echo -e "Installing Go...\n"
  mise use --global go@latest
  ;;
php)
  echo -e "Installing PHP...\n"
  install_php
  ;;
laravel)
  echo -e "Installing PHP and Laravel...\n"
  install_php
  install_node
  composer global require laravel/installer
  echo -e "\nYou can now run: laravel new myproject"
  ;;
symfony)
  echo -e "Installing PHP and Symfony...\n"
  install_php
  omarchy-pkg-add symfony-cli
  echo -e "\nYou can now run: symfony new --webapp myproject"
  ;;
python)
  echo -e "Installing Python...\n"
  mise use --global python@latest
  echo -e "\nInstalling uv...\n"
  curl -fsSL https://astral.sh/uv/install.sh | sh
  ;;
elixir)
  echo -e "Installing Elixir...\n"
  mise use --global erlang@latest
  mise use --global elixir@latest
  mise x elixir -- mix local.hex --force
  ;;
phoenix)
  echo -e "Installing Phoenix Framework...\n"
  # Ensure Erlang/Elixir first
  mise use --global erlang@latest
  mise use --global elixir@latest
  # Hex & Rebar
  mise x elixir -- mix local.hex --force
  mise x elixir -- mix local.rebar --force
  # Phoenix project (phx_new)
  mise x elixir -- mix archive.install hex phx_new --force
  echo -e "\nYou can now run: mix phx.new my_app"
  ;;
rust)
  echo -e "Installing Rust...\n"
  bash -c "$(curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs)" -- -y
  ;;
java)
  echo -e "Installing Java...\n"
  mise use --global java@latest
  ;;
zig)
  echo -e "Installing Zig...\n"
  mise use --global zig@latest
  mise use -g zls@latest
  ;;
ocaml)
  echo -e "Installing OCaml...\n"
  bash -c "$(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"
  opam init --yes
  eval "$(opam env)"
  opam install ocaml-lsp-server odoc ocamlformat utop --yes
  ;;
dotnet)
  echo -e "Installing .NET...\n"
  mise use --global dotnet@latest
  ;;
clojure)
  echo -e "Installing Clojure...\n"
  omarchy-pkg-add rlwrap
  mise use --global clojure@latest
  ;;
scala)
  echo -e "Installing Scala...\n"
  mise use --global java@latest
  mise use --global scala@latest
  mise use --global scala-cli@latest
  ;;
esac
