#! /bin/sh -v

# This script is used to detect mail which has been on smail's
# queue for more than 3 days. A message to the sender is auto-
# matically generated and the original message is returned with
# it. The action is logged.

# Argument 1 must be the name of the smail spool directory, e.g.
# /var/spool/smail/input, and argument 2 must be the name of a
# log file for recording what this script does, for example,
# /var/spool/smail/log/stalelog. There is an assumption that
# there exists a directory called msglog as a sister directory
# to the input directory (which is how smail normally works).

# A typical cron entry (for root) is therefore
#
# 5 0 * * * /usr/local/smail/util/stalemail /usr/spool/smail/input \
#           /usr/spool/smail/log/stalelog

# When running smail with more than one spool directory, this
# script has to be called for each one.

# Written by Philip Hazel <ph10@cus.cam.ac.uk> January 1992
# Feb 13 1992: Bugs in decoding spool files fixed


dir=$1
log=$2

datestring=`date "+%h %d %H:%M"`

files=`find $dir -mtime +2 -print`
if [ "$files" = "" ]; then exit; fi
set $files

while [ "$1" != "" ]; do
  exec < $1
  read sender
  read dummy
  while read line; do
    if [ "$line" = "" ]; then break; fi

    case $line in
      -f   )  read sender; continue ;;
      -*   )  continue ;;
    esac

    recipient="$line"
    while read nextrecipient; do
      if [ "$nextrecipient" = "" ]; then break 2; fi
      recipient="$recipient, $nextrecipient"
    done
  done

  if [ "$sender" != "" ] ; then
    msglog=`dirname $1`/../msglog/`basename $1`

(cat 2<&0 <<End ; \
  cat 2<&0 $msglog; \
  echo ""; \
  echo "----------------------- Message Follows ------------------------"; \
  echo ""; \
  cat) | \
  /usr/local/bin/smail -f postmaster $sender postmaster
From: postmaster
To: $sender
Subject: Mail not sent for 3 or more days

The mail system at $visible_name has been unable to deliver your message
to one or more of

$recipient

for three or more days. It is therefore being returned to you. If you need
any help with electronic mail, please contact postmaster@$visible_name

--------------------- Message Log Follows ----------------------

End

    echo "$datestring " `ls -l $1 | cut -c33-45` $sender >> $log
    cat $msglog >> $log
    /bin/rm -f $1 $msglog
  fi
  shift
done


