#!/usr/bin/env bash set -euo pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "${script_dir}/.." && pwd)" env_file="${repo_root}/.env.devprod" project_name="" write_if_missing="0" source "${script_dir}/lib-compose-project.sh" usage() { cat <<'USAGE' Usage: ./scripts/ensure-compose-project-name.sh [options] Pins or reports the stable Docker Compose project name used by this cloud. Keeping COMPOSE_PROJECT_NAME stable prevents folder renames from attaching the instance to a fresh set of named volumes. Options: --env-file Env file to inspect/update (default: .env.devprod) --project-name Explicit project name to write after sanitization --write-if-missing If COMPOSE_PROJECT_NAME is missing, seed it from the env file directory name -h, --help Show this help USAGE } while [[ $# -gt 0 ]]; do case "$1" in --env-file) env_file="${2:-}" shift 2 ;; --env-file=*) env_file="${1#*=}" shift ;; --project-name) project_name="${2:-}" shift 2 ;; --project-name=*) project_name="${1#*=}" shift ;; --write-if-missing) write_if_missing="1" shift ;; -h|--help) usage exit 0 ;; *) echo "Unknown option: $1" usage exit 1 ;; esac done if [[ "${env_file}" != /* ]]; then env_file="${repo_root}/${env_file}" fi if [[ ! -f "${env_file}" ]]; then echo "Missing env file: ${env_file}" exit 1 fi current_project_name="$(read_compose_project_name_from_env "${env_file}" || true)" if [[ -n "${current_project_name}" ]]; then echo "COMPOSE_PROJECT_NAME=${current_project_name}" exit 0 fi if [[ -n "${project_name}" ]]; then project_name="$(sanitize_compose_project_name "${project_name}")" set_compose_project_name_in_env "${env_file}" "${project_name}" echo "Pinned COMPOSE_PROJECT_NAME=${project_name} in ${env_file}" exit 0 fi if [[ "${write_if_missing}" == "1" ]]; then project_name="$(derive_compose_project_name_seed "${env_file}")" set_compose_project_name_in_env "${env_file}" "${project_name}" echo "Seeded COMPOSE_PROJECT_NAME=${project_name} in ${env_file}" exit 0 fi warn_missing_compose_project_name "${env_file}" exit 1