#!/bin/bash
## envs/ から k8s/secrets.env を生成する
## usage: cd k8s && bash gen-secrets.sh

ENVS_DIR="${1:-../envs}"
OUT="secrets.env"

get_val() {
  local file="$1" key="$2"
  grep "^${key}=" "$file" 2>/dev/null | head -1 | cut -d'=' -f2-
}

cat > "$OUT" <<EOF
pds-admin-password=$(get_val "$ENVS_DIR/pds" PDS_ADMIN_PASSWORD)
pds-plc-rotation-key=$(get_val "$ENVS_DIR/pds" PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX)
pds-repo-signing-key=$(get_val "$ENVS_DIR/pds" PDS_REPO_SIGNING_KEY_K256_PRIVATE_KEY_HEX)
pds-jwt-secret=$(get_val "$ENVS_DIR/pds" PDS_JWT_SECRET)
pds-email-smtp-url=$(get_val "$ENVS_DIR/pds" PDS_EMAIL_SMTP_URL)
bsky-admin-passwords=$(get_val "$ENVS_DIR/bsky" BSKY_ADMIN_PASSWORDS)
bsky-service-signing-key=$(get_val "$ENVS_DIR/bsky" BSKY_SERVICE_SIGNING_KEY)
bgs-admin-key=$(get_val "$ENVS_DIR/bgs" BGS_ADMIN_KEY)
ozone-admin-password=$(get_val "$ENVS_DIR/ozone" OZONE_ADMIN_PASSWORD)
ozone-signing-key-hex=$(get_val "$ENVS_DIR/ozone" OZONE_SIGNING_KEY_HEX)
EOF

echo "generated $OUT"
