#!/bin/sh
# Convert an mh folder recursively into a maildir with subfolders. 
# Based on a script by Adam Sampson <ats@offog.org>
# Recursion added by Moritz Wilhelmy <moritz plus maildir at wzff dot de>
# Depends On: safecat

if [ $# != 2 ] ; then
	echo "Usage: $0 mhdir maildir/"
	echo " For instance: $0 ~/Mail ~/Maildir/ (slash at end of maildir is important)"
	exit 1
fi

mhdir="$1"
maildir="$2"
# imported mails are flagged as "old":
md_import() { safecat "$maildir/tmp" "$maildir/cur"; } 

# the following line requires safecat with the suffix patch applied
# (http://barfooze.de/~moritz/patches/safecat-1.13-suffix.diff)
# and marks all mails as seen when importing them into the maildir.
#md_import() { safecat "$maildir/tmp" "$maildir/cur" ':2,S'; } 

# imported mails are neither flagged as seen nor flagged as "old"
#md_import() { maildir "$maildir"; } 

echo "$mhdir -> $maildir"

mkdir -p "$maildir/new" "$maildir/cur" "$maildir/tmp" || exit 1
ls "$mhdir" | egrep '^[0-9]+$' | sort -n | while read n ; do
	md_import < "$mhdir/$n" || exit 1
done

for sub in "$mhdir"/*/; do
	[ ! -d "$sub" ] && break 
	sub="`basename "$sub"`"
	"$0" "$mhdir/$sub" "$maildir.$sub"
done

# $Id: mh2md.sh,v 1.1 2011/04/13 22:39:40 moritz Exp $
