#!/bin/bash
# Server Audit - Run on AWS to see current state
# Usage: ssh server 'bash -s' < ctrl/server/audit.sh

echo "=== SERVER AUDIT ==="
echo "Date: $(date)"
echo "Host: $(hostname)"
echo "User: $USER"
echo ""

# =============================================================================
# Directory Structure
# =============================================================================
echo "=== DIRECTORY STRUCTURE ==="
echo ""
echo "Home directory contents:"
ls -lah ~/
echo ""

echo "core_room structure (if exists):"
if [ -d ~/core_room ]; then
    tree ~/core_room -L 2 -I ".git" 2>/dev/null || find ~/core_room -maxdepth 2 -type d | sort
else
    echo "  ~/core_room does NOT exist"
fi
echo ""

echo "soleprint location:"
if [ -d ~/soleprint ]; then
    ls -lah ~/soleprint/ | head -10
    echo "  ..."
else
    echo "  ~/soleprint does NOT exist"
fi
echo ""

# =============================================================================
# Docker
# =============================================================================
echo "=== DOCKER ==="
echo ""
echo "Docker version:"
docker --version 2>/dev/null || echo "  Docker NOT installed"
echo ""

echo "Docker Compose version:"
docker compose version 2>/dev/null || echo "  Docker Compose NOT installed"
echo ""

echo "Running containers:"
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo "  None or Docker not running"
echo ""

echo "All containers (including stopped):"
docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}" 2>/dev/null | head -20
echo ""

echo "Docker networks:"
docker network ls 2>/dev/null || echo "  None"
echo ""

echo "Docker volumes:"
docker volume ls 2>/dev/null | grep -E "core_room|amar|soleprint|DRIVER" || echo "  No core_room/amar/soleprint volumes"
echo ""

# =============================================================================
# Nginx
# =============================================================================
echo "=== NGINX ==="
echo ""
echo "Nginx version:"
nginx -v 2>&1 || echo "  Nginx NOT installed"
echo ""

echo "Nginx status:"
systemctl status nginx --no-pager -l 2>/dev/null | head -5 || echo "  Cannot check status"
echo ""

echo "Sites enabled:"
ls -lah /etc/nginx/sites-enabled/ 2>/dev/null || echo "  Directory does not exist"
echo ""

echo "Sites available (core_room related):"
ls -lah /etc/nginx/sites-available/ 2>/dev/null | grep -E "room|amar|soleprint|artery|album|ward" || echo "  None found"
echo ""

# =============================================================================
# SSL Certificates
# =============================================================================
echo "=== SSL CERTIFICATES ==="
echo ""
echo "Certbot version:"
certbot --version 2>/dev/null || echo "  Certbot NOT installed"
echo ""

echo "Certificates:"
if [ -d /etc/letsencrypt/live ]; then
    sudo ls -lah /etc/letsencrypt/live/ 2>/dev/null || echo "  Permission denied"
else
    echo "  /etc/letsencrypt/live does NOT exist"
fi
echo ""

# =============================================================================
# Environment Files
# =============================================================================
echo "=== ENVIRONMENT FILES ==="
echo ""
for location in ~/core_room/amar ~/core_room/soleprint ~/soleprint; do
    if [ -d "$location" ]; then
        echo "$location/.env:"
        if [ -f "$location/.env" ]; then
            echo "  EXISTS"
            echo "  Size: $(stat -c%s "$location/.env" 2>/dev/null || stat -f%z "$location/.env" 2>/dev/null) bytes"
            echo "  ROOM_NAME: $(grep "^ROOM_NAME=" "$location/.env" 2>/dev/null || echo "not set")"
            echo "  NETWORK_NAME: $(grep "^NETWORK_NAME=" "$location/.env" 2>/dev/null || echo "not set")"
        else
            echo "  does NOT exist"
        fi

        echo "$location/.env.example:"
        [ -f "$location/.env.example" ] && echo "  EXISTS" || echo "  does NOT exist"
        echo ""
    fi
done

# =============================================================================
# Ports in Use
# =============================================================================
echo "=== PORTS IN USE ==="
echo ""
echo "Listening on ports (3000, 8000, 13000-13003):"
sudo netstat -tlnp 2>/dev/null | grep -E ":3000|:8000|:1300[0-3]" || sudo ss -tlnp | grep -E ":3000|:8000|:1300[0-3]" || echo "  Cannot check (need sudo)"
echo ""

# =============================================================================
# Systemd Services
# =============================================================================
echo "=== SYSTEMD SERVICES ==="
echo ""
echo "Soleprint-related services:"
systemctl list-units --type=service --all 2>/dev/null | grep -E "soleprint|artery|album|ward" || echo "  None found"
echo ""

# =============================================================================
# Disk Usage
# =============================================================================
echo "=== DISK USAGE ==="
echo ""
echo "Overall:"
df -h / 2>/dev/null
echo ""

echo "Docker space:"
docker system df 2>/dev/null || echo "  Docker not available"
echo ""

# =============================================================================
# Summary
# =============================================================================
echo "=== SUMMARY ==="
echo ""
echo "Key Questions:"
echo ""
echo "1. Is there an existing core_room deployment?"
[ -d ~/core_room ] && echo "   YES - ~/core_room exists" || echo "   NO"
echo ""

echo "2. Are Docker containers running?"
docker ps -q 2>/dev/null | wc -l | xargs -I {} echo "   {} containers running"
echo ""

echo "3. Is nginx configured for core_room?"
[ -f /etc/nginx/sites-enabled/core_room.conf ] && echo "   YES - core_room.conf installed" || echo "   NO"
echo ""

echo "4. Are there old individual nginx configs?"
ls /etc/nginx/sites-enabled/ 2>/dev/null | grep -E "amar|soleprint|artery|album|ward" | wc -l | xargs -I {} echo "   {} old configs found"
echo ""

echo "5. SSL certificates present?"
[ -d /etc/letsencrypt/live/room.mcrn.ar ] && echo "   *.room.mcrn.ar: YES" || echo "   *.room.mcrn.ar: NO"
[ -d /etc/letsencrypt/live/mcrn.ar ] && echo "   *.mcrn.ar: YES" || echo "   *.mcrn.ar: NO"
echo ""

echo "=== END AUDIT ==="
