#!/usr/bin/env bash

# Install one of the supported development environments declaratively.
# Usually called via Install > Development > * in the Nomarchy Menu.

if [[ -z $1 ]]; then
  echo "Usage: nomarchy-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() {
  echo -e "Installing PHP environment declaratively...\n"
  nomarchy-pkg-add php
  nomarchy-pkg-add phpExtensions.bcmath
  nomarchy-pkg-add phpExtensions.intl
  nomarchy-pkg-add phpExtensions.iconv
  nomarchy-pkg-add phpExtensions.openssl
  nomarchy-pkg-add phpExtensions.pdo_sqlite
  nomarchy-pkg-add phpExtensions.pdo_mysql
  nomarchy-pkg-add phpPackages.composer
  echo -e "\nPHP environment added to your packages."
}

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

case "$1" in
ruby)
  echo -e "Installing Ruby with mise...\n"
  nomarchy-pkg-add libyaml
  mise use --global ruby@latest
  echo -e "\nYou can now use: ruby --version"
  ;;
node)
  install_node
  ;;
bun)
  echo -e "Installing Bun with mise...\n"
  mise use -g bun@latest
  ;;
deno)
  echo -e "Installing Deno with mise...\n"
  mise use -g deno@latest
  ;;
go)
  echo -e "Installing Go with mise...\n"
  mise use --global go@latest
  ;;
php)
  install_php
  ;;
laravel)
  echo -e "Installing Laravel stack...\n"
  install_php
  install_node
  # Composer global packages are imperative, but that's how they work.
  composer global require laravel/installer
  echo -e "\nYou can now run: laravel new myproject"
  ;;
python)
  echo -e "Installing Python with mise...\n"
  mise use --global python@latest
  echo -e "\nInstalling uv...\n"
  # uv can be installed via nix
  nomarchy-pkg-add uv
  ;;
rust)
  echo -e "Installing Rust declaratively...\n"
  # Instead of shell script from website, use nixpkgs
  nomarchy-pkg-add rustup
  rustup-init -y
  ;;
# Add more language cases as needed, utilizing mise or nixpkgs.
*)
  # Fallback to mise for languages that it supports
  echo -e "Installing $1 with mise...\n"
  if mise use --global "$1@latest"; then
    echo "$1 installed successfully."
  else
    echo "Unsupported language: $1"
    exit 1
  fi
  ;;
esac
