#!/usr/bin/env bash

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

# Based: omarchy-install-dev-env hands over here when the development backend
# is asdf. Each environment is upstream's step with mise swapped for asdf, on
# Debian and on macOS alike: the build dependencies through omarchy-pkg-add,
# whose specs mark Debian's -dev packages Linux only and brew's libraries macOS
# only, then omarchy-dev-env-tool for the plugin, the latest version and the
# ~/.tool-versions line, then the framework on top. Three steps read
# omarchy-os: PHP and the Symfony CLI, which the Mac installs from brew rather
# than building, and Erlang's configure, which has to be told where brew keeps
# OpenSSL. Go, Java and Python carry what the retired dev tooling installed
# beside them: the Go release tools, Maven, and pipx, Poetry and a five-day
# release cooldown on uv, pip and Poetry (BASED-91).

set -eo pipefail

usage="Usage: omarchy-install-dev-env <ruby|node|bun|deno|go|laravel|symfony|php|python|elixir|phoenix|rust|java|zig|ocaml|dotnet|clojure|scala>"

if [[ -z $1 ]]; then
  echo "$usage" >&2
  exit 1
fi

asdf_data=${ASDF_DATA_DIR:-$HOME/.asdf}

omarchy-pkg-add asdf

# The asdf package puts its shims on PATH from the next login; the framework
# steps below need them now.
export PATH="$asdf_data/shims:$PATH"

# tool <plugin> [version prefix]
tool() {
  omarchy-dev-env-tool "$@"
}

tool_missing() {
  ! compgen -G "$asdf_data/installs/$1/*" >/dev/null
}

install_node() {
  echo -e "Installing Node.js...\n"
  omarchy-pkg-add curl linux=only gawk gpg linux=only dirmngr linux=only
  tool nodejs
}

install_php() {
  if [[ $(omarchy-os) == darwin ]]; then
    # the Mac map's route: brew's PHP and Composer, not a source build
    omarchy-pkg-add php composer
  else
    omarchy-pkg-add autoconf bison build-essential curl gettext git libcurl4-openssl-dev libedit-dev libgd-dev \
      libicu-dev libjpeg-dev libmariadb-dev libonig-dev libpng-dev libpq-dev libreadline-dev libsqlite3-dev \
      libssl-dev libxml2-dev libzip-dev openssl pkg-config re2c zlib1g-dev
    tool php
  fi

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

install_java() {
  omarchy-pkg-add unzip linux=only jq
  if tool_missing java; then
    tool java openjdk
  fi
}

install_elixir() {
  omarchy-pkg-add build-essential linux=only autoconf linux=only m4 linux=only libncurses-dev linux=only \
    libssl-dev linux=only unzip linux=only openssl@3 darwin=only
  local configure="--without-javac --without-wx --without-odbc"
  if [[ $(omarchy-os) == darwin ]]; then
    configure+=" --with-ssl=$(brew --prefix openssl@3)"
  fi
  KERL_CONFIGURE_OPTIONS=$configure tool erlang
  tool elixir
  mix local.hex --force
}

case "$1" in
ruby)
  echo -e "Installing Ruby on Rails...\n"
  omarchy-pkg-add autoconf linux=only patch linux=only build-essential linux=only rustc linux=only \
    libssl-dev linux=only libyaml-dev linux=only libreadline-dev linux=only zlib1g-dev linux=only \
    libgmp-dev linux=only libncurses-dev linux=only libffi-dev linux=only libgdbm-dev linux=only \
    libdb-dev linux=only uuid-dev linux=only \
    openssl@3 darwin=only libyaml darwin=only gmp darwin=only readline darwin=only
  tool ruby
  echo "gem: --no-document" >~/.gemrc
  gem install rails --no-document
  asdf reshim ruby
  echo -e "\nYou can now run: rails new myproject"
  ;;
node)
  install_node
  ;;
bun)
  echo -e "Installing Bun...\n"
  omarchy-pkg-add curl linux=only unzip linux=only
  tool bun
  ;;
deno)
  echo -e "Installing Deno...\n"
  omarchy-pkg-add curl linux=only unzip linux=only
  tool deno
  ;;
go)
  echo -e "Installing Go...\n"
  omarchy-pkg-add curl linux=only
  tool golang
  echo -e "\nInstalling golangci-lint, GoReleaser and nfpm...\n"
  tool golangci-lint
  tool goreleaser
  tool nfpm
  ;;
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
  asdf reshim php
  echo -e "\nYou can now run: laravel new myproject"
  ;;
symfony)
  echo -e "Installing PHP and Symfony...\n"
  install_php
  if [[ $(omarchy-os) == darwin ]]; then
    omarchy-pkg-add symfony-cli
  else
    tool symfony-cli
  fi
  echo -e "\nYou can now run: symfony new --webapp myproject"
  ;;
python)
  echo -e "Installing Python...\n"
  omarchy-pkg-add build-essential linux=only curl linux=only libssl-dev linux=only zlib1g-dev linux=only \
    libbz2-dev linux=only libreadline-dev linux=only libsqlite3-dev linux=only libncurses-dev linux=only \
    xz-utils linux=only tk-dev linux=only libxml2-dev linux=only libxmlsec1-dev linux=only \
    libffi-dev linux=only liblzma-dev linux=only \
    openssl@3 darwin=only readline darwin=only sqlite darwin=only xz darwin=only zlib darwin=only tcl-tk darwin=only
  tool python
  echo -e "\nInstalling uv, pipx and Poetry...\n"
  tool uv
  omarchy-pkg-add pipx
  tool poetry
  # BASED-91: nothing published in the last five days installs, on every
  # Python package manager that can say so
  mkdir -p ~/.config/uv
  grep -q '^exclude-newer' ~/.config/uv/uv.toml 2>/dev/null || echo 'exclude-newer = "5 days"' >>~/.config/uv/uv.toml
  python -m pip config --user set install.uploaded-prior-to P5D
  poetry config solver.min-release-age 5
  ;;
elixir)
  echo -e "Installing Elixir...\n"
  install_elixir
  ;;
phoenix)
  echo -e "Installing Phoenix Framework...\n"
  install_elixir
  mix local.rebar --force
  mix archive.install hex phx_new --force
  echo -e "\nYou can now run: mix phx.new my_app"
  ;;
rust)
  echo -e "Installing Rust...\n"
  omarchy-pkg-add build-essential linux=only curl linux=only
  tool rust
  ;;
java)
  echo -e "Installing Java...\n"
  install_java
  echo -e "\nInstalling Maven...\n"
  tool maven
  ;;
zig)
  echo -e "Installing Zig...\n"
  omarchy-pkg-add python3 linux=only xz-utils linux=only
  tool zig
  tool zls
  ;;
ocaml)
  echo -e "Installing OCaml...\n"
  omarchy-pkg-add build-essential linux=only bubblewrap linux=only m4 linux=only pkg-config linux=only \
    rsync linux=only unzip linux=only pkgconf darwin=only
  tool opam
  opam init --yes
  eval "$(opam env)"
  opam install ocaml-lsp-server odoc ocamlformat utop --yes
  ;;
dotnet)
  echo -e "Installing .NET...\n"
  omarchy-pkg-add libicu-dev linux=only
  tool dotnet
  ;;
clojure)
  echo -e "Installing Clojure...\n"
  omarchy-pkg-add rlwrap
  install_java
  tool clojure
  ;;
scala)
  echo -e "Installing Scala...\n"
  install_java
  tool scala
  tool scala-cli
  ;;
*)
  echo "$usage" >&2
  exit 1
  ;;
esac
