#!/bin/bash

#
# This script has to use bash as it uses arrays.
#

set -e

#
# Skip, if we are not in "configure" state
#
if [ "$1" != "configure" ]; then
        echo "I: Skipping configuration"
        exit 0
fi


if [ ! -f /etc/ssl/private/exim4-dhparams.pem ] ; then
  #
  # generate dhparams if they don't exist already
  #
  /etc/cron.weekly/symbiosis-email --verbose
fi

#
# Configure supplementary groups for clamav and freshclam
#
if ! ( groups clamav | grep -q Debian-exim ) ; then
  #
  #  Add the user
  #
  echo "I: Adding clamav user to Debian-exim group"
  adduser clamav Debian-exim > /dev/null 2>&1
fi

#
# Debian-exim needs to be part of the ssl-cert group, so it can read the
# cert/key in /etc/ssl/ssl.combined.
#
if ! ( groups Debian-exim | grep -q ssl-cert ) ; then
  echo "I: Adding Debian-exim user to ssl-cert group"
  adduser Debian-exim ssl-cert > /dev/null 2>&1
fi

if ( grep -q '^AllowSupplementaryGroups \+false' /etc/clamav/clamd.conf ) ; then
  echo "I: Allowing clamav to operate using its supplementary groups"
  #
  # Editing this in place is OK because clamav-daemon will pick this up in its maintscripts
  #
  sed -i -e 's/^AllowSupplementaryGroups .*$/AllowSupplementaryGroups true/' /etc/clamav/clamd.conf
fi 

#
# These are here to aid wheezy-upgrades.
#
if ( grep -q '^PidFile ' /etc/clamav/clamd.conf ) ; then
  echo "I: Removing PidFile setting from clamav config"

  sed -i -e 's/\(^PidFile .*$\)/# \1/' /etc/clamav/clamd.conf
fi 

if ( grep -q '^PidFile ' /etc/clamav/freshclam.conf ) ; then
  echo "I: Removing PidFile setting from freshclam config"

  sed -i -e 's/\(^PidFile .*$\)/# \1/' /etc/clamav/freshclam.conf
fi 


#
# Set the TMPDIR variable for clamav.
#
echo "TMPDIR='/var/tmp/'" >> /etc/default/clamav-daemon

#
#  Enable the spamassassin module
#
if [ -e /etc/default/spamassassin ]; then
    sed -i -e 's/^\(ENABLED=\)0/\11/' /etc/default/spamassassin
fi

#
# Enable spamassassin daily updates
#
if [ -e /etc/default/spamassassin ]; then
    sed -i -e 's/^\(CRON=\)0/\11/' /etc/default/spamassassin
fi

#
#  Make sure ownership is correct on our file
#
touch /etc/exim4/rewrites
chown Debian-exim /etc/exim4/rewrites

#
#  Rebuild exim4
#
if [ -e /etc/exim4/Makefile ]; then
    cd /etc/exim4 && make exim4.conf
fi

# 
# Dovecot ships with its own config.
#
package="symbiosis-email"
conf="/etc/dovecot/dovecot.conf"
theirfile="$conf.dpkg-symbiosis-orig"

#
# Add the diversion if the file has not been generated by Symbiosis, and if the
# diversion doesn't exist.
#
if [ -e $conf ] && ( ! grep -q '/etc/dovecot/symbiosis.d/' $conf ) && \
  ( ! dpkg-divert --list "$package" | grep -xFq "diversion of $conf to $theirfile by $package" ) ; then

  dpkg-divert --divert "$theirfile" --rename --package "$package" --add "$conf"

elif [ -e ${conf}~ ] && ( ! grep -q '/etc/dovecot/symbiosis.d/' ${conf}~ ) ; then

  #
  # This is an attempt to tidy up from previous versions of symbiosis-email
  # that would have moved the original dovecot.conf to dovecot.conf~
  #
  echo "I: Moving old backed-up ${conf}~ to ${theirfile}"
  mv ${conf}~ ${theirfile}

  if ! dpkg-divert --list "$package" | grep -xFq "diversion of $conf to $theirfile by $package" ; then
    dpkg-divert --divert "$theirfile" --package "$package" --add "$conf"
  fi
fi


#
#  Rebuild dovecot
#
if [ -e /etc/dovecot/Makefile ] ; then
    cd /etc/dovecot && make dovecot.conf
fi

#
#  Restart all deamons
#
for i in exim4 dovecot spamassassin clamav-daemon clamav-freshclam symbiosis-email-dict-proxy symbiosis-email-poppassd; do
    # spamassassin + clamav-daemon might not be installed.  Wrap the invokation.
    service $i restart || true
done

#DEBHELPER#
exit 0
