#!/bin/sh

set -e

if [ "$1" = "purge" ] ; then

  #
  # Remove the exim4 config, but this doesn't break exim, as the Debian
  # autogenerated one should still be in place.
  #
  if [ -e /etc/exim4/exim4.conf ] ; then
    echo "I: Removing /etc/exim4/exim4.conf"
    rm -f /etc/exim4/exim4.conf /etc/exim4/exim4.conf~
  fi

  #
  # Remove the old generated dovecot.conf
  #
  if [ -e /etc/dovecot/dovecot.conf.dpkg-symbiosis ] ; then
    echo "I: Removing dovecot.conf.dpkg-symbiosis"
    rm -f /etc/dovecot/dovecot.conf.dpkg-symbiosis /etc/dovecot/dovecot.conf~
  fi 

  #
  # Remove dovecot.conf if it has been generated by Symbiosis, but not if dovecot is still installed.
  #
  if ( grep -q '/etc/dovecot/symbiosis.d' /etc/dovecot/dovecot.conf ) ; then
    if [ -x /usr/sbin/dovecot ] ; then
      echo "I: Leaving /etc/dovecot/dovecot.conf in place as dovecot still appears to be installed"
    else
      echo "I: Removing /etc/dovecot/dovecot.conf"
      rm /etc/dovecot/dovecot.conf
    fi
  fi
  
  #
  # Configure supplementary groups for clamav and freshclam
  #
  if ( groups clamav | grep -q Debian-exim ) ; then
    echo "I: Removing clamav from Debian-exim group"
    deluser clamav Debian-exim > /dev/null 2>&1
  fi

  #
  # If the clamav user has no more supplementary groups, set AllowSupplementaryGroups to false.
  #
  add_groups=`groups "clamav" | awk -F ':' '{print $2}' | sed -e s/clamav//`

  if [ -z "$add_groups"  ] && ( grep -q '^AllowSupplementaryGroups \+true' /etc/clamav/clamd.conf ) ; then
    echo "I: Disallowing clamav to operate using its supplementary groups"
    sed -i -e 's/^AllowSupplementaryGroups .*$/AllowSupplementaryGroups false/' /etc/clamav/clamd.conf
  fi
fi

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

#DEBHELPER#

exit 0
