#!/usr/bin/env bash
# Rhinobase Next host-readiness check (read only).
# Copyright (c) 2026 Rhinobase Next project. All rights reserved.
set -u
say(){ printf '%-27s %s\n' "$1" "$2"; }
warn(){ printf 'WARNING: %s\n' "$*"; }
if [[ -r /etc/os-release ]]; then
  # shellcheck source=/etc/os-release
  . /etc/os-release
else
  echo 'ERROR: /etc/os-release not available.' >&2
  exit 1
fi
printf '\nRhinobase Next — Ubuntu 24.04 Host Check\n'
printf '=========================================\n'
say 'Detected OS:' "${PRETTY_NAME:-unknown}"
say 'Architecture:' "$(uname -m)"
status=0
if [[ "${ID:-}" != ubuntu || "${VERSION_ID:-}" != 24.04 ]]; then warn 'This installer framework targets Ubuntu 24.04 LTS only.'; status=1; fi
if [[ "$(uname -m)" != x86_64 ]]; then warn 'This release framework currently targets x86_64/amd64 only.'; status=1; fi
if command -v free >/dev/null 2>&1; then
  total_mb=$(free -m | awk '/^Mem:/ {print $2}')
  say 'Physical RAM:' "${total_mb:-unknown} MiB"
  [[ ${total_mb:-0} -lt 4096 ]] && warn 'Less than provisional 4 GiB planning baseline; assess capacity.'
fi
if command -v df >/dev/null 2>&1; then
  target=/; [[ -d /opt ]] && target=/opt
  available_kb=$(df -Pk "$target" | awk 'NR==2 {print $4}')
  if [[ "$available_kb" =~ ^[0-9]+$ ]]; then
    say 'Free disk on /opt FS:' "$((available_kb / 1024)) MiB"
    ((available_kb < 40*1024*1024)) && warn 'Less than provisional 40 GiB planning baseline; images and backup need separate capacity.'
  fi
fi
if command -v docker >/dev/null 2>&1; then
  say 'Docker Engine:' "$(docker --version 2>/dev/null || echo installed)"
  if docker compose version >/dev/null 2>&1; then
    say 'Docker Compose:' "$(docker compose version --short 2>/dev/null)"
  else warn 'Docker Compose plugin not detected; see official Docker Ubuntu instructions.'; fi
else
  warn 'Docker Engine not installed. Follow https://docs.docker.com/engine/install/ubuntu/'
fi
if command -v ss >/dev/null 2>&1; then
  echo
  echo 'Listening TCP ports (may include unrelated services):'
  ss -H -ltn 2>/dev/null | awk '{print $4}' | sort -u | sed -n '1,25p'
fi
printf '\nThis script made no changes to the server.\n'
printf 'Planning thresholds are not application performance measurements.\n'
exit "$status"
