#!/bin/bash

# export env variables again so they are not mandatory in docker-compose.yml in a backward compatible manner
export NUM_WORKERS_DEFAULT=${NUM_WORKERS_DEFAULT:-${WORKERS:-5}}
export NUM_WORKERS_PRIO=${NUM_WORKERS_PRIO:-${WORKERS:-5}}
export NUM_WORKERS_EMAIL=${NUM_WORKERS_EMAIL:-${WORKERS:-5}}
export NUM_WORKERS_UPDATE=${NUM_WORKERS_UPDATE:-${WORKERS:-1}}
export NUM_WORKERS_CACHE=${NUM_WORKERS_CACHE:-${WORKERS:-5}}

export MYSQL_HOST=${MYSQL_HOST:-db}
export MYSQL_PORT=${MYSQL_PORT:-3306}
export MYSQL_USER=${MYSQL_USER:-misp}
export MYSQL_PASSWORD=${MYSQL_PASSWORD:-example}
export MYSQL_DATABASE=${MYSQL_DATABASE:-misp}
export MYSQL_CMD="mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -P $MYSQL_PORT -h $MYSQL_HOST -r -N $MYSQL_DATABASE"
export REDIS_HOST=${REDIS_HOST:-redis}
export REDIS_PORT=${REDIS_PORT:-6379}
export ENABLE_REDIS_EMPTY_PASSWORD=${ENABLE_REDIS_EMPTY_PASSWORD:-false}

# Set Redis password based on ENABLE_REDIS_EMPTY_PASSWORD setting
if [ "$ENABLE_REDIS_EMPTY_PASSWORD" = "true" ]; then
    # This still need to be set to empty string to ensure all places where it's used got the correct value
    export REDIS_PASSWORD=""
else
    export REDIS_PASSWORD=${REDIS_PASSWORD:-redispassword}
fi
export BASE_URL=${BASE_URL:-https://localhost}
export DISABLE_IPV6=${DISABLE_IPV6:-false}
export DISABLE_SSL_REDIRECT=${DISABLE_SSL_REDIRECT:-false}
export DISABLE_CA_REFRESH=${DISABLE_CA_REFRESH:-false}
export SMTP_FQDN=${SMTP_FQDN:-mail}

export CRON_USER_ID=${CRON_USER_ID:-1}
export CRON_PULLALL=${CRON_PULLALL:-86400}
export CRON_PUSHALL=${CRON_PUSHALL:-86400}

export ADMIN_EMAIL=${ADMIN_EMAIL:-admin@admin.test}
export GPG_PASSPHRASE=${GPG_PASSPHRASE:-passphrase}
export MISP_MODULES_FQDN=${MISP_MODULES_FQDN:-http://misp-modules}
export ATTACHMENTS_DIR=${ATTACHMENTS_DIR:-/var/www/MISP/app/files}

export AUTOCONF_GPG=${AUTOCONF_GPG:-true}
export AUTOCONF_ADMIN_KEY=${AUTOCONF_ADMIN_KEY:-true}
export OIDC_ENABLE=${OIDC_ENABLE:-false}
export OIDC_MIXEDAUTH=${OIDC_MIXEDAUTH:-false}
export LDAP_ENABLE=${LDAP_ENABLE:-false}
export ENABLE_DB_SETTINGS=${ENABLE_DB_SETTINGS:-false}
export ENABLE_BACKGROUND_UPDATES=${ENABLE_BACKGROUND_UPDATES:-false}
export PROXY_ENABLE=${PROXY_ENABLE:-false}
export DEBUG=${DEBUG:-0}

export FASTCGI_READ_TIMEOUT=${FASTCGI_READ_TIMEOUT:-300s}
export FASTCGI_SEND_TIMEOUT=${FASTCGI_SEND_TIMEOUT:-300s}
export FASTCGI_CONNECT_TIMEOUT=${FASTCGI_CONNECT_TIMEOUT:-300s}

export PHP_FCGI_CHILDREN=${PHP_FCGI_CHILDREN:-5}
export PHP_FCGI_START_SERVERS=${PHP_FCGI_START_SERVERS:-2}
export PHP_FCGI_SPARE_SERVERS=${PHP_FCGI_SPARE_SERVERS:-1}
export PHP_FCGI_MAX_REQUESTS=${PHP_FCGI_MAX_REQUESTS:-0}

export PHP_MEMORY_LIMIT=${PHP_MEMORY_LIMIT:-2048M}
export PHP_MAX_EXECUTION_TIME=${PHP_MAX_EXECUTION_TIME:-300}
export PHP_UPLOAD_MAX_FILESIZE=${PHP_UPLOAD_MAX_FILESIZE:-50M}
export PHP_POST_MAX_SIZE=${PHP_POST_MAX_SIZE:-50M}
export PHP_MAX_INPUT_TIME=${PHP_MAX_INPUT_TIME:-300}
export PHP_MAX_FILE_UPLOADS=${PHP_MAX_FILE_UPLOADS:-50}

export PHP_FCGI_CHILDREN=${PHP_FCGI_CHILDREN:-5}
export PHP_FCGI_START_SERVERS=${PHP_FCGI_START_SERVERS:-2}
export PHP_FCGI_SPARE_SERVERS=${PHP_FCGI_SPARE_SERVERS:-1}
export PHP_FCGI_MAX_REQUESTS=${PHP_FCGI_MAX_REQUESTS:-0}

export PHP_SESSION_TIMEOUT=${PHP_SESSION_TIMEOUT:-60}
export PHP_SESSION_COOKIE_TIMEOUT=${PHP_SESSION_COOKIE_TIMEOUT:-10080}
export PHP_SESSION_DEFAULTS=${PHP_SESSION_DEFAULTS:-php}
export PHP_SESSION_AUTO_REGENERATE=${PHP_SESSION_AUTO_REGENERATE:-false}
export PHP_SESSION_CHECK_AGENT=${PHP_SESSION_CHECK_AGENT:-false}
export PHP_SESSION_COOKIE_SECURE=${PHP_SESSION_COOKIE_SECURE:-true}
export PHP_SESSION_COOKIE_DOMAIN=${PHP_SESSION_COOKIE_DOMAIN}
export PHP_SESSION_COOKIE_SAMESITE=${PHP_SESSION_COOKIE_SAMESITE:-Lax}
export PHP_TIMEZONE=${PHP_TIMEZONE:-UTC}

export NGINX_X_FORWARDED_FOR=${NGINX_X_FORWARDED_FOR:-false}
export NGINX_SET_REAL_IP_FROM=${NGINX_SET_REAL_IP_FROM}
export NGINX_CLIENT_MAX_BODY_SIZE=${NGINX_CLIENT_MAX_BODY_SIZE:-50M}

export SUPERVISOR_HOST=${SUPERVISOR_HOST:-127.0.0.1}
export SUPERVISOR_USERNAME=${SUPERVISOR_USERNAME:-supervisor}
export SUPERVISOR_PASSWORD=${SUPERVISOR_PASSWORD:-supervisor}

# Hinders further execution when sourced from other scripts
if [ -n "${BASH_SOURCE[0]}" ]; then
    return
fi

# start supervisord using the main configuration file so we have a socket interface
exec /usr/bin/tini -- /usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf
