#!/usr/bin/env bash
set -euo pipefail

export GPG_TTY=$(tty)

function get_or_gen_secret() {
  local path=$1
  local generate=${2:-false}
  local description=$3

  if ! gopass show "$path" >/dev/null 2>&1; then
    if [ "$generate" = "true" ]; then
      local val=$(openssl rand -hex 32)
      gopass insert -f "$path" <<< "$val"
    else
      echo >&2 "Missing required secret: $description ($path)"
      echo >&2 "Please enter the value:"
      read -rs val
      gopass insert -f "$path" <<< "$val"
    fi
  fi
  gopass show -o "$path"
}

FIRECRAWL_DOMAIN=$(get_or_gen_secret "projects/firecrawl-dokploy/prod/firecrawl_domain" false "Firecrawl domain (e.g. firecrawl.yourdomain.com)")
TEST_API_KEY=$(get_or_gen_secret "projects/firecrawl-dokploy/prod/api_key" true "Firecrawl API Key")
BULL_AUTH_KEY=$(get_or_gen_secret "projects/firecrawl-dokploy/prod/bull_auth_key" true "Bull Auth Key")
POSTGRES_PASSWORD=$(get_or_gen_secret "projects/firecrawl-dokploy/prod/postgres_password" true "PostgreSQL Password")
SEARXNG_SECRET_KEY=$(get_or_gen_secret "projects/firecrawl-dokploy/prod/searxng_secret_key" true "SearXNG Secret Key")
OPENAI_API_KEY=$(get_or_gen_secret "projects/firecrawl-dokploy/prod/openai_api_key" false "OpenAI API Key (optional, press enter to skip)")

cat <<EOF
FIRECRAWL_DOMAIN=$FIRECRAWL_DOMAIN
TEST_API_KEY=$TEST_API_KEY
BULL_AUTH_KEY=$BULL_AUTH_KEY
POSTGRES_PASSWORD=$POSTGRES_PASSWORD
SEARXNG_SECRET=$SEARXNG_SECRET_KEY
OPENAI_API_KEY=$OPENAI_API_KEY
EOF
