#!/usr/bin/env bash set -euo pipefail if [[ $# -lt 1 || $# -gt 2 ]]; then echo "Usage: $0 [domain]" echo "Example: $0 webpage4 mydomain.de" exit 1 fi NAME="$1" DOMAIN="${2:-mydomain.de}" if [[ ! "$NAME" =~ ^[a-zA-Z0-9][a-zA-Z0-9_-]*$ ]]; then echo "Invalid route name: $NAME" echo "Allowed: letters, numbers, underscore, dash" exit 1 fi ROOT_BASE="${ROOT_BASE:-/srv/web4beginners}" COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.traefik-routes.yml}" ROOT="${ROOT_BASE}/${NAME}" if [[ ! -f "$COMPOSE_FILE" ]]; then echo "Compose file not found: $COMPOSE_FILE" exit 1 fi mkdir -p "$ROOT" if [[ ! -f "$ROOT/web4beginners.com.html" ]]; then cp web4beginners.com.html "$ROOT/web4beginners.com.html" echo "Created: $ROOT/web4beginners.com.html" fi if [[ ! -f "$ROOT/site-content.de.json" ]]; then cp content/site-content.de.json "$ROOT/site-content.de.json" echo "Created: $ROOT/site-content.de.json" fi if rg -q "^ ${NAME}:$" "$COMPOSE_FILE"; then echo "Service '${NAME}' already exists in $COMPOSE_FILE" else block_file="$(mktemp)" tmp_file="$(mktemp)" cat > "$block_file" < 0) print line close(block_file) inserted = 1 } { print } END { if (inserted == 0) { while ((getline line < block_file) > 0) print line close(block_file) } } ' "$COMPOSE_FILE" > "$tmp_file" mv "$tmp_file" "$COMPOSE_FILE" rm -f "$block_file" echo "Inserted service '${NAME}' into $COMPOSE_FILE" fi echo echo "Next steps:" echo "1) docker compose -f $COMPOSE_FILE up -d --build" echo "2) Open: https://${DOMAIN}/${NAME}/"