#!/bin/bash

# --- Configuration ---
EMAIL="amin.uzan@gmail.com"
WEB_ROOT="/var/www/html"

# Ensure the script is run with sudo
if [ "$EUID" -ne 0 ]; then 
  echo "Please run as root or with sudo"
  exit
fi

show_menu() {
    echo ""
    echo "===================================="
    echo "   WORDPRESS PORTABLE INSTALLER"
    echo "===================================="
    echo "1. Create WP Portable Site"
    echo "2. Delete WP Portable Site"
    echo "3. Exit"
    echo -n "Select an option [1-3]: "
}

create_wp() {
    echo -n "Enter Blog URL (e.g., blog.example.com): "
    read servn

    if [ -z "$servn" ]; then
        echo "Error: URL cannot be empty."
        return
    fi

    basedir="$WEB_ROOT/$servn"

    echo -n "Install Let's Encrypt SSL? (y/n): "
    read install_ssl

    # 1. Setup Directory and Download
    echo "Creating directory: $basedir"
    mkdir -p "$basedir"
    
    echo "Downloading WordPress Installer..."
    wget -q https://wepe.cdnweb.info/wp_v13.zip -O "$basedir/wp.zip"
    
    if [ ! -f "$basedir/wp.zip" ]; then
        echo "Error: Download failed!"
        return
    fi

    echo "Extracting files..."
    unzip -q "$basedir/wp.zip" -d "$basedir"
    rm "$basedir/wp.zip"

    # 2. Set Secure Permissions
    echo "Setting ownership to www-data and applying 755/644 permissions..."
    chown -R www-data:www-data "$basedir"
    find "$basedir" -type d -exec chmod 755 {} \;
    find "$basedir" -type f -exec chmod 644 {} \;

    # 3. Create Virtual Host
    echo "Creating Virtual Host configuration..."
    vhost_file="/etc/apache2/sites-available/$servn.conf"

    cat > "$vhost_file" <<EOF
<VirtualHost *:80>
    DocumentRoot $basedir
    ServerName $servn
    ServerAlias $servn

    <Directory $basedir>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/apache2/${servn}-error_log
    CustomLog /var/log/apache2/${servn}-access_log common
</VirtualHost>
EOF

    # 4. Activation
    echo "Activating site..."
    a2ensite "$servn.conf"
    systemctl restart apache2

    if [ $? -eq 0 ]; then
        echo "-- Virtual host for $servn created successfully!"
    else
        echo "-- FAIL to create Virtual host for $servn"
        return
    fi

    # 5. SSL Activation
    if [[ "$install_ssl" =~ ^([yY][eE][sS]|[yY])$ ]]; then
        echo "SSL Activation..."
        certbot --apache -d "$servn" --agree-tos -m "$EMAIL" --no-eff-email --redirect
        echo "SSL Setup Done."
        systemctl restart apache2
    else
        echo "Skipping SSL. Site will run on HTTP (Port 80)."
    fi

    echo "DONE! Your site is ready at: http://$servn"
}

delete_wp() {
    echo -n "Enter the Blog URL/Directory name to DELETE: "
    read servn

    if [ -z "$servn" ]; then
        echo "Operation cancelled."
        return
    fi

    echo "Disabling site..."
    a2dissite "$servn.conf"
    
    echo "Removing configuration and files..."
    rm -f "/etc/apache2/sites-available/$servn.conf"
    rm -rf "$WEB_ROOT/$servn"
    
    systemctl restart apache2
    echo "Site $servn has been removed."
}

# --- Main Execution Loop ---
while true; do
    show_menu
    read opt
    case $opt in
        1) create_wp ;;
        2) delete_wp ;;
        3) echo "Goodbye!"; exit 0 ;;
        *) echo "Invalid option." ;;
    esac
done