#!/bin/bash

### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

postfix_dir="/etc/postfix"
fliwi_cluster_conf_subdir="fliwi"
target_file=$postfix_dir"/mail-server-inbound.main.cf.fliwi"


### Get the public hostname
mail_hostname=''
ip=$(ymc_get_primary_ip_for_network wan || ymc_get_primary_ip_for_network www)
if [ $? -eq 0 ]; then
  auto_public_hostname=$(ymc_get_ptr_full_hostname_for_ip $ip)
  if [ $? -eq 0 ]; then
    mail_hostname=$auto_public_hostname
  fi
fi
if [ "$mail_hostname" == '' ]; then
  echo "WARNING: Failed to get the systems public hostname - falling back to the local full hostname" 1>&2
  echo "         Mails to remote destination will most likely be dropped!" 1>&2
  ### Use the systems long hostname as an fallback
  fallback_mail_hostname=$(ymc_get_local_full_hostname)
  if [ $? -eq 0 ]; then
    mail_hostname=$fallback_mail_hostname
  fi
fi
if [ "$mail_hostname" == '' ]; then
  echo "ERROR: COuld not determine a valid full hostname" 1>&2
  exit 1
else
  echo "INFO: Setting $mail_hostname as the systems mail name"
  echo $mail_hostname > /etc/mailname
fi

echo "# Dynamic main.cf file for postfix(1) generated by $(basename $0)" > $target_file
echo "#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN" >> $target_file

echo 'myorigin = /etc/mailname' >> $target_file
echo 'myhostname = '$mail_hostname >> $target_file
echo 'smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)' >> $target_file
echo 'biff = no' >> $target_file
echo 'append_dot_mydomain = no' >> $target_file
echo 'delay_warning_time = 4h' >> $target_file
echo 'readme_directory = no' >> $target_file

echo 'smtpd_tls_cert_file = /etc/postfix/ssl/postfix.crt' >> $target_file
echo 'smtpd_tls_key_file = /etc/postfix/ssl/postfix.key' >> $target_file
echo 'smtpd_use_tls = yes' >> $target_file

echo 'alias_maps = hash:/etc/aliases' >> $target_file
echo 'alias_database = hash:/etc/aliases' >> $target_file
echo 'mydestination = $myhostname, localhost.cluster, localhost' >> $target_file

echo 'relayhost = ' >> $target_file
echo 'mynetworks = 127.0.0.0/8, 10.0.0.0/8' >> $target_file
echo 'mailbox_size_limit = 0' >> $target_file
echo 'message_size_limit = 0' >> $target_file
echo 'virtual_mailbox_limit = 0' >> $target_file

echo 'recipient_delimiter = +' >> $target_file
echo 'inet_interfaces = all' >> $target_file
echo 'mailbox_command = procmail -a "$EXTENSION"' >> $target_file
echo 'inet_protocols = ipv4' >> $target_file
echo 'virtual_mailbox_domains = mysql:'$postfix_dir'/'$fliwi_cluster_conf_subdir'/mysql-virtual-mailbox-domains.cf' >> $target_file
echo 'virtual_mailbox_maps = mysql:'$postfix_dir'/'$fliwi_cluster_conf_subdir'/mysql-virtual-mailbox-maps.cf' >> $target_file
echo 'virtual_alias_maps = mysql:'$postfix_dir'/'$fliwi_cluster_conf_subdir'/mysql-virtual-alias-maps.cf,mysql:'$postfix_dir'/'$fliwi_cluster_conf_subdir'/mysql-email2email.cf' >> $target_file
echo 'virtual_transport = dovecot' >> $target_file
echo 'dovecot_destination_recipient_limit = 1' >> $target_file
echo 'smtpd_sasl_type = dovecot' >> $target_file
echo 'smtpd_sasl_path = private/auth' >> $target_file
echo 'smtpd_sasl_auth_enable = yes' >> $target_file
echo 'smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination' >> $target_file
echo 'smtpd_tls_auth_only = yes' >> $target_file

echo 'smtpd_client_restrictions =' >> $target_file
echo '    permit_mynetworks' >> $target_file
echo '    permit_sasl_authenticated' >> $target_file
echo '    reject_unauth_pipelining' >> $target_file
echo '    reject_rbl_client bl.spamcop.net' >> $target_file
echo '    reject_rbl_client zen.spamhaus.org' >> $target_file
echo '    check_policy_service inet:127.0.0.1:10023' >> $target_file

echo 'header_checks = regexp:/etc/postfix/'$fliwi_cluster_conf_subdir'/header_checks.cf' >> $target_file

