#!/bin/sh -e
#
# 2021 Joachim Desroches (joachim.desroches@epfl.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

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

CFG_DIR="/etc/opendkim"
CFG_FILE="$CFG_DIR/opendkim.conf"
service="opendkim"
case "$os" in
'alpine')
	:
	;;
'debian')
	CFG_DIR="/etc/dkimkeys"
	CFG_FILE="/etc/opendkim.conf"
	;;
'freebsd')
	CFG_DIR="/usr/local/etc/mail"
	CFG_FILE="$CFG_DIR/opendkim.conf"

	service="milter-opendkim"
	;;
*)
	printf "__opendkim does not yet support %s.\n" "$os" >&2
	printf "Please contribute an implementation if you can.\n" >&2
	exit 1
	;;
esac
export CFG_DIR

__package opendkim

# Required parameters
SOCKET="$(cat "${__object:?}/parameter/socket")"
export SOCKET

# Optional parameters
if [ -f "${__object:?}/parameter/basedir" ]; then
	BASEDIR="$(cat "${__object:?}/parameter/basedir")"
	export BASEDIR
fi

if [ -f "${__object:?}/parameter/canonicalization" ]; then
	CANON="$(cat "${__object:?}/parameter/canonicalization")"
	export CANON
fi

if [ -f "${__object:?}/parameter/subdomains" ]; then
	SUBDOMAINS="$(cat "${__object:?}/parameter/subdomains")"
	export SUBDOMAINS
fi

if [ -f "${__object:?}/parameter/umask" ]; then
	UMASK="$(cat "${__object:?}/parameter/umask")"
	export UMASK
fi

if [ -f "${__object:?}/parameter/userid" ]; then
	USERID="$(cat "${__object:?}/parameter/userid")"
	export USERID
fi

# Custom configuration handling.
if [ -f "${__object:?}/parameter/custom-config" ]; then
	CUSTOM_CONFIG="$(cat "${__object:?}/parameter/custom-config")"
	export CUSTOM_CONFIG
fi

# Debian: set configuration specific to debian packaging if no explicit value
# is requested.
if [ "$os" = "debian" ]; then
	# In Debian, opendkim runs as user "opendkim". A umask of 007 is required when
	# using a local socket with MTAs that access the socket as a non-privileged
	# user (for example, Postfix). You may need to add user "postfix" to group
	# "opendkim" in that case.

	# We only set UserID if it is not provided via custom configuration.
	if [ -z "$USERID" ] && echo "$CUSTOM_CONFIG" | grep -Eq '^UserID\s+\w+$'; then
		export USERID="opendkim"
	fi

	if [ -z "$UMASK" ] && echo "$CUSTOM_CONFIG" | grep -Eq '^UMask\s+\w+$'; then
		export UMASK="007"
	fi

	if ! echo "$CUSTOM_CONFIG" | grep -Eq '^PidFile\s+\w+$'; then
		export PIDFILE="/run/opendkim/opendkim.pid"
	fi
fi

# Boolean parameters
[ -f "${__object:?}/parameter/syslog" ] && export SYSLOG=yes

# Generate and deploy configuration file.
source_file="${__object:?}/files/opendkim.conf"
target_file="${CFG_FILE}"

mkdir -p "${__object:?}/files"

"${__type:?}/files/opendkim.conf.sh" >"$source_file"

require="__package/opendkim" __file "$target_file" \
	--source "$source_file" --mode 0644

require="__package/opendkim" __start_on_boot "${service}"

# Ensure Key and Signing tables exist and have proper permissions
key_table="${CFG_DIR}/KeyTable"
signing_table="${CFG_DIR}/SigningTable"

require="__package/opendkim" \
	__file "${key_table}" \
	--mode 444

require="__package/opendkim" \
	__file "${signing_table}" \
	--mode 444

require="__file${target_file} __file${key_table}
		__file${signing_table} __start_on_boot/${service}" \
	__check_messages opendkim \
		--pattern "^__file${target_file}" \
		--execute "service ${service} restart"
