#!/bin/bash

if [ $# -lt 2 ]; then
    echo "Usage: $0 <domain> <foldername>"
    exit 1
fi

DOMAIN="$1"
FOLDERNAME="$2"

if [[ ! "$DOMAIN" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    echo "Invalid domain format. Please provide a valid domain without 'www', e.g., example.com"
    exit 1
fi

if [ ! -f "proxy-domain.conf" ]; then
    echo "Error: proxy-domain.conf not found in the current directory."
    exit 1
fi

if [ ! -d "/etc/apache2/sites-available" ]; then
    echo "Error: Apache sites-available directory not found."
    exit 1
fi

# Replace "000" with the provided folder name in the virtual host configuration file
    sudo cp "./proxy-domain.conf" "/etc/apache2/sites-available/${DOMAIN}-default.conf"
 sudo sed -i "s|domain|${DOMAIN}|g; s|foldername|${FOLDERNAME}|g" "/etc/apache2/sites-available/${DOMAIN}-default.conf"


sudo systemctl reload apache2

echo "Apache virtual host configured for '$DOMAIN' in folder '$FOLDERNAME'."

