#!/usr/bin/env bash set -Eeuo pipefail env_file="${1:-compose/.env}" admin_compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}" infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" if [[ ! -r "$env_file" ]]; then printf 'Environment file not found: %s\n' "$env_file" >&2 exit 1 fi env_value() { awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" } infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file") super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \ --username "$(env_value POSTGRES_USER)" --dbname "$(env_value ADMIN_POSTGRES_DB)" \ --tuples-only --no-align \ --command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'")" if [[ "$super_admin_count" != "1" ]]; then printf 'Refusing to clear bootstrap password; expected one SUPER_ADMIN, found: %s\n' "$super_admin_count" >&2 exit 1 fi temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")" awk ' BEGIN { found = 0 } index($0, "ADMIN_INITIAL_PASSWORD=") == 1 { if (!found) print "ADMIN_INITIAL_PASSWORD=" found = 1 next } { print } END { if (!found) print "ADMIN_INITIAL_PASSWORD=" } ' "$env_file" > "$temporary_file" chmod 600 "$temporary_file" mv -f "$temporary_file" "$env_file" project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)" admin_compose=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$admin_compose_file") "${admin_compose[@]}" config --quiet "${admin_compose[@]}" up -d --force-recreate admin-backend printf 'Admin bootstrap password cleared from compose/.env and backend container environment.\n'