#!/usr/bin/env bash
set -euo pipefail

need_cmd() {
  command -v "$1" >/dev/null 2>&1 || {
    echo "missing required command: $1" >&2
    return 1
  }
}

need_one() {
  local ok=1
  for cmd in "$@"; do
    if command -v "$cmd" >/dev/null 2>&1; then
      ok=0
      break
    fi
  done
  if [[ "$ok" -ne 0 ]]; then
    echo "missing one of required commands: $*" >&2
    return 1
  fi
}

echo "checking base prerequisites"
need_cmd git
need_cmd curl
need_cmd python3
need_cmd tmux
need_one node nodejs

echo "ensuring ~/.local/bin exists"
mkdir -p "$HOME/.local/bin"

if ! command -v bun >/dev/null 2>&1; then
  echo "installing bun"
  curl -fsSL https://bun.sh/install | bash
fi

ln -sf "$HOME/.bun/bin/bun" "$HOME/.local/bin/bun"
ln -sf "$HOME/.bun/bin/bunx" "$HOME/.local/bin/bunx"

echo "bun=$(command -v bun)"
bun --version

echo "prereqs ok"
