#!/bin/bash
# Test subdomain setup script voor test.baro-edegem.com
# Auteur: Senior DevOps Engineer
# Datum: 2025-12-29
# Server: Ubuntu 24.04

set -e  # Stop bij eerste error

echo "=========================================="
echo "BLOK 1: Map aanmaken en permissions"
echo "=========================================="

# Map aanmaken voor test website
sudo mkdir -p /var/www/testbaro

# Ownership naar user baro
sudo chown -R baro:baro /var/www/testbaro

# Permissions: 755 voor directories, 644 voor files
sudo chmod 755 /var/www/testbaro

echo "✓ Map /var/www/testbaro aangemaakt"
echo ""

echo "=========================================="
echo "BLOK 2: Test HTML plaatsen"
echo "=========================================="

# Simpele test pagina
cat > /var/www/testbaro/index.html <<'EOF'
<!DOCTYPE html>
<html lang="nl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Baro Edegem - Werkt!</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        .container {
            text-align: center;
            padding: 2rem;
            background: rgba(0,0,0,0.3);
            border-radius: 10px;
        }
        h1 { margin: 0 0 1rem 0; font-size: 3rem; }
        p { font-size: 1.2rem; margin: 0.5rem 0; }
        .status { color: #4ade80; font-weight: bold; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 Test Subdomain Actief!</h1>
        <p class="status">✓ test.baro-edegem.com draait succesvol</p>
        <p>Server: Ubuntu 24.04 (Hetzner)</p>
        <p>IP: 5.75.159.94</p>
        <p>User: baro</p>
    </div>
</body>
</html>
EOF

chmod 644 /var/www/testbaro/index.html

echo "✓ Test HTML geplaatst"
echo ""

echo "=========================================="
echo "BLOK 3: Nginx config aanmaken"
echo "=========================================="

# Check of sites-available structuur bestaat
if [ -d "/etc/nginx/sites-available" ]; then
    CONFIG_PATH="/etc/nginx/sites-available/test.baro-edegem.com"
    ENABLE_PATH="/etc/nginx/sites-enabled/test.baro-edegem.com"
    USE_SITES_STRUCTURE=true
else
    CONFIG_PATH="/etc/nginx/conf.d/test.baro-edegem.com.conf"
    USE_SITES_STRUCTURE=false
fi

# Nginx server block
sudo tee $CONFIG_PATH > /dev/null <<'EOF'
# Test subdomain: test.baro-edegem.com
# Aangemaakt: 2025-12-29
# NIET VERWIJDEREN: Dit is een test site naast zaiko.baro-edegem.com

server {
    listen 80;
    listen [::]:80;

    server_name test.baro-edegem.com;

    root /var/www/testbaro;
    index index.html index.htm;

    # Access en error logs
    access_log /var/log/nginx/test.baro-edegem.com.access.log;
    error_log /var/log/nginx/test.baro-edegem.com.error.log;

    # Try files
    location / {
        try_files $uri $uri/ =404;
    }

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Disable .ht access
    location ~ /\.ht {
        deny all;
    }
}
EOF

echo "✓ Nginx config aangemaakt: $CONFIG_PATH"

# Enable site als sites-available/sites-enabled structuur bestaat
if [ "$USE_SITES_STRUCTURE" = true ]; then
    sudo ln -sf $CONFIG_PATH $ENABLE_PATH
    echo "✓ Site geactiveerd via symlink"
fi

echo ""

echo "=========================================="
echo "BLOK 4: Nginx test en reload"
echo "=========================================="

# Test nginx config
echo "Testing nginx configuratie..."
sudo nginx -t

# Reload (geen restart voor minimale downtime)
echo "Reloading nginx..."
sudo systemctl reload nginx

echo "✓ Nginx succesvol gereload"
echo ""

echo "=========================================="
echo "BLOK 5: DNS verificatie"
echo "=========================================="

echo "Checking DNS voor test.baro-edegem.com..."
dig +short test.baro-edegem.com

echo ""

echo "=========================================="
echo "BLOK 6: HTTP test"
echo "=========================================="

echo "Testing HTTP respons..."
sleep 2  # Even wachten na nginx reload
curl -I http://test.baro-edegem.com

echo ""

echo "=========================================="
echo "SETUP COMPLEET!"
echo "=========================================="
echo ""
echo "Je test site is nu live op: http://test.baro-edegem.com"
echo ""
echo "Volgende stappen:"
echo "1. Open http://test.baro-edegem.com in je browser"
echo "2. Run HTTPS setup script voor SSL: ./test-baro-https.sh"
echo ""
echo "Logs checken:"
echo "  sudo tail -f /var/log/nginx/test.baro-edegem.com.access.log"
echo "  sudo tail -f /var/log/nginx/test.baro-edegem.com.error.log"
echo ""
