#!/bin/sh

#
# Recursively process a directory containing Unix mbox files
# into an Evolution-style directory hierarchy.  A quick hack.
#
# (C) 2000 Joe Ammond <<A HREF="mailto:jra@twinight.org">jra@twinight.org</A>>
#
# 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
ORIG=`pwd`

# 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'
	#ln `pwd`/$source $target/$source/mbox		||  \
	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


