#! /bin/sh
###############################################################################
#
#  $Id: movemail,v 1.4 1997/12/09 21:02:00 behanw Exp $
#
#  'movemail' for filtered mailboxes
#
#  Written by: Brian White <bcwhite@pobox.com>
#         and: Behan Webster <behanw@pobox.com>
#
#  This file has been placed in the public domain.
#

fromfile=$1;
tofile=$2;


#
# Constants for this program (user settings are taken from user's procmailrc)
#
XMESSAGE=/usr/bin/X11/xmessage


#
# Extract settings from the procmailrc files...
#    LOCKFILE, MAILDIR, DEFAULT, MBOXDIR
#
# MBOXDIR is not part of procmail, but must be set for movemail to know
# where you want things moved to.
#
# Note that your DEFAULT mailbox is not necessarily the same as $fromfile
# which is often set to your spool file (/var/spool/mail/$USER) by the
# calling program.  If you don't use your spool file as your DEFAULT
# mailbox, you can get your mail filter program to write to your spool
# file so that xbiff or netscape knows you have new mail.
#
for rcfile in /etc/procmailrc $HOME/.procmailrc; do
    if [ -r $rcfile ]; then
        eval `sed -n -e 's/#.*$//' -e '/^[A-Z_]*=/p' $rcfile`
    fi
done

if [ -z "$LOCKFILE" ]; then
    echo "$0: LOCKFILE is not set" >&2
    exit 1;
fi
if [ -z "$MAILDIR" ]; then
    echo "$0: MAILDIR is not set" >&2
    exit 1;
fi
if [ -z "$DEFAULT" ]; then
    echo "$0: DEFAULT is not set" >&2
    exit 1;
fi
if [ -z "$MBOXDIR" ]; then
    echo "$0: MBOXDIR is not set" >&2
    exit 1;
fi


#
# Determine the "default" in-box:  Mail in this box will be copied to the
# $tofile specified on the command line.  All other boxes will just be
# copied to the MBOXDIR directory.
#
inbox=`basename $DEFAULT`


#
# Go to mail directory; touch default destination box to show that something
# happened (netscape likes this).
#
cd $MAILDIR
touch $tofile


#
# Locate all the mailbox files (may be in sub-directories)
#
mboxlist=`find . \( -name ".??*" -prune \) -o -type f ! -name ".*" -print | sed -e 's|^./||' | sort`


#
# Tell the user what messages are available
#
if [ -x $XMESSAGE -a "${MOVEDMSG:-no}" = "yes" ]; then
    grep -c "^From " $mboxlist non-existant-box 2>/dev/null \
	| awk -F: '{ if ( $2 > 0 ) printf("%-4d%s\n", $2, $1) }' \
	| $XMESSAGE -file - &
fi


#
# Lock mail, move each box if it has any data
#
if lockfile -r0 -l3600 $LOCKFILE 2>/dev/null; then
    trap "rm -f $LOCKFILE" 1 2 3 15
    for box in $mboxlist; do
#       echo "Moving box '$box'..." >&2
        if [ -s $box ]; then
            if [ "$box" = "$inbox" ]; then
                out=$tofile
            else
                out="$MBOXDIR/$box"
            fi
            umask 077
            cat $box >>$out && cat /dev/null >$box
        fi
    done
    rm -f $LOCKFILE
fi


#
# Truncate spool file (first parameter) if there is no valid mail in it.
# We may have written something to the spool file just so xbiff or Netscape
# will know that there is new mail to retrieve.
#
lockfile -l3600 -ml
if [ -w $fromfile ]; then
    if [ `grep -c '^From ' $fromfile` -eq 0 ]; then
        cat /dev/null > $fromfile
    fi
fi
lockfile -mu


#
# We're outta here...
#
exit 0
