#!/usr/bin/env sh
set -eu

vault_dir="/vault/secrets"

read_secret() {
  tr -d '\r\n' < "${vault_dir}/$1"
}

read_optional() {
  if [ -f "${vault_dir}/$1" ]; then
    tr -d '\r\n' < "${vault_dir}/$1"
  else
    printf ''
  fi
}

export TURN_STATIC_AUTH_SECRET="$(read_secret turn-secret)"
export TURN_PASSWORD="${TURN_STATIC_AUTH_SECRET}"

export LIVEKIT_API_SECRET="$(read_secret livekit-primary)"
export LIVEKIT_SECRET="${LIVEKIT_API_SECRET}"

export BOT_PASS="$(read_secret bot-pass)"
export BOT_PASS_QUICK="$(read_optional bot-quick-pass)"
export BOT_PASS_SMART="$(read_optional bot-smart-pass)"
export BOT_PASS_GENIUS="$(read_optional bot-genius-pass)"
if [ -z "${BOT_PASS_QUICK}" ]; then BOT_PASS_QUICK="${BOT_PASS}"; fi
if [ -z "${BOT_PASS_SMART}" ]; then BOT_PASS_SMART="${BOT_PASS}"; fi
if [ -z "${BOT_PASS_GENIUS}" ]; then BOT_PASS_GENIUS="${BOT_PASS_SMART}"; fi
export SEEDER_PASS="$(read_secret seeder-pass)"

export CHAT_API_KEY="$(read_secret chat-matrix)"
export CHAT_API_HOMEPAGE="$(read_secret chat-homepage)"

export MAS_ADMIN_CLIENT_SECRET_FILE="${vault_dir}/mas-admin-secret"
export PGPASSWORD="$(read_secret synapse-db-pass)"

export MAS_DB_PASSWORD="$(read_secret mas-db-pass)"
export MATRIX_SHARED_SECRET="$(read_secret mas-matrix-shared)"
export KEYCLOAK_CLIENT_SECRET="$(read_secret mas-kc-secret)"
