[prev in list] [next in list] [prev in thread] [next in thread] 

List:       evolution
Subject:    [Evolution] Quick tool to import a directory of mbox-style files
From:       Joe Ammond <jra () twinight ! org>
Date:       2000-07-31 18:49:42
Message-ID: 20000731144942.K1422 () dawn ! twinight ! org
[Download RAW message or body]

I wanted to import my old ~/Mail directory with 15+ years of stored mail
into Evolution (a nice stress-test), so I came up with this quick and
dirty hack to mass-import a directory of mbox-style files and subdirectories
into the specified Evolution folder.  I'm sending it here in case it might
be useful to other Evolution users.

Useage: mbox-import /path/to/evolution/local/foldername.  It reads all
files and subdirectories, and copies them into the specified folder.  It
descends into subdirectories and does the Right Thing (I hope).  Each
mbox file is created as a subfolder of the specified folder, with 
subdirectories created as sub-subfolders.

Caveats: Only been tested with mbox-style folders.  Will copy any file,
regardless of whether it's an mbox folder.  Copies, not moves.  Shouldn't
touch the original files.  Only tested on my machines (RH6.2, Solaris 7).
YMMV. 

ja.
-- 
Joe Ammond                                                  jra@twinight.org


#!/bin/sh

#
# Recursively process a directory containing Unix mbox files
# into an Evolution-style directory hierarchy.  A quick hack.
#
# (C) 2000 Joe Ammond <jra@twinight.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# A quick hack, to say the least.
#


# Some initial variables
PROG=$0

# A useful function (stolen from perl).
die() {
	echo "$PROG: error: $1"
	exit 1
}

#
# XML_cruft
# 
# Takes a directory, and creates the folder-metadata.xml file.

XML_cruft () {

	# Create the necessary cruft.   I like here documents.
	cat > $1/folder-metadata.xml <<-EOF
		<?xml version="1.0"?>
		<efolder>
  		<type>mail</type>
		</efolder>
EOF
	# Slightly cleaner than || with a here document.
	[ "$?" = "0" ] || die 'cat failed in XML_cruft'
}
	

#
# Target_file creates the target Evolution directory, the XML
# metadata, and copies the mbox-style file over.  It doesn't
# check to see that what it copies over is actually mbox data.
#
# Arguments
#
#	$1 = target directory
#	$2 = source file to copy to the target directory

Target_file () {
	target=$1
	source=$2

	mkdir -p $target/$source			|| \
		 die 'mkdir failed in Target_file'
	cp $source $target/$source/mbox		||  \
		die 'cp failed in Target_file'
	XML_cruft $target/$source
}
	
#
# Target_directory sets up the necessary structure that Evolution needs
# to be able to recognize a folder with subfolders.
#
# Arguments
#
#	$1 = target directory
#	$2 = source file to copy to the target directory

Target_directory () {
	target=$1
	source=$2

	mkdir -p $target/$source/subfolders	|| \
		die 'mkdir failed in Target_directory.'
	touch $target/$source/mbox		|| \
		die 'touch failed in Target_directory.'
	XML_cruft $target/$source
	(cd $source ; Process $target/$source/subfolders)
}

#
# Iterate through all the files and subdirectories in the current
# directory, processing them into Evolution.
#
# Arguments
#
#	$1 = target directory

Process () {
	target=$1

	for file in * ; do
		if [ -f $file ]; then
			echo -n "Processing $file..."
			Target_file $target $file
			echo "done."
		elif [ -d $file ]; then
			echo "Descending into $file"
			Target_directory $target $file
		else
			echo "skipping non-regular file $file."
		fi
	done
}


#
# Main program

# getopt and friends aren't really necessary.  Maybe for v2
if [ "$#"x != "1x" ]; then
	echo "Usage: $PROG target_directory"
	echo "Imports all files and subdirectories into the target directory,"
	echo "and creates all the neccessary supporting structure that
	echo "Evolution needs to function."
	exit 1
fi

# If target doesn't exist, create it.
if [ ! -d $1 ]; then
	mkdir -p $1 			|| \
		die 'mkdir of $1 failed'
	echo 'Created $1.'
fi

# Abort if the target already has subfolders.
if [ -d $1/subfolders ]; then
	echo "$1 already has an Evolution-style"
	echo "subfolders/ directory, aborting."
	exit 1
fi

# Start the process
Target_directory $1 .

exit 0


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic