#!/bin/sh
#
# /etc/cron.daily/1postfix
#
# This file is run on a daily basis to perform checks on your postfix
# configuration and to analyse your mail logs.
#
# The file is named "1postfix" to ensure that it is run before logrotate
# when the /var/log/maillog files are rotated.
#
# The file is also flagged by rpm as a configuration file, so if you modify
# it, further upgrades to postfix will not be overwriten.
#
# More information at http://jimsun.linxnet.com/postfix_contrib.html
#
# uncomment the next line to completely disable the daily cronjob
# exit 0

# Default location of the log file
LOGFILE=/var/log/maillog
if [ ! -f ${LOGFILE} ]; then
    # check Mandrake's default mail log location
    [ -f /var/log/mail/info ] && LOGFILE=/var/log/mail/info
endif
EXECUTABLE=/usr/sbin/pflogsumm.pl
PFLOGSUMTMP=/etc/postfix/pfls.tmp.$$	# safer directory than /tmp
#OPTIONS="--verp_mung=2 --problems_first --iso_date_time  -q -u 10 -h 10 -d yesterday"
OPTIONS="--verp_mung=2 --problems_first --iso_date_time  -q -u 10 -h 10"
SUBJECT="`hostname` Daily Mail Report for `LC_TIME=C date +\"%A, %d %B %Y\"`"
POSTMASTER="postmaster@`hostname`"
SENDMAIL=/usr/sbin/sendmail
SENDMAIL_OPTS="-oi -t"
POSTFIX=/usr/sbin/postfix

# Clean up when done or when aborting.
trap "test -x ${PFLOGSUMTMP} && rm -f ${PFLOGSUMTMP}" 0 1 2 3 15 

# uncomment the next line to disable retrieval of mail statistics
# exit 0

[ -x ${POSTFIX} ] || { echo "${POSTFIX} not found or not executable - exiting"; exit 0; }
${POSTFIX} check

# Invoke pflogsumm.pl to gather statistics
# see http://jimsun.linxnet.com/postfix_contrib.html for further info.

[ -x ${EXECUTABLE} ] || { echo "${EXECUTABLE} not found or not executable - exiting"; exit 0; }

# WARNING - This is a bit of a hack
#
# pflogsumm.pl needs Date::Calc to work so we do the following:
# Check if our installation works with Date::Calc
# - if so, use pflogsumm.pl directly
# - if not filter out the Date::Calc useage and use the resulting script

perl -e "use Date::Calc;" 2>/dev/null || {
    # strip out Date::Calc from pflogsumm.pl
    sed -e '/# ---Begin:/,/# ---End:/d' < ${EXECUTABLE} > ${PFLOGSUMTMP}
    chmod u=rx,go= ${PFLOGSUMTMP}
    EXECUTABLE=${PFLOGSUMTMP}
}

# Double check that the sendmail binary exists before proceeding
[ -x ${SENDMAIL} ] || { echo "${SENDMAIL} not found or not executable - exiting"; exit 0; }
(
cat <<EOF
To: ${POSTMASTER}
Subject: ${SUBJECT}

EOF
LC_TIME=C ${EXECUTABLE} ${OPTIONS} ${LOGFILE} ) | ${SENDMAIL} ${SENDMAIL_OPTS}
