#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=scripts/common.sh
source "$SCRIPT_DIR/scripts/common.sh"

BRIEF=0
while [[ $# -gt 0 ]]; do
  case "$1" in
    --brief)
      BRIEF=1
      shift
      ;;
    -h|--help)
      echo "Usage: ./status.sh [--brief]"
      exit 0
      ;;
    *)
      die "unknown option: $1"
      ;;
  esac
done

load_env
echo "installer_root=$INSTALLER_ROOT"
echo "host_data_dir=$HOST_DATA_DIR"
echo "image=$RPKI_IMAGE"
echo "platform=$RPKI_PLATFORM"
echo "rirs=${RIRS:-}"
echo "max_runs=${MAX_RUNS:-}"
echo "interval_secs=${INTERVAL_SECS:-}"
echo "periodic_snapshot_reset=${PERIODIC_SNAPSHOT_RESET:-0}"
echo "periodic_snapshot_max_deltas=${PERIODIC_SNAPSHOT_MAX_DELTAS:-100}"
echo
if command -v docker >/dev/null 2>&1; then
  docker version --format 'docker={{.Server.Version}}' 2>/dev/null || echo "docker=unavailable"
  docker compose version 2>/dev/null || true
  compose_cmd --profile core --profile sidecar --profile monitor ps || true
else
  echo "docker=missing"
fi
echo
df -h "$HOST_DATA_DIR" 2>/dev/null || true
echo
latest="$(latest_run_dir || true)"
if [[ -n "$latest" ]]; then
  echo "latest_run=$latest"
  print_run_summary "$latest" || true
else
  echo "latest_run=none"
fi
if [[ "$BRIEF" == "0" ]]; then
  echo
  endpoint_ok "http://127.0.0.1:${METRICS_PORT:-9556}/metrics" && echo "metrics=ok" || echo "metrics=unavailable"
  endpoint_ok "http://127.0.0.1:${PROMETHEUS_PORT:-9090}/-/ready" && echo "prometheus=ok" || echo "prometheus=unavailable"
  endpoint_ok "http://127.0.0.1:${GRAFANA_PORT:-3000}/api/health" && echo "grafana=ok" || echo "grafana=unavailable"
fi
