#!/bin/bash

###############################################################################
# Step 6: Create Apache Virtual Host for Ninanolte
# Run this on: NEW server (vps2.scala4.com)
###############################################################################

set -e

echo "=========================================="
echo "Creating Apache Virtual Host for Ninanolte"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "ERROR: Please run as root (use sudo)"
    exit 1
fi

# Create virtual host
echo "Creating virtual host configuration..."
cat > /etc/apache2/sites-available/ninanolte.conf << 'EOF'
<VirtualHost *:80>
    ServerName ninanolte.com
    ServerAlias www.ninanolte.com
    DocumentRoot /var/www/html/ninanolte/wordpress
    
    <Directory /var/www/html/ninanolte/wordpress>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/ninanolte-error.log
    CustomLog ${APACHE_LOG_DIR}/ninanolte-access.log combined
</VirtualHost>
EOF

echo "✓ Virtual host created"

# Enable the site
echo "Enabling site..."
a2ensite ninanolte.conf

# Test Apache configuration
echo "Testing Apache configuration..."
if apache2ctl configtest; then
    echo "✓ Apache configuration is valid"
else
    echo "✗ Apache configuration has errors"
    exit 1
fi

# Reload Apache
echo "Reloading Apache..."
systemctl reload apache2

# Verify
echo ""
echo "Verifying virtual host..."
apache2ctl -S | grep -i ninanolte || echo "Site enabled (check with: apache2ctl -S)"

echo ""
echo "=========================================="
echo "Virtual Host Created!"
echo "=========================================="
echo ""
echo "Access the site at:"
echo "  http://135.181.153.129/"
echo "  http://ninanolte.com (if DNS is configured)"
echo ""


