#!/usr/bin/env bash
# tools/cmd/railiance-doctor — workstation & provisioning toolchain checks
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source "${ROOT}/lib/railiance-print.sh"

ver() {
  local cmd="$1" out=""
  if command -v "$cmd" >/dev/null 2>&1; then
    out="$("$cmd" --version 2>/dev/null || "$cmd" -V 2>/dev/null || "$cmd" version 2>/dev/null || true)"
    echo "$out" | head -n1
  else
    echo ""
  fi
}
have(){ command -v "$1" >/dev/null 2>&1; }

OP_REQ=(curl git jq python3 ssh)
PROV_REQ=(ansible)
PROV_OPT=(age docker helm kubectl kustomize podman rsync scp sops tar yq)

missing_req=0

print_hdr "Operator toolchain (required on your workstation)"
for cmd in "${OP_REQ[@]}"; do
  if have "$cmd"; then ok "$cmd" "$(ver "$cmd")"; else bad "$cmd" "missing"; missing_req=1; fi
done

print_hdr "Provisioning toolchain"
for cmd in "${PROV_REQ[@]}"; do
  if have "$cmd"; then ok "$cmd" "$(ver "$cmd")"; else bad "$cmd" "missing (needed to run playbooks)"; missing_req=1; fi
done
for cmd in "${PROV_OPT[@]}"; do
  if have "$cmd"; then ok "$cmd" "$(ver "$cmd")"; else warn "$cmd" "not installed (only needed if you use related features)"; fi
done

cat <<'NOTE'

Notes:
- Operator tools are needed on your local machine to drive Railiance.
- Provisioning:
  • ansible — required to bootstrap hosts
  • kubectl/helm/kustomize — needed once Kubernetes is up
  • sops/age — for managing encrypted secrets (recommended)
  • docker or podman — only if building images locally
  • yq — handy for YAML manipulation in scripts
  • rsync/scp/tar — file transfer & packaging utilities

TIP: On Debian/Ubuntu install basics with:
  sudo apt-get update && sudo apt-get install -y \
    curl git jq python3 openssh-client ansible rsync tar
NOTE

exit $missing_req
