#!/bin/bash export PATH=/usr/local/bin:/usr/local/sbin:/usr/pkg/bin:/usr/pkg/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/home/pbraun/bin # # $Id: reprocess-mbox-via-procmail,v 1.2 2004/06/19 11:49:32 suter Exp $ # Copyright (C) 2003 Mark Suter # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # [MJS 17 Jul 2002] Based on example in procmail(1) # [MJS 5 Jun 2003] Reworked using "or die" technique ## Our "die" function (think perl) function die () { echo "$@" 1>&2 ; exit 1 ; } ## Set our PATH and test for needed binaries #export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin hash test basename lockfile mktemp cat true formail procmail || die $0: required binaries not present ## Check our single parameter (the mbox to reprocess) MBOX="$1" test -n "$MBOX" || die Usage: $( basename $0 ) filename test -f $MBOX || die $0: File \"$MBOX\" is not a plain file. test -r $MBOX || die $0: File \"$MBOX\" is not readable. test -s $MBOX || die $0: File \"$MBOX\" is empty. ## Move the data into a temporary file (locking in case it's our system mailbox) tmpfile=$(mktemp) || die $0: mktemp failed. lockfile -r2 -ml || die $0: Cannot lock your system mailbox. cat $MBOX > $tmpfile || die $0: Cannot copy \$MBOX $MBOX. true > $MBOX || die $0: Cannot zero \$MBOX $MBOX - copy in $tmpfile. lockfile -mu || die $0: Cannot unlock your system mailbox - data in $tmpfile. ## Reprocess our copy via procmail formail -s procmail < $tmpfile || die $0: formail failed - data in $tmpfile. ## All successful, delete $tmpfile and exit rm -f $tmpfile || die $0: error deleting \$tmpfile $tmpfile exit 0