#!/bin/sh

TIMESTAMP=`date "+%b-%d-%Y-%T"`

# Update /etc/postfix/transport
#------------------------------

/usr/sbin/postmap /etc/postfix/transport

# Set default configuration
#--------------------------

logger -p local6.notice -t installer "app-smtp-core - setting default configuration"

if [ -e /etc/postfix/main.cf ]; then
    cp /etc/postfix/main.cf /var/clearos/smtp/backup/main.cf.$TIMESTAMP
fi

cp /usr/clearos/apps/smtp/deploy/main.cf /etc/postfix/main.cf

if [ -e /etc/postfix/master.cf ]; then
    cp /etc/postfix/master.cf /var/clearos/smtp/backup/master.cf.$TIMESTAMP
fi

cp /usr/clearos/apps/smtp/deploy/master.cf /etc/postfix/master.cf

# Clean out default aliases
#--------------------------

if [ -f /etc/aliases ]; then
    SILLYALIASES="adm all bin canna daemon dbus decode desktop dovecot dumper falken fax foo ftp games gopher hostmaster info ingres lp manager marketing moof moog news nfsnobody nobody noc nscd nut ntp office operator ops pcap postgres privoxy pvm radiusd radvd rpc rpcuser rpm sales smmsp staff squid support sync system tech toor usenet uucp vcsa webalizer wnn www xfs"
    for ALIAS in $SILLYALIASES; do
        CHECK=`grep "^$ALIAS:" /etc/aliases`
        if [ -n "$CHECK" ]; then
            logger -p local6.notice -t installer "app-smtp-core - disabling obscure alias: $ALIAS"
            sed -i -e "s/^$ALIAS/# $ALIAS/" /etc/aliases
        fi
    done
    /usr/bin/newaliases
fi

# Initialize domain name with default
#------------------------------------

/usr/clearos/apps/smtp/deploy/initialize

# Set default start/boot
#-----------------------

logger -p local6.notice -t installer "app-smtp-core - enabling SMTP server"
chkconfig postfix on

logger -p local6.notice -t installer "app-smtp-core - starting SMTP server"
if [ -z `/sbin/pidof master` ]; then
    service postfix start
else
    service postfix condrestart
fi

logger -p local6.notice -t installer "app-smtp-core - enabling saslauth server"
chkconfig saslauthd on

logger -p local6.notice -t installer "app-smtp-core - starting saslauthd server"
service saslauthd start
