#!/usr/bin/env bash

LENNY_ENV_FILE=".env"
READER_ENV_FILE="reader.env"

genpass() {
    len=${1:-32}
    dd if=/dev/urandom bs=1 count=$((len * 2)) 2>/dev/null | base64 | tr -dc 'A-Za-z0-9' | head -c "$len"
}

# Exit if the file already exists
if [ -f "$LENNY_ENV_FILE" ]; then
  echo "Skipping configure: $LENNY_ENV_FILE already configured."
else
  echo "Creating $LENNY_ENV_FILE."
 
  # Use environment variables if they are set, otherwise provide defaults or generate secure values
  LENNY_HOST="localhost"
  LENNY_PORT="${LENNY_PORT:-8080}"
  LENNY_WORKERS="${LENNY_WORKERS:-1}"
  LENNY_LOG_LEVEL="${LENNY_LOG_LEVEL:-debug}"
  LENNY_RELOAD="${LENNY_RELOAD:-1}"
  LENNY_SSL_CRT="${LENNY_SSL_CRT:-}"
  LENNY_SSL_KEY="${LENNY_SSL_KEY:-}"
  LENNY_SEED="${LENNY_SEED:-$(genpass 32)}"

  READER_PORT="${READER_PORT:-3000}"
  READIUM_PORT="${READIUM_PORT:-15080}"

  NEXT_PUBLIC_ENABLE_MANIFEST_ROUTE="${NEXT_PUBLIC_ENABLE_MANIFEST_ROUTE:-true}"
  NEXT_PUBLIC_MANIFEST_FORCE_ENABLE="${NEXT_PUBLIC_MANIFEST_FORCE_ENABLE:-true}"
  NODE_ENV="${NODE_ENV:-production}"

  DB_USER="${POSTGRES_USER:-librarian}"
  DB_HOST="${POSTGRES_HOST:-127.0.0.1}"
  DB_PORT="${POSTGRES_PORT:-5432}"

  DB_PASSWORD="${POSTGRES_PASSWORD:-$(genpass 32)}"
  DB_NAME="${DB_NAME:-lenny}"

  S3_ACCESS_KEY="${MINIO_ROOT_USER:-$(genpass 20)}"
  S3_SECRET_KEY="${MINIO_ROOT_PASSWORD:-$(genpass 40)}"
  S3_ENDPOINT="${S3_ENDPOINT:-http://s3:9000}"

  # Write to lenny.env
  cat <<EOF > "$LENNY_ENV_FILE"
# API
LENNY_PROXY=
LENNY_HOST=$LENNY_HOST
LENNY_PORT=$LENNY_PORT
LENNY_WORKERS=$LENNY_WORKERS
LENNY_SEED=$LENNY_SEED
LENNY_LOG_LEVEL=$LENNY_LOG_LEVEL
LENNY_RELOAD=$LENNY_RELOAD
LENNY_SSL_CRT=$LENNY_SSL_CRT
LENNY_SSL_KEY=$LENNY_SSL_KEY

# Service Ports
READER_PORT=$READER_PORT
READIUM_PORT=$READIUM_PORT

# DB
DB_USER=$DB_USER
DB_HOST=$DB_HOST
DB_PORT=$DB_PORT
DB_PASSWORD=$DB_PASSWORD
DB_NAME=$DB_NAME
DB_TYPE=postgres

# S3 Credentials
S3_ACCESS_KEY=$S3_ACCESS_KEY
S3_SECRET_KEY=$S3_SECRET_KEY
S3_ENDPOINT=$S3_ENDPOINT
S3_PROVIDER=minio
S3_SECURE=false

EOF
fi

# Exit if the file already exists
if [ -f "$READER_ENV_FILE" ]; then
  echo "Skipping configure: $READER_ENV_FILE already configured."
else
  echo "Creating $READER_ENV_FILE"
  cat <<EOF > "$READER_ENV_FILE"
# Reader
NEXT_PUBLIC_ENABLE_MANIFEST_ROUTE=$NEXT_PUBLIC_ENABLE_MANIFEST_ROUTE
NEXT_PUBLIC_MANIFEST_FORCE_ENABLE=$NEXT_PUBLIC_MANIFEST_FORCE_ENABLE
NEXT_PUBLIC_MANIFEST_ALLOWED_DOMAINS=127.0.0.1,localhost,*.trycloudflare.com
NODE_ENV=$NODE_ENV

EOF
fi
