diff --git a/README.md b/README.md index 9c744cd..fdf0877 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,15 @@ HTML_VOLUME=test_html_staging Two important Nginx configuration files are available in the project directory. After editing, perform a restart: ```bash -./default.conf # Automatically generated each time the script runs +./default.conf # generate-compose-nginx.sh respects user added changes ./nginx.conf # Manually customizable configuration ``` -⚠️ **Warning:** `default.conf` is regenerated every time `generate-compose-nginx.sh` runs. -Make a backup before modifying if you plan to change the project name. - --- ## 🚀 Usage -### **Initialize a Project with a Unique `COMPOSE_PROJECT_NAME`** +### **Initialize a Project with a Unique `COMPOSE_PROJECT_NAME`; expects one word** Run the following script to generate the required files: ```bash ./generate-compose-nginx.sh diff --git a/generate-compose-nginx.sh b/generate-compose-nginx.sh index a580f06..1a270d8 100755 --- a/generate-compose-nginx.sh +++ b/generate-compose-nginx.sh @@ -16,4 +16,20 @@ fi export SERVICE_SUFFIX=${COMPOSE_PROJECT_NAME:-main} export COMPOSE_PROJECT_NAME envsubst < docker-compose.template.yml > docker-compose.yml -envsubst '${COMPOSE_PROJECT_NAME}' < default.template.conf > default.conf \ No newline at end of file + +# Define the new value for the database host +export new_wp="wordpress_${COMPOSE_PROJECT_NAME}" + +# If default.conf doesn't exist, create it from the template (using envsubst restricted to COMPOSE_PROJECT_NAME) +if [ ! -f default.conf ]; then + echo "default.conf not found, creating from default.template.conf..." + envsubst '${COMPOSE_PROJECT_NAME}' < default.template.conf > default.conf +fi + +# Check if the existing default.conf contains a database hostname matching the pattern (it may or may not contain the env variable placeholder) +if grep -qE "\bwordpress_\w+(?::\d+)?\b" default.conf; then + # Update in-place only the portion that matches the database host pattern. + # sed -i "s/\bwordpress_\w+(?::\d+)?\b/${new_wp}/g" default.conf + perl -pi -e 's/\bwordpress_\w+(?::(\d+))?\b/$ENV{"new_wp"} . (defined($1) ? ":$1" : "")/eg' default.conf + echo "default.conf updated with COMPOSE_PROJECT_NAME='${COMPOSE_PROJECT_NAME}'." +fi