#!/usr/bin/perl -w
#
# Deletes a single user's inbox and mail/ folders from Cyrus
#
#

if ($#ARGV < 0) {
	print "Usage: $0 username\n";
	print "  Deletes username's inbox and mail/ folders from Cyrus\n";
	exit(-1);
}

my $username = $ARGV[0];
my $adminuser = "cyrus";
my $adminpw = "XXXXXX";
my $server = "localhost";

use Cyrus::IMAP::Admin;


# Connect to Cyrus
$imap = Cyrus::IMAP::Admin->new($server) || die "Unable to connect to $server";

if (! $imap) {
    die "Error creating IMAP connection object\n";
}

$imap->authenticate(-user => $adminuser, 
		-mechanism => "LOGIN",
		-password => $adminpw,
		);

if ($imap->error) {
	print "ERROR: " . $imap->error . "\n";
	exit(-1);
}

print "Successfully connected to IMAP server.\n";


# Verify that the mailbox exists
if (! $imap->listmailbox("user.$username")) {
	print "ERROR: Mailbox 'user.$username' does not exist\n";
	exit(-1);
}


# Give ourselves delete rights on the mailbox
$imap->setaclmailbox("user.$username", "cyrus", "c");
if ($imap->error) {
	print "ERROR: " . $imap->error . "\n";
	exit(-1);
}


# Delete the mailbox
$imap->deletemailbox("user.$username");
if ($imap->error) {
	print "ERROR: " . $imap->error . "\n";
	exit(-1);
}


print "Successfully deleted mailbox 'user.$username'\n";

