#!/bin/sh -
#
# Quick hack for fetchmail to locally spool messages.
#
# To spool:
#     fetchmail -mda "fetchspool -t %T"
# To de-spool
#     fetchspool -f
#
# If you try it mail me - I've never tested it ...
#
# Robert de Bath  <robert@mayday.cix.co.uk>

MAILSPOOL=/var/spool/mailspool

if [ "$1" != "-f" ]
then
   if [ "$1" = "-t" ]
   then ADDR="$2"
   else ADDR="$1"
   fi

   cat - > $MAILSPOOL/tmp.$$ 				   || exit 1
   mv tmp.$$ "$MAILSPOOL/msg.`date +%j%H%M%S`$$.to.$ADDR"  || exit 1

   exit 0
else
   for i in $MAILSPOOL/msg.*.to.*
   do
      [ -f "$i" ] || continue
      TO="echo \"$i\" | sed 's/^msg.[^.]*.to.//'"
      /usr/lib/sendmail -oem "$TO" << "$i" ||
      {
         echo "Sendmail failed on `basename \"$i\"`"
	 continue
      }
      rm -f "$i"
   done
   exit 0
fi
