#!/bin/sh

# This script is a replacement for the postfix/sendmail binary
# that floods /var/spool/postfix/maildrop directory with unwanted
# files even when the emails are undeliverable. As there's no
# pickup process running on the system, the directory doesn't get
# cleaned and grows indefinitely. That eventually leads to the
# inode depletion on the /var filesystem.
# The purpose of this script is to redirect all sendmail produced
# emails to /var/log/messages via the system logger.

myname=`basename $0`
if [ "$myname" = "newaliases" ]; then
  exit 0
elif [ "$myname" = "mailq" ]; then
  echo "Mail queue is empty"
  exit 0
fi

args="${@}"
dot_means_end=1
while [ ! "$1" = "" ]; do
  [ "$1" = "-oi" ] && dot_means_end=0
  shift
done

text=""
while read -r line; do
  [ ${dot_means_end} -eq 1 ] && [ "${line}" = "." ] && break
  text="${text}|${line}"
done

/bin/logger -p daemon.warning -t sendmail "|${args}${text}|"
