#!/bin/bash

# Install or update AGY (Antigravity), Ollama, and OpenCode on Debian/Ubuntu.
set -euo pipefail

if [[ "${EUID}" -eq 0 ]]; then
  sudo() { while [[ "$1" == -* ]]; do shift; done; "$@"; }
  export -f sudo
fi

log() {
  printf '==> %s\n' "$1"
}

command_exists() {
  command -v "$1" >/dev/null 2>&1
}

ensure_base_dependencies() {
  log "Refreshing package metadata"
  sudo apt-get update
  sudo apt-get install -y --no-install-recommends ca-certificates curl
}

ensure_path_for_local_binary() {
  local binary_path="$1"
  if [[ -x "${binary_path}" ]]; then
    PATH="${binary_path%/*}:${PATH}"
    export PATH
  fi
}

install_agy() {
  if command_exists agy; then
    log "Updating AGY"
  else
    log "Installing AGY"
  fi

  curl -fsSL https://antigravity.google/cli/install.sh | bash

  ensure_path_for_local_binary "${HOME}/.local/bin/agy"

  if ! command_exists agy && [[ -x "${HOME}/.local/bin/agy" ]]; then
    ensure_path_for_local_binary "${HOME}/.local/bin/agy"
  fi

  if ! command_exists agy; then
    echo "agy command was not available after install."
    exit 1
  fi

  log "AGY ready: $(command -v agy)"
}

install_ollama() {
  if command_exists ollama; then
    log "Updating Ollama"
  else
    log "Installing Ollama"
  fi

  curl -fsSL https://ollama.com/install.sh | sh

  if ! command_exists ollama; then
    echo "ollama command was not available after install."
    exit 1
  fi

  log "Ollama ready: $(command -v ollama)"
}

install_node_runtime() {
  log "Ensuring Node.js current for npm-based installs"

  if command_exists node && command_exists npm; then
    log "Node.js already present; refreshing NodeSource current channel for OpenCode"
  else
    log "Node.js missing; installing from NodeSource current"
  fi

  curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
  sudo apt-get install -y --no-install-recommends nodejs
}

install_opencode() {
  if command_exists opencode || command_exists opencode.exe; then
    log "Updating OpenCode"
  else
    log "Installing OpenCode"
  fi

  install_node_runtime
  sudo npm install -g opencode-ai

  local opencode_binary
  opencode_binary="$(command -v opencode || true)"
  if [[ -z "${opencode_binary}" && command_exists opencode.exe ]]; then
    opencode_binary="$(command -v opencode.exe)"
  fi

  if [[ -z "${opencode_binary}" ]]; then
    local npm_prefix
    npm_prefix="$(npm config get prefix)"
    if [[ -n "${npm_prefix}" && -x "${npm_prefix}/bin/opencode" ]]; then
      opencode_binary="${npm_prefix}/bin/opencode"
    fi
  fi

  if [[ -z "${opencode_binary}" ]]; then
    echo "OpenCode binary was not available after install."
    exit 1
  fi

  log "OpenCode ready: ${opencode_binary}"
}

persist_path_hints() {
  local hint='export PATH="$HOME/.local/bin:$PATH"'
  if [[ -f "${HOME}/.bashrc" ]] && ! grep -qF "${hint}" "${HOME}/.bashrc"; then
    echo "${hint}" >> "${HOME}/.bashrc"
  fi
  if [[ -f "${HOME}/.zshrc" ]] && ! grep -qF "${hint}" "${HOME}/.zshrc"; then
    echo "${hint}" >> "${HOME}/.zshrc"
  fi
}

ensure_base_dependencies
install_agy
install_ollama
install_opencode
persist_path_hints

log "AGY, Ollama, and OpenCode are installed and refreshed."
