#!/bin/bash
# Prints this VM's connection card, then a few tips about the box. lima-ai
# installs it into ~/.local/bin of every workspace VM, runs it at the end of
# `lima-ai new`, and the bash prompt runs it once per shell session; it is the
# one place that formats these details.
set -euo pipefail

# The host runs this through a non-interactive shell, where ~/.bashrc has not
# put the per-user tool directories on PATH; add them so the tool list does
# not depend on how the script was started.
PATH="$HOME/.local/bin:$HOME/.cargo/bin:$HOME/.local/share/fnm/aliases/default/bin:$PATH"

file="$HOME/.config/lima-ai/instance"
if [ ! -f "$file" ]; then
  echo "lima-ai-info: no instance file; run lima-ai sync-claude from the host" >&2
  exit 1
fi
. "$file"

fqdn="$(hostname).local"
ip="$( (ip -4 -o addr show lima0 2>/dev/null || true) | sed -nE 's/.*inet ([0-9.]+)\/.*/\1/p' | head -n 1)"
ip="${ip:-unknown}"

# The working copy counts as a repository only when it is the top level of
# one: git would otherwise walk up and find a repository above it.
workdir="$HOME/$LIMA_AI_WORKDIR"
if prefix="$(git -C "$workdir" rev-parse --show-prefix 2>/dev/null)" && [ -z "$prefix" ]; then
  branch="$(git -C "$workdir" symbolic-ref -q --short HEAD || echo '(detached HEAD)')"
else
  branch="no git repository"
fi

title="LIMA-AI - Instance Details"
rows=(
  "Project:    $LIMA_AI_PROJECT"
  "Workspace:  $LIMA_AI_WORKSPACE"
  "FQDN:       $fqdn"
  "IP:         $ip"
  "Created at: $LIMA_AI_CREATED_AT"
  "Git branch: $branch"
)

width=${#title}
for row in "${rows[@]}"; do
  if [ "${#row}" -gt "$width" ]; then width=${#row}; fi
done
edge="+$(printf '%*s' "$((width + 2))" '' | tr ' ' '-')+"

# printf's %-*s pads by bytes while ${#row} counts characters, so a row with
# accented letters would come out short; pad by the character count instead.
line() {
  printf '| %s%*s |\n' "$1" "$((width - ${#1}))" ''
}

echo "$edge"
line "$title"
echo "$edge"
for row in "${rows[@]}"; do
  line "$row"
done
echo "$edge"

# --- tips ---------------------------------------------------------------------

os_name="$( (. /etc/os-release 2>/dev/null && printf '%s %s' "$NAME" "${VERSION_ID:-}") || echo Ubuntu)"
os_name="${os_name% }"

# The tools the base image provisions, in the order they are listed; only the
# ones present are named, since older VMs came from a smaller base.
tools=()
for tool in docker git lazygit tmux rg fzf jq node npm cargo claude rtk docker-backup aws session-manager-plugin; do
  command -v "$tool" >/dev/null 2>&1 || continue
  if [ "$tool" = node ]; then
    major="$(node -v 2>/dev/null | sed -nE 's/^v?([0-9]+).*/\1/p')"
    tool="node${major:+ $major}"
  fi
  tools+=("$tool")
done
installed="$(IFS=,; echo "${tools[*]}")"
installed="${installed//,/, }"

echo
echo "* This is an ${os_name} box. Install whatever you want; Claude may install things here too."
echo "* Run \`cc\` for Claude Code with permissions skipped (claude --dangerously-skip-permissions)."
echo "* Installed here: ${installed:-nothing from the base image yet}."
echo "* rtk trims the command output Claude reads. See what it saved with \`rtk gain\`."
echo "* Print this again with \`lima-ai-info\`."
