#!/bin/bash

# KNEL Postfix Module
# Configures postfix for email delivery

set -euo pipefail

echo "Running postfix module..."

# Stop postfix
systemctl stop postfix

# Configure postfix for local mail relay
if [[ -f ./configs/postfix_generic ]]; then
    cp ./configs/postfix_generic /etc/postfix/generic
    postmap /etc/postfix/generic
fi

# Set postfix configuration
postconf -e "inet_protocols = ipv4"
postconf -e "inet_interfaces = 127.0.0.1"
postconf -e "mydestination = 127.0.0.1"
postconf -e "relayhost = tsys-cloudron.knel.net"
postconf -e "smtp_generic_maps = hash:/etc/postfix/generic"

# Restart postfix
systemctl start postfix

# Test mail delivery
echo "Test email from $(hostname)" | mail -s "Test from $(hostname)" root

echo "Postfix module completed"