#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# NoctisMail Outbound Smart Relay Configurator (Sovereign MailChannels Alternative)
# Routes outbound hosting mail from cPanel (Exim) or Plesk/Linux (Postfix)
# through your NoctisMail server over Port 587 STARTTLS with DKIM, Brand Studio,
# ClamAV scanning, anti-spam rate limiting, and 52-RBL circuit breaker!
#
# Usage:
#   sudo bash noctismail-smart-relay.sh \
#     --panel cpanel|plesk|postfix \
#     --relay-host mail.yourdomain.com \
#     --relay-port 587 \
#     --username relay@yourdomain.com \
#     --password 'YOUR_APP_PASSWORD'
# =============================================================================

PANEL=""
RELAY_HOST=""
RELAY_PORT="587"
USERNAME=""
PASSWORD=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --panel) PANEL="$2"; shift 2 ;;
    --relay-host) RELAY_HOST="$2"; shift 2 ;;
    --relay-port) RELAY_PORT="$2"; shift 2 ;;
    --username) USERNAME="$2"; shift 2 ;;
    --password) PASSWORD="$2"; shift 2 ;;
    *) shift ;;
  esac
done

if [[ -z "$PANEL" || -z "$RELAY_HOST" || -z "$USERNAME" || -z "$PASSWORD" ]]; then
  echo "Usage: sudo bash noctismail-smart-relay.sh --panel cpanel|plesk|postfix --relay-host mail.yourdomain.com --relay-port 587 --username user@domain.com --password 'APP_PASSWORD'"
  exit 1
fi

if [[ "$PANEL" == "cpanel" ]]; then
  echo "Configuring cPanel Exim Outbound Smart Relay -> ${RELAY_HOST}:${RELAY_PORT}..."
  cat > /etc/exim.conf.local.noctismail <<EOF
@AUTH@
noctismail_login:
  driver = plaintext
  public_name = LOGIN
  client_send = : ${USERNAME} : ${PASSWORD}

@PREROUTERS@
noctismail_smart_route:
  driver = manualroute
  domains = ! +local_domains
  transport = noctismail_smtp
  route_list = * ${RELAY_HOST}::${RELAY_PORT}
  no_more

@TRANSPORTSTART@
noctismail_smtp:
  driver = smtp
  port = ${RELAY_PORT}
  hosts_require_auth = *
  hosts_require_tls = *
EOF
  cp -a /etc/exim.conf.local /etc/exim.conf.local.bak.noctis 2>/dev/null || true
  cp -f /etc/exim.conf.local.noctismail /etc/exim.conf.local
  /scripts/buildeximconf
  /scripts/restartsrv_exim
  echo "✅ cPanel Exim Smart Relay active! All outbound hosting mail now routes through NoctisMail (${RELAY_HOST}:${RELAY_PORT})."
else
  echo "Configuring Postfix / Plesk Outbound Smart Relay -> [${RELAY_HOST}]:${RELAY_PORT}..."
  postconf -e "relayhost = [${RELAY_HOST}]:${RELAY_PORT}"
  postconf -e "smtp_sasl_auth_enable = yes"
  postconf -e "smtp_sasl_password_maps = hash:/etc/postfix/noctismail_sasl_passwd"
  postconf -e "smtp_sasl_security_options = noanonymous"
  postconf -e "smtp_tls_security_level = encrypt"
  echo "[${RELAY_HOST}]:${RELAY_PORT} ${USERNAME}:${PASSWORD}" > /etc/postfix/noctismail_sasl_passwd
  chmod 0600 /etc/postfix/noctismail_sasl_passwd
  postmap /etc/postfix/noctismail_sasl_passwd
  systemctl reload postfix || service postfix reload
  echo "✅ Postfix / Plesk Smart Relay active! All outbound mail now routes through NoctisMail (${RELAY_HOST}:${RELAY_PORT})."
fi
