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

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>
        <p>Webserver: Caddy 🔥</p>
    </div>
</body>
</html>
EOF

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

echo "✓ Test HTML geplaatst"
echo ""

echo "=========================================="
echo "BLOK 3: Backup Caddyfile"
echo "=========================================="

# Backup maken van huidige config
sudo cp /etc/caddy/Caddyfile /etc/caddy/Caddyfile.backup.$(date +%Y%m%d_%H%M%S)

echo "✓ Backup gemaakt"
echo ""

echo "=========================================="
echo "BLOK 4: Nieuwe Caddyfile aanmaken"
echo "=========================================="

# Nieuwe Caddyfile met BEIDE subdomeinen
sudo tee /etc/caddy/Caddyfile > /dev/null <<'EOF'
# Zaiko subdomain (BESTAAND - NIET AANPASSEN)
zaiko.baro-edegem.com {
    encode gzip zstd

    @api path_regexp apiprefix ^/api/
    reverse_proxy @api 127.0.0.1:8000

    handle_path /web/* {
      root * /home/baro/app/web
      uri strip_prefix /web
      try_files {path} {path}/
    header * {
        Cache-Control "no-cache, no-store, must-revalidate"
        Pragma "no-cache"
        Expires "0"
    }
      file_server
    }

    root * /home/baro/app/web
    try_files {path} /web/index.html
    file_server
}

# Test subdomain (NIEUW)
test.baro-edegem.com {
    encode gzip zstd
    
    root * /var/www/testbaro
    file_server
    
    # Security headers
    header {
        X-Frame-Options "SAMEORIGIN"
        X-Content-Type-Options "nosniff"
        X-XSS-Protection "1; mode=block"
    }
    
    # Logs
    log {
        output file /var/log/caddy/test.baro-edegem.com.log
    }
}
EOF

echo "✓ Caddyfile bijgewerkt met test.baro-edegem.com"
echo ""

echo "=========================================="
echo "BLOK 5: Caddy config valideren"
echo "=========================================="

# Test caddy config
echo "Testing Caddy configuratie..."
sudo caddy validate --config /etc/caddy/Caddyfile

echo "✓ Caddy config is geldig"
echo ""

echo "=========================================="
echo "BLOK 6: Caddy reload"
echo "=========================================="

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

echo "✓ Caddy succesvol gereload"
echo ""

echo "=========================================="
echo "BLOK 7: DNS verificatie"
echo "=========================================="

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

echo ""

echo "=========================================="
echo "BLOK 8: HTTP test"
echo "=========================================="

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

echo ""

echo "=========================================="
echo "BLOK 9: Zaiko verificatie"
echo "=========================================="

echo "Verifiëren dat zaiko.baro-edegem.com nog werkt..."
curl -I http://zaiko.baro-edegem.com | head -5

echo ""

echo "=========================================="
echo "SETUP COMPLEET!"
echo "=========================================="
echo ""
echo "✅ Je test site is nu live op: http://test.baro-edegem.com"
echo "✅ Zaiko blijft werken op: http://zaiko.baro-edegem.com"
echo "✅ Caddy regelt automatisch HTTPS (Let's Encrypt)"
echo ""
echo "Test nu in je browser:"
echo "  http://test.baro-edegem.com (wordt auto-redirect naar HTTPS)"
echo "  https://test.baro-edegem.com"
echo ""
echo "Backup van oude config:"
echo "  ls -la /etc/caddy/Caddyfile.backup.*"
echo ""
echo "Logs checken:"
echo "  sudo tail -f /var/log/caddy/test.baro-edegem.com.log"
echo "  sudo journalctl -u caddy -f"
echo ""
