#!/bin/bash
# TMDA Maintenance
# Go through the list of users and generate a TMDA PENDING list for any
# user that is using TMDA (has a .tmda/config file)
#
DEBUG=0
DOIT=1
FLAGS="-C -b -T"
DATE=`date '+%b %d, %Y'`
HOSTNAME=`hostname`
IFS=":";
cat /etc/passwd |
(
while read LOGINID PASSWD PUID PGID NAME HOME USERSHELL
   do
   if [ -f $HOME/.tmda/config ]; then
      EMAIL="$LOGINID@$HOSTNAME"

      if [ "$DEBUG"x != "0"x ]; then
         echo "Processing $EMAIL"
      fi

      if [ "$DOIT"x = "1"x ]; then
         # silently and immediately delete all messages older than 14 days
         /bin/su - $LOGINID --command="/usr/bin/tmda-pending -q -b -d -O 14d"

         # mail a summary report of all new pending messages
         /bin/su - $LOGINID --command="/usr/sbin/tmda-pending-mail \"$NAME\" $LOGINID $HOSTNAME"
      fi

      # If there is a delivery error, notify me
      FAILURE_NOTICE=$HOME/TMDA_DELIVERY_FAILURE
      SUBECT="TMDA DELIVERY FAILURE ($DATE)"
      if [ -f $FAILURE_NOTICE ]; then
	mail -s "$SUBJECT" $EMAIL < $FAILURE_NOTICE
	rm -f $FAILURE_NOTICE
      fi
   fi
   done
)
