#!/usr/bin/env bash set -Eeuo pipefail env_file="${1:-compose/.env}" source_directory="${2:-/srv/ai-soulmate/sources/ai-soulmate-server}" public_host="${3:-}" for required_command in git grep mktemp openssl; do if ! command -v "$required_command" >/dev/null 2>&1; then printf 'Required command is unavailable: %s\n' "$required_command" >&2 exit 1 fi done if [[ ! -f "$env_file" ]]; then printf 'Environment file not found: %s\n' "$env_file" >&2 exit 1 fi if [[ ! -d "$source_directory/.git" ]]; then printf 'AI SoulMate source checkout not found: %s\n' "$source_directory" >&2 exit 1 fi if [[ -z "$public_host" || ! "$public_host" =~ ^[A-Za-z0-9.-]+$ ]]; then printf 'Provide the approved LAN IPv4 address or DNS name as argument 3.\n' >&2 exit 1 fi source_branch="$(git -C "$source_directory" branch --show-current)" source_commit="$(git -C "$source_directory" rev-parse HEAD)" source_short_commit="${source_commit:0:12}" if [[ "$source_branch" != "local-deployment" ]]; then printf 'Expected source branch local-deployment, found: %s\n' "$source_branch" >&2 exit 1 fi if [[ -n "$(git -C "$source_directory" status --short --untracked-files=no)" ]]; then printf 'Tracked source changes are present; commit or discard them before building.\n' >&2 exit 1 fi env_value() { local key="$1" sed -n "s/^${key}=//p" "$env_file" | head -n 1 } set_value() { local key="$1" local value="$2" local temporary_file temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")" awk -v key="$key" -v value="$value" ' BEGIN { prefix = key "="; found = 0 } index($0, prefix) == 1 { if (!found) print prefix value found = 1 next } { print } END { if (!found) print prefix value } ' "$env_file" > "$temporary_file" chmod 600 "$temporary_file" mv -f "$temporary_file" "$env_file" } ensure_secret() { local key="$1" if [[ -z "$(env_value "$key")" ]]; then set_value "$key" "$(openssl rand -hex 32)" fi } set_value MINIO_API_BIND_ADDRESS "$public_host" set_value MINIO_API_CORS_ALLOW_ORIGIN "http://${public_host},http://${public_host}:8080,http://${public_host}:8081,http://localhost:8080,http://127.0.0.1:8080" set_value MINIO_APP_ACCESS_KEY ai_soulmate_app ensure_secret MINIO_APP_SECRET_KEY set_value BACKEND_COMPOSE_PROJECT_NAME ai-soulmate-backend set_value BACKEND_IMAGE "ai-soulmate-backend:${source_short_commit}" set_value BACKEND_BIND_ADDRESS 127.0.0.1 set_value BACKEND_HOST_PORT 3001 set_value BACKEND_MEMORY_LIMIT 4g set_value BACKEND_CPU_LIMIT 2.0 set_value BACKEND_UPLOADS_VOLUME_NAME ai-soulmate-backend-uploads set_value BACKEND_LOGS_VOLUME_NAME ai-soulmate-backend-logs set_value APP_ENV production ensure_secret SECRET_KEY ensure_secret ENCRYPTION_KEY ensure_secret ORDER_NO_SECRET ensure_secret CDK_CODE_PEPPER ensure_secret MLKJ_INTEGRATION_KEY ensure_secret MLKJ_INTEGRATION_SECRET ensure_secret PRINT_CLIENT_SHARED_TOKEN set_value OSS_PUBLIC_ENDPOINT "http://${public_host}:9000" set_value CORS_ALLOWED_ORIGINS "http://${public_host},http://${public_host}:8080,http://${public_host}:8081,http://localhost:8080,http://127.0.0.1:8080" set_value AI_SOULMATE_PUBLIC_URL "http://${public_host}:8080" set_value FRONTEND_URL "http://${public_host}:8080" if ! grep -q '^TRIPO3D_API_KEY=' "$env_file"; then set_value TRIPO3D_API_KEY "" fi set_value TRIPO3D_BASE_URL https://api.tripo3d.com/v2/openapi set_value TRIPO3D_DEFAULT_MODEL_VERSION v3.1-20260211 chmod 600 "$env_file" printf 'Backend environment configured without displaying secrets.\n' printf 'Source commit: %s\n' "$source_commit" printf 'Backend image: ai-soulmate-backend:%s\n' "$source_short_commit" printf 'Backend bind: 127.0.0.1:3001\n' printf 'MinIO API bind: %s:9000\n' "$public_host" printf 'MinIO console remains bound to 127.0.0.1.\n'