# migration script from mokeladmin-newforms to mokeladmin-rewrite

from django.contrib.auth.models import Group as DjangoGroup

from oldmail.models import User as oldUser, \
        Domain as oldDomain, \
        Recipient as oldRecipient, \
        Alias as oldAlias, \
        UserProfile as oldUserProfile, \
        ConfigScheme as oldConfigScheme


from mail.models import Server, Config, Domain, User, Alias

stdserver, created = Server.objects.get_or_create(name="mail.mokelbu.de",
        smtp=True,
        xmpp=True,
        imap=True,
        pop3=True)
if not created:
    print "stdserver %s existed" % stdserver

stdgroup, created = DjangoGroup.objects.get_or_create(name="StdAdmin")
if not created:
    print "stdgroup %s existed" % stdgroup


# migrate Domains
for od in oldDomain.objects.all():
    print "doing %s" % od
    try: 
        d = Domain.objects.get(name=od.name)
    except Domain.DoesNotExist:
        # migrate cfgs
        oldcfg = od.configscheme
        newcfg, created = Config.objects.get_or_create(name=oldcfg.name,
                smtp_server=stdserver,
                xmpp_server=None,
                imap_server=stdserver,
                pop3_server=stdserver,
                info="automatically imported cfg from oldcfgobject %s" % oldcfg,
                prefix=oldcfg.maildirprefix,
                uid=oldcfg.uid,
                gid=oldcfg.gid,
                admins=stdgroup)
        if not created:
            print "%s already existed" % newcfg
        print newcfg

        d, created = Domain.objects.get_or_create(name=od.name,
                max_mailboxes=od.maxmailboxes,
                max_quota=od.maxquota,
                created_date=od.created_date,
                enabled=od.enabled,
                transport=od.transport,
                config=newcfg)
        if not created:
            print "%s already existed" % d

# migrate accounts
for oU in oldUser.objects.all():
    print "doing %s" % oU
    # mail.domain-object aus altem user.domain ezeugen
    dom = Domain.objects.get(name=oU.domain.name)
    # mail.config object aus altem oldmail.configscheme erzeugen
    conf = Config.objects.get(name=oU.domain.configscheme.name)

    # Account initialisieren
    try:
        nUs = User.objects.get(local_part=oU.address, domain__name=oU.domain.name)
    except User.DoesNotExist:
        nUs, created = User.objects.get_or_create(local_part=oU.address,
                domain=dom,
                enabled=oU.enabled,
                password=oU.password,
                real_name=oU.realname,
                alternative_mail=oU.alternative_mail,
                quota=oU.maxquota,
                created_date=oU.created_date,
                config=conf,
                smtp=oU.smtp,
                imap=oU.imap,
                pop3=oU.pop3,
                xmpp=oU.xmpp)
        if not created:
            print "account %s already there" % nUS
        nUs.save()

# migrate all aliases and recipients
for oAl in oldAlias.objects.all():
    print "doing Alias %s " % oAl
    dom = Domain.objects.get(name=oAl.domain.name)
    print dom
    try: 
        nAl = Alias.objects.get(local_part=oAl.address, domain=dom)
    except Alias.DoesNotExist:
        nAl, created = Alias.objects.get_or_create(local_part=oAl.address,
                domain=dom)
        if not created:
            print "%s already existed" % nAl
    nAl.recipients = ", ".join(x.full_address for x in oAl.recipients.all())
    nAl.save()


