#!/usr/bin/perl
##
# Taken from some mbox2imap.pl script found on the net and modded to fit my needs...
#
# Be sure to change the imap array below with your credentials..
#
# wildcat - 2009
##
use File::Find;
use Mail::IMAPClient;
use Mail::Box::Mbox;
use Mail::Message::Convert::MailInternet;
if ($#ARGV != 1) { 
  print "Syntax: <mbox file> <imap folder>\n"; 
  exit
}

# open connection
my $imap = Mail::IMAPClient->new(
	Server => 'mail.ians.be',
	User => 'user@domain.com',
	Password => 'password') || die "Could not connect to IMAP server.\n";
# push all messages in all folders (sorting is only so that a folder is created before the subfolders)
$l = $ARGV[0];
$f = $ARGV[1];
$f =~ s/\//\./g;
$f = "INBOX.$f";
if (!$imap->exists("$f")) {
	print "WARNING: Creating folder " . $f . " since it does not exist on the server.\n";
	$imap->create($f) or print "ERROR: Could not create folder.\n";
}
# Now copy all files from the local store to the IMAP store
my $box = Mail::Box::Mbox->new("folder" => $l);
my $conv = Mail::Message::Convert::MailInternet->new;
print $f . ": " . $imap->message_count($f) . "\n"; # debug output only
my $count = 1;
foreach ($box->messages()) {
	$imap->append($f, $conv->export($_)->as_string()); # as_string ensures RFC 822 format
	print $count++ . "\n";
}
print $f . ": " . $imap->message_count($f) . "\n";
$imap->close();
