#!/usr/bin/python2

from gi.repository import Gio
import os

OLD_MEDIA_KEYS_PATH = "/org/cinnamon/settings-daemon/plugins/media-keys/"
OLD_MEDIA_KEYS_SCHEMA = "org.cinnamon.settings-daemon.plugins.media-keys"
NEW_MEDIA_KEYS_SCHEMA = "org.cinnamon.desktop.keybindings.media-keys"

OLD_WM_KEYBINDINGS_PATH = "/org/cinnamon/muffin/keybindings/"
OLD_WM_KEYBINDINGS_SCHEMA = "org.cinnamon.muffin.keybindings"
NEW_WM_KEYBINDINGS_SCHEMA = "org.cinnamon.desktop.keybindings.wm"

OLD_CUSTOM_KEYS_PARENT_SCHEMA = "org.cinnamon.keybindings"
OLD_CUSTOM_KEYS_BASENAME = "/org/cinnamon/keybindings/custom-keybindings"
OLD_CUSTOM_KEYS_SCHEMA = "org.cinnamon.keybindings.custom-keybinding"

NEW_CUSTOM_KEYS_PARENT_SCHEMA = "org.cinnamon.desktop.keybindings"
NEW_CUSTOM_KEYS_BASENAME = "/org/cinnamon/desktop/keybindings/custom-keybindings"
NEW_CUSTOM_KEYS_SCHEMA = "org.cinnamon.desktop.keybindings.custom-keybinding"

def migrate(old, new, array):
    old_settings = Gio.Settings.new(old)
    new_settings = Gio.Settings.new(new)

    old_list = old_settings.list_keys()
    new_list = new_settings.list_keys()

    for k in new_list:
        try:
            try:
                if k in old_list and old_settings.get_user_value(k) is not None:
                    if array:
                        new_settings.set_value(k, old_settings.get_value(k))
                    else:
                        ov = old_settings.get_string(k)
                        new_settings.set_strv(k, (ov,))
            except:
                if k in old_list:
                    if array:
                        if len(old_settings.get_strv(k)) > 0:
                            new_settings.set_value(k, old_settings.get_value(k))
                    else:
                        ov = old_settings.get_string(k)
                        if ov != "":
                            new_settings.set_strv(k, (ov,))
        except:
            print "Problem migrating key %s from %s to %s, skipping it" % (k, old, new)
    Gio.Settings.sync()

def migrate_custom_keys():
    old_parent = Gio.Settings.new(OLD_CUSTOM_KEYS_PARENT_SCHEMA)
    new_parent = Gio.Settings.new(NEW_CUSTOM_KEYS_PARENT_SCHEMA)

    custom_list = old_parent.get_strv("custom-list")
    new_parent.set_strv("custom-list", custom_list)
    old_parent.set_strv("custom-list", None)

    for entry in custom_list:
        old_custom_path = OLD_CUSTOM_KEYS_BASENAME + "/" + entry + "/"
        old_schema = Gio.Settings.new_with_path(OLD_CUSTOM_KEYS_SCHEMA, old_custom_path)

        new_custom_path = NEW_CUSTOM_KEYS_BASENAME + "/" + entry + "/"
        new_schema = Gio.Settings.new_with_path(NEW_CUSTOM_KEYS_SCHEMA, new_custom_path)

        new_schema.set_string("name", old_schema.get_string("name"))
        new_schema.set_string("command", old_schema.get_string("command"))
        new_schema.set_strv("binding", (old_schema.get_string("binding"),))

        old_schema.delay()
        old_schema.reset("name")
        old_schema.reset("command")
        old_schema.reset("binding")
        old_schema.apply()
        Gio.Settings.sync()

if os.path.exists(os.path.expanduser("~/.cinnamon/.keybindings-migration-2.4-complete-do-not-erase")):
    print "Migration has been performed already for this user."
    exit(0)

print "Migrating keybindings from previous release..."

try:
    source = Gio.SettingsSchemaSource.get_default()

    if source.lookup(OLD_MEDIA_KEYS_SCHEMA, True) != None:
        migrate(OLD_MEDIA_KEYS_SCHEMA, NEW_MEDIA_KEYS_SCHEMA, False)
        print "  ... media keybindings: done."

    if source.lookup(OLD_WM_KEYBINDINGS_SCHEMA, True) != None:
        migrate(OLD_WM_KEYBINDINGS_SCHEMA, NEW_WM_KEYBINDINGS_SCHEMA, True)
        print "  ... wm keybindings: done."

    if source.lookup(OLD_CUSTOM_KEYS_PARENT_SCHEMA, True) != None:
        migrate_custom_keys()
        print "  ... custom keybindings: done."

    Gio.Settings.sync()

    open(os.path.expanduser("~/.cinnamon/.keybindings-migration-2.4-complete-do-not-erase"), "w").close()

except Exception, detail:
    print "  !! %s" % detail

print "  Done."

exit(0)
