#!/bin/sh

set -e

basedir="/etc/local-REDPILL"

domains="$(cat "$basedir/realms" | sed 's/#.*//')"

exit1() {
	response="${1:+Error: }${1:-Internal error!}"
	echo "$response"
	exit 1
}

for domain in $domains; do
	[ -r "$basedir/$domain/mailnetworks" ] || exit1 "Could not read input file $basedir/$domain/mailnetworks"
done

echo '' > "$basedir/postfix/permit_mx_backup_networks"
for domain in $domains; do
	cat "$basedir/$domain/mailnetworks" >> "$basedir/postfix/permit_mx_backup_networks"
done