#!/bin/bash

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

postfix_dir="/etc/postfix"
target_file=$postfix_dir"/mail_satellite.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 global fallback-mail-domain with hostname..." 1>&2

  fallback_mail_hostname=''
  fallback_mail_domain=$(ymc_get_hostname_from_dns_txt fallback-mail-domain basics)
  if [ $? -eq 0 ] && [ "$fallback_mail_domain" != '' ]; then
    fallback_mail_hostname=$(ymc_get_local_short_hostname)'.'$fallback_mail_domain
    host $fallback_mail_hostname 2>/dev/null 1>/dev/null
    if [ $? -ne 0 ]; then
      echo "WARNING: Failed to to get an A-record for $fallback_mail_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 final fallback
      fallback_mail_hostname=$(ymc_get_local_full_hostname)
      if [ $? -ne 0 ]; then
        fallback_mail_hostname=''
      fi
    fi
  else
    echo "WARNING: Failed to get the fallback-mail-domain - 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 final fallback
    fallback_mail_hostname=$(ymc_get_local_full_hostname)
    if [ $? -ne 0 ]; then
      fallback_mail_hostname=''
    fi
  fi

  if [ "$fallback_mail_hostname" != '' ]; then
    echo "WARNING: A fallback mailname has been chosen - please keep in mind that this server might fail to deliver mails to some destinations!" 1>&2
    mail_hostname=$fallback_mail_hostname
  fi
fi

if [ "$mail_hostname" == '' ]; then
  echo "ERROR: COuld not determine a valid full mailname" 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 '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 = mail-server-outbound.cluster' >> $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 'recipient_delimiter = +' >> $target_file
#echo 'inet_interfaces = loopback-only' >> $target_file
echo 'inet_interfaces = 127.0.0.1, '$(ymc_get_primary_ip_for_network lan) >> $target_file

echo 'mailbox_command = procmail -a "$EXTENSION"' >> $target_file
echo 'inet_protocols = ipv4' >> $target_file

