#! /bin/sh
# usage: uniq-mbox {output_file}
# uniq-mbox will remove duplicates from mbox on stdin, 
#    truncate the output_file and put uniq-ified mail there,
#    tell you the name of the tempfile where it left duplicate emails
#

me=`basename $0`
date=`date +%Y%m%d-%H%M%S`
uniqfile=$1

if test -z "$uniqfile"; then
    echo "usage: $me {output_file}" 1>&2
    exit 1
fi
: > $uniqfile
status=$?
if test $status != 0; then
    echo "$me Error: could not truncate output file $uniqfile" 1>&2
    exit 1
fi

procmailrc=`tempfile --prefix=pmrc`
dupfile=`tempfile --prefix=dups`
cachefile=`tempfile --prefix=cache`
trap "rm -f $procmailrc $cachefile" TERM INT

cat > $procmailrc <<EOF
# temporary procmailrc file generated by $me $date

# uncomment line below for debugging
# VERBOSE=on

:0 Whc: $cachefile.lock
| formail -D 32768 $cachefile

:0 a:
$dupfile

:0 :
$uniqfile

EOF

# formail runs procmail on each email on stdin
#
formail -s procmail -m $procmailrc

echo "mail is in $uniqfile" 1>&2
echo "duplicates are in $dupfile" 1>&2
sh -xc "rm -f $procmailrc $cachefile"
