#!/bin/sh
#
# Configure postfix
#
_mail_log="/var/log/mail.log"
_main_cf="/etc/postfix/main.cf"
_main_cf_default="/etc/postfix/main.cf.default"
_master_cf="/etc/postfix/master.cf"
_master_cf_tmp="/etc/postfix/master.cf.tmp"
_master_cf_orig="/etc/postfix/master.cf.orig"
_master_cf_default="/etc/postfix/master.cf.default"
_postfix_data_dir="/var/run/postfix"
_other_plist="/etc/MailServicesOther.plist"
_other_plist_default="/etc/MailServicesOther.plist.default"
_mail_user_settings="/var/db/.mailusersettings.plist"

echo "Configuring Postfix..."

# Create the mail.log if it doesn't exist
if [ ! -e "$_mail_log" ] ; then
  touch "$_mail_log"
fi

# Set mail.log ownership
if [ -e "$_mail_log" ] ; then
  chown root:admin "$_mail_log"
  chmod 640 "$_mail_log"
fi

# Create the mail user settings file if it doesn't exist
if [ ! -e "$_mail_user_settings" ] ; then
  touch "$_mail_user_settings"
fi
chown _postfix:mail $_mail_user_settings
chmod 660 $_mail_user_settings

# Create main.cf file
if [ ! -e "$_main_cf" ] ; then
  if [ -e "$_main_cf_default" ] ; then
    cp "$_main_cf_default" "$_main_cf"
    chmod 644 "$_main_cf"
  else
    echo "$_main_cf_default does not exist:  Could not create $_main_cf"
  fi
fi

# Create master.cf
if [ ! -e "$_master_cf" ] ; then
  if [ -e "$_master_cf_default" ] ; then
    cp "$_master_cf_default" "$_master_cf"
    chmod 644 "$_master_cf"
  else
    echo "$_master_cf_default does not exist:  Could not create $_master_cf"
  fi
fi

# Setup mail other settins file with defaults
if [ ! -e "$_other_plist" ] ; then
  if [ -e "$_master_cf_default" ] ; then
    cp "$_other_plist_default" "$_other_plist_default"
    chmod 644 "$_other_plist"
  else
    echo "$_other_plist_default does not exist:  Could not create $_other_plist"
  fi
fi

# Create new data directory
#  - Required by 2.5.5
if [ ! -d "$_postfix_data_dir" ] ; then
  mkdir "$_postfix_data_dir"
  chmod 755 "$_postfix_data_dir"
  chown _postfix "$_postfix_data_dir"
else
  chmod 755 "$_postfix_data_dir"
  chown _postfix "$_postfix_data_dir"
fi

# Set content_filter to nil in master.cf
#    This will override global settings
if [ -e "$_master_cf" ] ; then
  # Add the override
  sed '/^pickup/ a\
  \ \ -o content_filter=
  ' < $_master_cf > $_master_cf_tmp
  # If it succeeded, move original master.cf to master.cf.orig
  #    Then move new .tmp to master.cf
  if [ -e "$_master_cf_tmp" ] ; then
    mv $_master_cf $_master_cf_orig
    mv $_master_cf_tmp $_master_cf
  fi
fi

# Set postfix owner and group IDs
/usr/sbin/postconf -e mail_owner=_postfix
/usr/sbin/postconf -e setgid_group=_postdrop
/usr/sbin/postconf -e mailbox_size_limit=0
/usr/sbin/postconf -e mydestination='$myhostname, localhost.$mydomain, localhost, $mydomain'

# Set credentials for using URLAUTH with IMAP servers.
/usr/sbin/postconf -e imap_submit_cred_file=/private/etc/postfix/submit.cred
# Create submit.cred with either the same password dovecot is
# configured for, or an unguessable random password.
if [ ! -e /private/etc/postfix/submit.cred ] ; then
	hostname=`grep "^myhostname *=" /private/etc/postfix/main.cf | sed 's,.*= *,,'`
	if [ ! "$hostname" ] ; then
		hostname=`hostname`
	fi
	if [ -s /private/etc/dovecot/submit.passdb ] ; then
		pw=`grep "^submit:" /private/etc/dovecot/submit.passdb | sed 's,.*},,'`
	fi
	if [ ! "$pw" ] ; then
		pw=`dd if=/dev/urandom bs=256 count=1 | env LANG=C tr -dc a-zA-Z0-9 | cut -b 1-22`
	fi
	if [ "$pw" -a "$hostname" ]; then
		echo "submitcred version 1" > /private/etc/postfix/submit.cred
		echo "$hostname|submit|$pw" >> /private/etc/postfix/submit.cred
	fi
	chmod 600 /private/etc/postfix/submit.cred
fi

# Set spool permissions
/usr/sbin/postfix check > /dev/null 2>&1

# If postfix is not running we want to give it a quick start/stop so it creates all necessary sockets & pipes
POSTFIX_PID=`ps -cxU root | grep master | sed -n -e "s/[^0-9]*\([0-9]*\).*/\1/p"`
if [ "$POSTFIX_PID" = "" ] ; then
  /usr/sbin/postfix start > /dev/null 2>&1
  /usr/sbin/postfix stop > /dev/null 2>&1
fi

# Set log level to critical
sed 's/^mail\.\*/mail.warn/' < /etc/syslog.conf > /etc/syslog.conf.$$ && mv /etc/syslog.conf.$$ /etc/syslog.conf
