#!/bin/bash
# ==========================================
# HX-KI INVENTORY SCANNER – UNIVERSAL V1
# Läuft auf jedem HX-KI Server (Helsinki / Nürnberg / Falkenstein)
# ==========================================

set -euo pipefail

TIMESTAMP_UTC="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
HOSTNAME="$(hostname)"
OUT_DIR="/opt/hx-ki/inventory"
RUN_ID="$(date +"%Y%m%d-%H%M%S")"
OUT_FILE="${OUT_DIR}/hxki_inventory_${HOSTNAME}_${RUN_ID}.log"

mkdir -p "${OUT_DIR}"

# Alles in Datei + Konsole schreiben
exec > >(tee "${OUT_FILE}") 2>&1

echo "HX-KI INVENTAR – ${HOSTNAME}"
echo "========================================================"
echo "Timestamp (UTC): ${TIMESTAMP_UTC}"
echo

section() {
  echo
  echo "---- $1 ----"
}

# -------------------------------
# SERVER INFORMATION
# -------------------------------
section "SERVER INFORMATION"

if command -v hostnamectl >/dev/null 2>&1; then
  hostnamectl
else
  echo "Hostname: ${HOSTNAME}"
  echo "Kernel: $(uname -srmo)"
  if [ -f /etc/os-release ]; then
    echo
    echo "/etc/os-release:"
    cat /etc/os-release
  fi
fi

# -------------------------------
# CPU / RAM
# -------------------------------
section "CPU"

if command -v lscpu >/dev/null 2>&1; then
  lscpu | egrep 'Model name|CPU\(s\)|Thread|Core|Socket|MHz' || lscpu
else
  echo "lscpu nicht gefunden."
  echo "uname -m: $(uname -m)"
fi

section "RAM"

if command -v free >/dev/null 2>&1; then
  free -h
else
  echo "free nicht gefunden."
fi

# -------------------------------
# STORAGE
# -------------------------------
section "STORAGE"

df -h /

# -------------------------------
# DOCKER – GENERAL STATUS
# -------------------------------
section "DOCKER – GENERAL STATUS"

if command -v docker >/dev/null 2>&1; then
  echo "Docker Version:"
  docker version || echo "Docker Version konnte nicht abgefragt werden."

  echo
  echo "Docker Compose Version:"
  if command -v docker compose >/dev/null 2>&1; then
    docker compose version || echo "docker compose version fehlgeschlagen."
  elif command -v docker-compose >/dev/null 2>&1; then
    docker-compose version || echo "docker-compose version fehlgeschlagen."
  else
    echo "Keine docker compose / docker-compose Binary gefunden."
  fi

  echo
  echo "Docker Container (running):"
  docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}" || echo "docker ps fehlgeschlagen."

  echo
  echo "Docker Container (all):"
  docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}" || echo "docker ps -a fehlgeschlagen."

  echo
  echo "Docker Networks:"
  docker network ls || echo "docker network ls fehlgeschlagen."

  echo
  echo "Docker Volumes:"
  docker volume ls || echo "docker volume ls fehlgeschlagen."

else
  echo "Docker ist nicht installiert oder nicht im PATH."
fi

# -------------------------------
# DOCKER COMPOSE STACKS /opt/hx-ki/docker
# -------------------------------
section "DOCKER COMPOSE STACKS (/opt/hx-ki/docker)"

if [ -d /opt/hx-ki/docker ]; then
  for STACK_DIR in /opt/hx-ki/docker/*; do
    if [ -d "${STACK_DIR}" ] && [ -f "${STACK_DIR}/docker-compose.yml" ]; then
      STACK_NAME="$(basename "${STACK_DIR}")"
      echo
      echo "== Stack: ${STACK_NAME} =="
      echo "Pfad: ${STACK_DIR}"

      # Versuche docker compose, fallback auf docker-compose
      if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
        (
          cd "${STACK_DIR}"
          echo "Services (docker compose config --services):"
          docker compose config --services 2>/dev/null || echo "docker compose config fehlgeschlagen."
        )
      elif command -v docker-compose >/dev/null 2>&1; then
        (
          cd "${STACK_DIR}"
          echo "Services (docker-compose config --services):"
          docker-compose config --services 2>/dev/null || echo "docker-compose config fehlgeschlagen."
        )
      else
        echo "Keine docker compose / docker-compose Binary verfügbar."
      fi
    fi
  done
else
  echo "/opt/hx-ki/docker existiert nicht auf diesem Server."
fi

# -------------------------------
# /opt/hx-ki – DIRECTORY STRUCTURE
# -------------------------------
section "/opt/hx-ki – DIRECTORY STRUCTURE (max depth 2)"

if [ -d /opt/hx-ki ]; then
  if command -v tree >/dev/null 2>&1; then
    tree -L 2 /opt/hx-ki
  else
    echo "tree nicht installiert – verwende find:"
    find /opt/hx-ki -maxdepth 2 -mindepth 1 -type d | sort
  fi
else
  echo "/opt/hx-ki existiert nicht auf diesem Server."
fi

# -------------------------------
# PYTHON ENVS UNTER /opt/hx-ki
# -------------------------------
section "PYTHON ENVS UNTER /opt/hx-ki"

for VENV in /opt/hx-ki/env /opt/hx-ki/venv; do
  if [ -d "${VENV}" ]; then
    echo
    echo "Virtualenv: ${VENV}"
    if [ -x "${VENV}/bin/python" ]; then
      "${VENV}/bin/python" --version 2>&1 || true
    fi
    if [ -x "${VENV}/bin/pip" ]; then
      echo "Installierte Pakete (Top 40):"
      "${VENV}/bin/pip" list 2>/dev/null | head -n 40 || true
    fi
  fi
done

# -------------------------------
# SYSTEM PYTHON
# -------------------------------
section "SYSTEM PYTHON"

if command -v python3 >/dev/null 2>&1; then
  echo "python3 Version:"
  python3 --version 2>&1 || true
else
  echo "python3 nicht gefunden."
fi

section "FERTIG"
echo "Inventar-Datei: ${OUT_FILE}"
