#!/bin/sh -e

os="$(cat "${__global}/explorer/os")"

root_email="$(tr '\n' ',' < "${__object}/parameter/root-email" | sed -E 's/,+$//')"
smart_host="$(cat "${__object}/parameter/smart-host")"

if [ -f "${__object}/parameter/mailname" ]; then
	mailname="$(cat "${__object}/parameter/mailname")"
else
	# default mailname behaviour is different in certain systems
	case ${os} in
		debian|devuan|ubuntu)
			# Debian-like default to /etc/mailname
			mailname="/etc/mailname"
		;;
		*)
			# Otherwise let's use the hostname
			mailname="${__target_host}"
		;;
	esac
fi

aliases_file=/etc/mail/aliases
case ${os} in
	debian|devuan|ubuntu)
		# Debian-like requires installing DMA
		__package dma
		# Moving forward without DMA doesn't make much sense
		export require="__package/dma"
		aliases_file=/etc/aliases
	;;
	freebsd)
		# Disable sendmail + stop if necessary
		__key_value \
			--file      "/etc/rc.conf" \
			--comment   "# Disable sendmail " \
			--key       "sendmail_enable" \
			--delimiter "=" \
			--value     "NONE" \
			--onchange  "service sendmail onestop || true" \
			"sendmail_enable"
		# Setup mailwrapper accordingly
		__file /etc/mail/mailer.conf \
			--mode 0644 \
			--source '-' <<EOF
sendmail	/usr/libexec/dma
send-mail	/usr/libexec/dma
mailq		/usr/libexec/dma
newaliases	/usr/libexec/dma
rmail		/usr/libexec/dma
EOF
	;;
	*)
		cat > /dev/stderr <<EOF
Your OS (${os}) is not supported yet.

Maybe adding support is as simple as adapting the packages or allowing it,
we highly encourage you to open a PR with the necessary changes.
See: https://code.ungleich.ch/ungleich-public/cdist-contrib/
EOF
		exit 1
	;;
esac

DMA_CONF="$(cat <<EOF
# Managed remotely, changes will be lost
#
# Your smarthost (also called relayhost).  Leave blank if you don't want
# smarthost support.
SMARTHOST ${smart_host}

# Use this SMTP port.  Most users will be fine with the default (25)
#PORT 25

# Path to your alias file.  Just stay with the default.
#ALIASES /etc/aliases

# Path to your spooldir.  Just stay with the default.
#SPOOLDIR /var/spool/dma

# SMTP authentication
#AUTHPATH /etc/dma/auth.conf

# Uncomment if yout want TLS/SSL support
SECURETRANSFER

# Uncomment if you want STARTTLS support (only used in combination with
# SECURETRANSFER)
STARTTLS

# Uncomment if you have specified STARTTLS above and it should be allowed
# to fail ("opportunistic TLS", use an encrypted connection when available
# but allow an unencrypted one to servers that do not support it)
#OPPORTUNISTIC_TLS

# Path to your local SSL certificate
#CERTFILE

# If you want to use plain text SMTP login without using encryption, change
# the SECURE entry below to INSECURE.  Otherwise plain login will only work
# over a secure connection.  Use this option with caution.
#SECURE

# Uncomment if you want to defer your mails.  This is useful if you are
# behind a dialup line.  You have to submit your mails manually with dma -q
#DEFER

# Uncomment if you want the bounce message to include the complete original
# message, not just the headers.
#FULLBOUNCE

# The internet hostname dma uses to identify the host.
# If not set or empty, the result of gethostname(2) is used.
# If MAILNAME is an absolute path to a file, the first line of this file
# will be used as the hostname.
MAILNAME ${mailname}

# Masquerade envelope from addresses with this address/hostname.
# Use this if mails are not accepted by destination mail servers because
# your sender domain is invalid.
# By default, MASQUERADE is not set.
# Format: MASQUERADE [user@][host]
# Examples:
# MASQUERADE john@  on host "hamlet" will send all mails as john@hamlet
# MASQUERADE percolator  will send mails as \$username@percolator, e.g. fish@percolator
# MASQUERADE herb@ert  will send all mails as herb@ert

# Directly forward the mail to the SMARTHOST bypassing aliases and local delivery
#NULLCLIENT
EOF
)"

__file /etc/dma/dma.conf --mode 0644 --source '-' <<EOF
${DMA_CONF}
EOF

# Setup aliases file (Debian doesn't create it by default)
__file	--state exists \
	--mode 0644 \
	--source "${__type}/files/aliases" \
	"${aliases_file}"
export require="${require} __file${aliases_file}"


# Setup the root alias
__key_value	--key 'root' \
		--delimiter ":" \
		--comment "# Main admin(s) for this host" \
		--value "${root_email}" \
		--onchange 'newaliases' \
		--file "${aliases_file}" \
		root_mail_alias
