#! /bin/sh -e
#
# cron script to clean up mailscanner and exim4 files
#
# $Id: local-clock,v 1.5 2002/03/13 02:18:38 bcwhite Exp $
#
# Written by Brian White <bcwhite@precidia.com>
# Modified by Stephane Leclerc <sleclerc@actionweb.fr>
#

# Exim output spool directory
# Exim input spool directory is managed by cron.daily/exim4-base 
SPOOLDIROUT="/var/spool/exim4"

# How many days to keep quarantined messages
q_days=7

# Load user-defaults
if [ -f /etc/default/mailscanner ]; then
    . /etc/default/mailscanner
fi

# Go to mailscanner spool directory
cd /var/spool/MailScanner

# Remove old files and directories
find quarantine -type f -ctime +$q_days    -exec rm -f {} \; >/dev/null 2>&1
find quarantine -type d -depth -mindepth 1 -exec rmdir {} \; >/dev/null 2>&1

# For /var/spool/exim4 (SPOOLDIROUT)
# run tidydb as Debian-exim:Debian-exim.
if [ -x /usr/sbin/exim_tidydb ]; then
  cd $SPOOLDIROUT/db || exit 1
  if ! find $SPOOLDIROUT/db -maxdepth 1 -name '*.lockfile' -or -type f \
    -printf '%f\0' | \
      xargs -0r -n 1 \
      start-stop-daemon --start --exec /usr/sbin/exim_tidydb \
      --chuid Debian-exim:Debian-exim -- $SPOOLDIROUT > /dev/null; then
    # if we reach this, invoking exim_tidydb from start-stop-daemon has
    # failed, most probably because of libpam-tmpdir being in use
    # (see #373786 and #376165)
    find $SPOOLDIROUT/db -maxdepth 1 -name '*.lockfile' -or -type f \
    -printf '%f\0' | \
    su - --shell /bin/bash \
         --command "xargs -0r -n 1 /usr/sbin/exim_tidydb $SPOOLDIROUT > /dev/null" \
         Debian-exim
  fi
fi

# Exit cleanly
exit 0