#!/bin/sh
if [ -z "$1" -o ! -d ~/"$1" ] 
then
echo "please enter the name of an existing mailbox folder"
exit
fi

rm ~/$1/osso_to_claws_* -rf
i=0
box=""

for file in `ls ~/apps/email/Mail/Inbox/`
do
	if [ `echo "$file" | grep "Inbox"` ] ; 
	then
		oldbox=$box
		box=`echo $file | cut -f1 -d^`
		if [ "$box" != "$oldbox" -a $i -ne 0 ]
		then
			echo "$i emails saved on ~/$1/osso_to_claws_$oldbox"
		fi
		if [ ! -d ~/"$1"/osso_to_claws_$box/ ] 
		then
			i=0
			mkdir ~/$1/osso_to_claws_$box/
			if [ $? -ne 0 ]
			then
				echo "Unable to create the ~/$1/osso_to_claws_$box/ directory"
				exit
			else
				echo "~/$1/osso_to_claws_$box/ directory created"
			fi
		fi
		num=`echo $file | cut -f3 -d^`
		if [ `echo "$num" | grep "hdr"` ]
		then
			num=`echo $num | cut -f1 -d.`
		fi
		cp ~/apps/email/Mail/Inbox/$file ~/$1/osso_to_claws_$box/$num

		i=$(($i + 1))

		
	fi
done
echo "$i emails saved on ~/$1/osso_to_claws_$box"
