#!/bin/sh

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

# Update alias maps
#------------------

CHECK=`/usr/sbin/postconf virtual_alias_maps 2>/dev/null | grep imap-aliases`
if [ -z "$CHECK" ]; then
    logger -p local6.notice -t installer "app-imap-core - updating alias maps"
    /usr/sbin/postconf -e "virtual_alias_maps = \$alias_maps, \$virtual_maps, ldap:/etc/postfix/imap-aliases.cf, ldap:/etc/postfix/imap-groups.cf"
fi

# PAM check
#----------

CHECK=`grep clearos/imap.d /etc/pam.d/imap`
if [ -z "$CHECK" ]; then
    logger -p local6.notice -t installer "app-imap-core - applying IMAP PAM configuration"
    [ -e /etc/pam.d/imap ] && cp /etc/pam.d/imap /var/clearos/imap/backup/imap.pam.$TIMESTAMP
    cp /usr/clearos/apps/imap/deploy/imap.pam /etc/pam.d/imap
fi

CHECK=`grep clearos/imap.d /etc/pam.d/pop`
if [ -z "$CHECK" ]; then
    logger -p local6.notice -t installer "app-imap-core - applying POP PAM configuration"
    [ -e /etc/pam.d/pop ] && cp /etc/pam.d/pop /var/clearos/imap/backup/pop.pam.$TIMESTAMP
    cp /usr/clearos/apps/imap/deploy/pop.pam /etc/pam.d/pop
fi

# Add root to PAM authorize list
#-------------------------------

CHECK=`grep ^root /etc/clearos/imap.d/authorize`
if [ -z "$CHECK" ]; then
    logger -p local6.notice -t installer "app-imap-core - adding root to PAM authorize list"
    echo "root" >> /etc/clearos/imap.d/authorize
fi

# Postfix LDAP hooks - run template through the LDAP synchronize system
#----------------------------------------------------------------------

/usr/sbin/ldap-synchronize

# Restart postfix
#----------------

/sbin/service postfix condrestart >/dev/null 2>&1
