#!/bin/sh

# shellcheck disable=SC1091

export LOG_LEVEL="${LOG_LEVEL:-INFO}"
. /opt/scripts/logging.sh
: "${warning_counter:=0}"
: "${error_counter:=0}"

# Functions
check_deprecations() {
  # Warning for deprecated settings
  if [ -n "$BACKUP_FILE" ]; then
    warn "\$BACKUP_FILE is deprecated and will be removed in future versions. Please use \$BACKUP_DIR instead to specify the folder of the backup."
    if [ -z "$BACKUP_DIR" ]; then
      BACKUP_DIR=$(dirname "$BACKUP_FILE")
      warn "Since \$BACKUP_DIR is not set defaulting to BACKUP_DIR=$BACKUP_DIR"
    fi
  fi

  # Warning for deprecated settings
  if [ -n "$BACKUP_FILE_PERMISSIONS" ]; then
    warn "\$BACKUP_FILE_PERMISSIONS is deprecated and will be removed in future versions. Please use \$BACKUP_DIR_PERMISSIONS instead to specify the permissions of the backup folder."
    if [ -z "$BACKUP_DIR_PERMISSIONS" ]; then
      BACKUP_DIR_PERMISSIONS="$BACKUP_FILE_PERMISSIONS"
      warn "Since \$BACKUP_DIR_PERMISSIONS is not set defaulting to BACKUP_DIR_PERMISSIONS=$BACKUP_FILE_PERMISSIONS"
    fi
  fi

  # Warning for deprecated settings
  if [ -n "$DB_FILE" ]; then
    warn "\$DB_FILE is deprecated and will be removed in future versions. Please use \$VW_DATABASE_URL instead to specify the location of the source database file."
    if [ -z "$VW_DATABASE_URL" ]; then
      VW_DATABASE_URL="$DB_FILE"
      warn "Since \$VW_DATABASE_URL is not set defaulting to VW_DATABASE_URL=$DB_FILE"
    fi
  fi

  # Warning for deprecated settings
  if [ -n "$ATTACHMENT_DIR" ]; then
    warn "\$ATTACHMENT_DIR is deprecated and will be removed in future versions. Please use \$VW_ATTACHMENTS_FOLDER instead to specify the location of the source attachments folder."
    if [ -z "$VW_ATTACHMENTS_FOLDER" ]; then
      VW_ATTACHMENTS_FOLDER="$ATTACHMENT_DIR"
      warn "Since \$VW_ATTACHMENTS_FOLDER is not set defaulting to VW_ATTACHMENTS_FOLDER=$ATTACHMENT_DIR"
    fi
  fi

  # Warning for deprecated settings
  if [ -n "$LOGFILE" ]; then
    warn "\$LOGFILE is deprecated and will be removed in future versions. Please use \$LOG_DIR instead to specify the location of the logfile folder."
    if [ -z "$LOG_DIR" ]; then
      LOG_DIR="$(dirname "$(realpath "$LOGFILE")")"
      warn "Since \$LOG_DIR is not set defaulting to LOG_DIR=$LOG_DIR"
    fi
  fi

  # Warning for deprecated settings
  if [ -n "$ATTACHMENT_BACKUP_DIR" ]; then
    warn "\$ATTACHMENT_BACKUP_DIR is deprecated and will be removed in future versions. Attachment backups are stored in the \$BACKUP_DIR."
  fi

  # Warning for deprecated settings
  if [ -n "$ATTACHMENT_BACKUP_FILE" ]; then
    warn "\$ATTACHMENT_BACKUP_FILE is deprecated and will be removed in future versions. Attachment backups are stored in the \$BACKUP_DIR."
  fi
}

check_deprecations

# Set default environment variables
# Environment variables specific to this image
export APP_DIR="${APP_DIR:-/app}"
export APP_DIR_PERMISSIONS="${APP_DIR_PERMISSIONS:-700}"
export BACKUP_DIR="${BACKUP_DIR:-/backup}"
export BACKUP_DIR_PERMISSIONS="${BACKUP_DIR_PERMISSIONS:-700}"
export BACKUP_ON_STARTUP="${BACKUP_ON_STARTUP:-false}"
export BACKUP_USE_DEDUPE="${BACKUP_USE_DEDUPE:-false}"
export BACKUP_HASHING_ALGORITHM="${BACKUP_HASHING_ALGORITHM:-sha256}"
export BACKUP_HASHING_EXEC="${BACKUP_HASHING_EXEC:-"$BACKUP_HASHING_ALGORITHM"sum}"
export BACKUP_INI="${BACKUP_INI:-$BACKUP_DIR/vaultwarden-backup.ini}"
export CRONFILE="${CRONFILE:-/etc/crontabs/root}"
export CRON_TIME="${CRON_TIME:-0 5 * * *}"
export DELETE_AFTER="${DELETE_AFTER:-0}"
export ENCRYPTION_ALGORITHM="${ENCRYPTION_ALGORITHM:-AES256}"
export ENCRYPTION_BASE64_GPG_KEY="${ENCRYPTION_BASE64_GPG_KEY:-false}"
export ENCRYPTION_GPG_KEYFILE_LOCATION="${ENCRYPTION_GPG_KEYFILE_LOCATION:-$APP_DIR/encryption_key.asc}"
export ENCRYPTION_PASSWORD="${ENCRYPTION_PASSWORD:-false}"
export GID="${GID:-100}"
export GNUPGHOME="${GNUPGHOME:-/tmp/gpg}"
export GNUPGHOME_PERMISSIONS="${GNUPGHOME_PERMISSIONS:-700}"
export HEALTHCHECK_FILE="${HEALTHCHECK_FILE:-$APP_DIR/health}"
export HEALTHCHECK_FILE_PERMISSIONS="${HEALTHCHECK_FILE_PERMISSIONS:-700}"
export LOGFILE_APP="${LOGFILE_APP:-$LOG_DIR/app.log}"
export LOGFILE_CRON="${LOGFILE_CRON:-$LOG_DIR/cron.log}"
export LOG_CLEAR_AT_START="${LOG_CLEAR_AT_START:-false}"
export LOG_DIR="${LOG_DIR:-$APP_DIR/log}"
export LOG_DIR_PERMISSIONS="${LOG_DIR_PERMISSIONS:-777}"
export TIMESTAMP="${TIMESTAMP:-false}"
export UID="${UID:-100}"
export VW_BACKUP_VERSION="0.0.0-dev"

# Additional backup files
export BACKUP_ADD_DATABASE="${BACKUP_ADD_DATABASE:-true}"
export BACKUP_ADD_ATTACHMENTS="${BACKUP_ADD_ATTACHMENTS:-true}"
export BACKUP_ADD_CONFIG_JSON="${BACKUP_ADD_CONFIG_JSON:-true}"
export BACKUP_ADD_ICON_CACHE="${BACKUP_ADD_ICON_CACHE:-false}"
export BACKUP_ADD_RSA_KEY="${BACKUP_ADD_RSA_KEY:-true}"
export BACKUP_ADD_SENDS="${BACKUP_ADD_SENDS:-false}"

# Vaultwarden file locations
# Defaulting to <https://github.com/dani-garcia/vaultwarden/wiki/Changing-persistent-data-location>
export VW_DATA_FOLDER="${VW_DATA_FOLDER:-/data}"
export VW_DATABASE_URL="${VW_DATABASE_URL:-$VW_DATA_FOLDER/db.sqlite3}"
export VW_ATTACHMENTS_FOLDER="${VW_ATTACHMENTS_FOLDER:-$VW_DATA_FOLDER/attachments}"
export VW_ICON_CACHE_FOLDER="${VW_ICON_CACHE_FOLDER:-$VW_DATA_FOLDER/icon_cache}"
