# 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, Account, Address

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)

    # address initialisieren
    nAd, created = Address.objects.get_or_create(address=oU.address,
            domain=dom,
            enabled=oU.enabled)
    if not created:
        print "addr %s already there" % nAd

    # Account initialisieren
    try:
        nAc = Account.objects.get(name="%s@%s" % (oU.address, oU.domain.name))
    except Account.DoesNotExist:
        nAc, created = Account.objects.get_or_create(name="%s@%s" %
                (oU.address, oU.domain.name),
                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,
                primary_address=nAd, 
                prefix=oU.domain.name)
        if not created:
            print "account %s already there" % nAc
        nAc.admins.add(stdgroup)
        nAd.account = nAc
        nAd.save()
        nAc.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: 
        nAd = Address.objects.get(address=oAl.address, domain=dom)
    except Address.DoesNotExist:
        nAd, created = Address.objects.get_or_create(address=oAl.address,
                domain=dom)
        if not created:
            print "%s already existed" % nAd
    nAd.recipients = ", ".join(x.full_address for x in oAl.recipients.all())
    nAd.save()

