#!/usr/bin/env bash

# {{ ansible_managed }}

## ===================================================================
## - Configuration File: Install/Update Dovecot Server
## ===================================================================

# ---
# --- Configure Settings for your Server here..
# ---


# ---
# - Base Configuration
# ---

# - Set update=false if that is a new installation
# -
# - This parameter must be set here! There is no default value.
# -
_update=true


# - Install dovecot with systemd support
# -
# - Defaults to 'true' if systemd is present, otherwise to 'false'
# -
systemd_support="true"


# - postmaster_address
# -
# - This parameter must be set here! There is no default value.
# -
# - Example:
# -    postmaster_address="admin\@warenform.net"
# -
postmaster_address="{{ admin_email | split('@') | first }}\@{{ admin_email | split('@') | last }}"


# - hostname
# -
# - This parameter must be set here! There is no default value.
# -
{% if hostname is defined and hostname %}
hostname="{{ hostname }}"
{% else %}
hostname=
{% endif %}


# - IPv4 / IPv6 Addressen
# -
{% if ipv4_address is defined and ipv4_address %}
ipv4="{{ ipv4_address }}"
{% else %}
ipv4=
{% endif %}
{% if ipv6_address is defined and ipv6_address %}
ipv6="{{ ipv6_address }}"
{% else %}
ipv6=
{% endif %}


# - Webserver User
# -
# - defaults to:
# -    http_user=www-data
# -
#http_user="www-data"


# ---
# - Listener configuration
# ---
if [[ -n "$ipv6" ]]; then
   imap_listener_adresses="127.0.0.1 $ipv4 $ipv6"
   imaps_listener_adresses="$ipv4 $ipv6"

   pop_listener_adresses="$ipv4 $ipv6"
   pops_listener_adresses="$ipv4 $ipv6"
else
   imap_listener_adresses="127.0.0.1 $ipv4"
   imaps_listener_adresses="$ipv4"

   pop_listener_adresses="$ipv4"
   pops_listener_adresses="$ipv4"
fi

xmpp_listener=false
xmpp_listener_addresses="$ipv4 $ipv6"
xmpp_listener_port="44444"

# ---
# - Database settings
# ---

# - database
# -
# - This parameter must be set here! There is no default value.
# -
# - Possible values for parameter database are only  'mysql' and 'postgres'
# - 
{% if "PostgreSQL" == postfix_db_type %}
database="postgres"
{% else %}
database="mysql"
{% endif %}

# - dbname
# -
# - Defaults to 'postfix'
{% if postfix_db_name is defined and postfix_db_name %}
dbname={{ postfix_db_name }}
{% else %}
dbname=
{% endif %}

# - dbuser
# -
# - Defaults to 'postfix'
# -
{% if postfix_db_user is defined and postfix_db_user %}
dbuser={{ postfix_db_user }}
{% else %}
dbuser=
{% endif %}

# - dbpassword
# -
# - This parameter must be set here!
# -
{% if postfix_db_pass is defined and postfix_db_pass %}
dbpassword="{{ postfix_db_pass }}"
{% else %}
dbpassword=
{% endif %}

# - dbhost
# -
# - Defaults to:
# -    '/var/run/postgresql'  if database is set to 'postgres'
# -    '127.0.0.1'            if database is set to 'mysql'
# -
{% if postfix_db_host is defined and postfix_db_host %}
{% if postfix_db_type == 'PostgeSQL' %}
dbhost=/var/run/postgresql
{% else %}
dbhost={{ postfix_db_host }}
{% endif %}
{% else %}
dbhost=
{% endif %}

# - default_pass_scheme
# -
# - Password databases have a default password scheme.
# -
# - The password scheme can be overridden for each password by prefixing
# - it with {SCHEME}, for example: {SHA512-CRYPT}pass.
# -
# - See: https://wiki.dovecot.org/Authentication/PasswordSchemes
# -
# - Defaults to 'PLAIN'
# -
default_pass_scheme=PLAIN


# ---
# - Cert/Key configurations
# ---

cert_base_dir="/etc/dovecot/ssl"
server_cert=${cert_base_dir}/mailserver.crt
server_key=${cert_base_dir}/mailserver.key
dh_pem_file="${cert_base_dir}/dh_4096.pem"

imap_cert=${cert_base_dir}/mailserver.crt
imap_key=${cert_base_dir}/mailserver.key

pop_cert=${cert_base_dir}/mailserver.crt
pop_key=${cert_base_dir}/mailserver.key


# - Other settings
# -
spam_folder=Spam

max_userip_connections=48
#auth_mechanisms="plain login digest-md5 cram-md5"
auth_mechanisms="plain login"

# - Settings for quota warning sript
# - 
{% if dovecot_msg_language is defined and dovecot_msg_language %}
msg_language="{{ dovecot_msg_language }}"
{% else %}
msg_language=
{% endif %}
{% if dovecot_from_address is defined and dovecot_from_address %}
from_address="{{ dovecot_from_address }}"
{% else %}
from_address=
{% endif %}
{% if dovecot_reply_to is defined and dovecot_reply_to %}
reply_to="{{ dovecot_reply_to }}"
{% else %}
reply_to=
{% endif %}
{% if webmailer_address is defined and webmailer_address %}
webmailer="{{ webmailer_address }}"
{% else %}
webmailer=
{% endif %}
{% if salutation is defined and salutation %}
salutation="{{ salutation }}"
{% else %}
salutation=
{% endif %}

# ---
# --- End server-specific Settings
# ---
