#!/bin/bash
# Infoscreen Hardware Test Setup für Quad-Core 16GB System

echo "🖥️  Infoscreen Hardware Test Setup"
echo "=================================="
echo "System: Quad-Core, 16GB RAM, SSD"
echo ""

# System-Info anzeigen
echo "📊 System-Information:"
echo "CPU Cores: $(nproc)"
echo "RAM Total: $(free -h | grep Mem | awk '{print $2}')"
echo "Disk Free: $(df -h / | tail -1 | awk '{print $4}')"
echo ""

# Docker-Setup
echo "🐳 Docker-Setup..."
sudo apt update -y
sudo apt install -y docker.io docker-compose-plugin
sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker $USER

# Test-Verzeichnisse erstellen
echo "📁 Test-Umgebung erstellen..."
mkdir -p ~/infoscreen-hardware-test/{prod,dev,monitoring,scripts,backups}

# Performance-Monitoring-Tools
echo "📊 Monitoring-Tools installieren..."
sudo apt install -y htop iotop nethogs ncdu stress-ng

# Test-Script erstellen
cat > ~/infoscreen-hardware-test/scripts/system-monitor.sh << 'EOF'
#!/bin/bash
# System-Monitoring während Tests

echo "=== Infoscreen System Monitor ==="
echo "Zeit: $(date)"
echo ""

echo "🖥️  CPU-Info:"
echo "Load: $(uptime | awk -F'load average:' '{print $2}')"
echo "Cores: $(nproc) | Usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)%"

echo ""
echo "💾 Memory-Info:"
free -h

echo ""
echo "💿 Disk-Info:"
df -h /

echo ""
echo "🐳 Docker-Info:"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

echo ""
echo "🌡️  System-Temperature (falls verfügbar):"
sensors 2>/dev/null || echo "lm-sensors nicht installiert"

echo ""
echo "🌐 Network-Connections:"
ss -tuln | grep :80\\\|:443\\\|:8000\\\|:3306\\\|:1883
EOF

chmod +x ~/infoscreen-hardware-test/scripts/system-monitor.sh

# Load-Test-Script erstellen
cat > ~/infoscreen-hardware-test/scripts/load-test.sh << 'EOF'
#!/bin/bash
# Load-Test für Infoscreen-System

echo "🔥 Infoscreen Load-Test startet..."

# CPU-Load erzeugen (für Thermal-Tests)
echo "CPU-Stress-Test (30s)..."
stress-ng --cpu $(nproc) --timeout 30s &

# Memory-Test
echo "Memory-Stress-Test..."
stress-ng --vm 2 --vm-bytes 2G --timeout 30s &

# Disk-I/O-Test
echo "Disk-I/O-Test..."
stress-ng --hdd 1 --hdd-bytes 1G --timeout 30s &

# Warten auf Tests
wait

echo "✅ Load-Test abgeschlossen"
EOF

chmod +x ~/infoscreen-hardware-test/scripts/load-test.sh

# Docker-Test-Setup
echo "🧪 Docker-Test-Setup..."
cat > ~/infoscreen-hardware-test/docker-compose.test.yml << 'EOF'
version: '3.8'

services:
  test-web:
    image: nginx:alpine
    ports: ["8080:80"]
    deploy:
      resources:
        limits:
          memory: 256M
        reservations:
          memory: 128M

  test-db:
    image: mariadb:11.2
    environment:
      MYSQL_ROOT_PASSWORD: test123
      MYSQL_DATABASE: testdb
    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M

  test-load:
    image: alpine
    command: sh -c "while true; do wget -q -O- http://test-web/ > /dev/null; sleep 0.1; done"
    depends_on: [test-web]
EOF

echo ""
echo "✅ Setup abgeschlossen!"
echo ""
echo "🚀 Nächste Schritte:"
echo "1. Logout/Login für Docker-Gruppe"
echo "2. Test: docker run hello-world"
echo "3. System-Monitor: ~/infoscreen-hardware-test/scripts/system-monitor.sh"
echo "4. Load-Test: ~/infoscreen-hardware-test/scripts/load-test.sh"
echo "5. Docker-Test: cd ~/infoscreen-hardware-test && docker compose -f docker-compose.test.yml up"
echo ""
echo "📁 Test-Verzeichnis: ~/infoscreen-hardware-test/"
echo "📊 Monitoring: Führen Sie system-monitor.sh parallel zu Tests aus"
