#!/bin/sh # # $Id: mbox-to-maildir,v 1.2 2008/05/17 07:00:42 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 # [MJS 23 Apr 2004] Improvements during my 1.5GB transfer ## 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 maildirmake lockfile mktemp cat true formail procmail || die $0: required binaries not present ## Check our first parameter (the mbox to read from) MBOX="$1" test -n "$MBOX" || die Usage: $( basename $0 ) mbox maildir 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. ## Check our second parameter (the maildir to write to) MAILDIR="$2" test -e "$MAILDIR" || maildirmake $MAILDIR || die $0: maildirmake failed. test -d "$MAILDIR" || die $0: Thing \"$MAILDIR\" is not a directory. test -w "$MAILDIR" || die $0: Maildir \"$MAILDIR\" is not writeable. test -O "$MAILDIR" || die $0: Maildir \"$MAILDIR\" not owned by effective uid. ## Generate our procmailrc file rcfile=$(mktemp) || die $0: mktemp failed. cat > $rcfile < $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 $rcfile < $tmpfile || die $0: formail failed - data in $tmpfile. ## All successful, delete $tmpfile and exit rm -f $tmpfile $rcfile || die $0: error deleting \$tmpfile $tmpfile exit 0