#!/kolab/bin/perl
#
# script to read out the annotations for a user
# 	user.a@old.example.org
# and re-add them for a different domain and/or different user name
#       user.b@new.domain.com.
#
# USAGE:
#        1) edit user parameters below
#	 2) run script with OLD_UID NEW_UID
#
#  20061031 V0.2 transferannotations.pl emgaron+kolab@ribbrock.org
#       re-write to do full annotations transfer for given user
#  	Tested with Kolab Server 2.0.4 (src) -> 2.1.0beta2 (dst).
#
#  20060220 initial listannotations.pl bernhard@intevation.de	
#  	Tested with Kolab Server 2.1.0beta1.
#
#  This script is Free Software. 

use Cyrus::IMAP::Admin;
use strict;

#############################################################################
# USER PARAMETERS                                                           #
# Modify the values below to reflect your setup                             #
#############################################################################

my $old_domain="OLD.DOMAIN";
my $new_domain="NEW.DOMAIN"; # leave empty if domain doesn't change
my $src_server="SRC_SERVER.DOMAIN";
my $dst_server="DST_SERVER.DOMAIN";
my $src_admin="manager";
my $dst_admin="manager";
my $src_passwd="PASSWD";
my $dst_passwd="PASSWD";

#############################################################################
# END OF USER PARAMETERS                                                    #
#############################################################################
my $old_user;
my $new_user;
my $new_mailbox;
my $kolab_vendor;
my %info;

if ($#ARGV < 0 || $#ARGV > 1)
  {
    print ("\nUsage: $0 OLD_UID NEW_UID\n");
    print ("         If NEW_UID is empty, UID is not changed.\n\n");
    exit;
  }

$old_user = $ARGV[0];
$new_user = $ARGV[1];

my $src_client = Cyrus::IMAP::Admin->new("$src_server",);
$src_client->authenticate(-user => "$src_admin", -password => "$src_passwd");

my $dst_client = Cyrus::IMAP::Admin->new("$dst_server",);
$dst_client->authenticate(-user => "$dst_admin", -password => "$dst_passwd");

# transferring all annotations
# need to call getinfo twice, as there is no simple pattern that gets
# imapd to return the info without name clashes (e.g. testuser vs.
# testuser1 and suchlike)
%info = ($src_client->getinfo("user/$old_user/*\@$old_domain"),
	 $src_client->getinfo("user/$old_user\@$old_domain"));
foreach my $attrib (sort keys %info) {
  if($attrib =~ /\/vendor\/kolab\/(folder-type|incidences-for)/)
    {
      $new_mailbox = $attrib;

      if($new_domain ne "")
	{
	  # replace domain in mailbox
	  $new_mailbox =~ s/$old_domain/$new_domain/g;
	}

      if($new_user ne "")
	{
	  # replace user in mailbox
	  $new_mailbox =~ s/$old_user/$new_user/g;
	}

      # extract pure mailbox string
      $new_mailbox =~ s/^[^{]*{//;
      $new_mailbox =~ s/}[^}]*$//;

      # extract kolab vendor string
      $kolab_vendor = $attrib;
      $kolab_vendor =~ s/^[^}]*}//;

      #printf("\$dst_client->mboxconfig('%s', '%s', '%s')\n",
      #	     $new_mailbox, $kolab_vendor, $info{$attrib});
      printf("%s (%s) -> %s\n", $new_mailbox, $kolab_vendor,
                                       $info{$attrib});

      $dst_client->mboxconfig("$new_mailbox", "$kolab_vendor",
			      "$info{$attrib}");
    }
}
