#!/bin/bash
set -euo pipefail

# usage: file_env VAR [DEFAULT]
#    ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
#  "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
	local var="$1"
	local fileVar="${var}_FILE"
	local def="${2:-}"
	if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
		echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
		exit 1
	fi
	local val="$def"
	if [ "${!var:-}" ]; then
		val="${!var}"
	elif [ "${!fileVar:-}" ]; then
		val="$(< "${!fileVar}")"
	fi
	export "$var"="$val"
	unset "$fileVar"
}


envs=(
	DB_HOST
	DB_USER
	DB_PASSWORD
	DB_NAME
	SMTP_HOSTNAME
	SMTP_ADMIN_EMAIL
	SMTP_SERVER
	SMTP_PASSWORD
	SMTP_USE_TLS
	SMTP_STARTTLS
)

for e in "${envs[@]}"; do
	file_env "$e"
done

exec "$@"

#!/bin/bash
set -euo pipefail

# usage: file_env VAR [DEFAULT]
#    ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
#  "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
	local var="$1"
	local fileVar="${var}_FILE"
	local def="${2:-}"
	if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
		echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
		exit 1
	fi
	local val="$def"
	if [ "${!var:-}" ]; then
		val="${!var}"
	elif [ "${!fileVar:-}" ]; then
		val="$(< "${!fileVar}")"
	fi
	export "$var"="$val"
	unset "$fileVar"
}


envs=(
	DB_HOST
	DB_USER
	DB_PASSWORD
	DB_NAME
	SMTP_HOSTNAME
	SMTP_ADMIN_EMAIL
	SMTP_SERVER
    SMTP_USERNAME
	SMTP_PASSWORD
	SMTP_USE_TLS
	SMTP_STARTTLS
)

for e in "${envs[@]}"; do
	file_env "$e"
done

exec "$@"

Add SMTP_USERNAME to docker secrets params
Add  SMTP_USERNAME to docker secrets params
