#!/usr/bin/env python
import os
import re
import sys

def replace_one(txt, dst, src=None):
    if src is None:
        src = "K" + dst
    word_src = r"%s\b" % src

    txt = re.sub(r"find_dependency *\(" + word_src, "find_dependency(KF5" + dst, txt)
    txt = re.sub(r"find_package *\(" + word_src, "find_package(KF5" + dst, txt)
    txt = re.sub(r"(find_package *\( *KF5 [A-Za-z0-9\n ]*\s*)" + word_src, r"\1" + dst, txt, re.M)
    txt = re.sub(r"include *\(" + src + r"Macros", "include(KF5" + dst + "Macros", txt)
    txt = re.sub(r"KF5::" + src + r"\b", "KF5::" + dst, txt)
    return txt


def replace_all(path):
    with open(path) as f:
        txt = f.read()
    # tier1
    txt = replace_one(txt, "Archive")
    txt = replace_one(txt, "Codecs")
    txt = replace_one(txt, "ConfigCore")
    txt = replace_one(txt, "ConfigGui")
    txt = replace_one(txt, "Config")
    txt = replace_one(txt, "CoreAddons")
    txt = replace_one(txt, "DBusAddons")
    txt = replace_one(txt, "GuiAddons")
    txt = replace_one(txt, "IdleTime")
    txt = replace_one(txt, "ItemModels", "ItemModels")
    txt = replace_one(txt, "ItemViews", "ItemViews")
    txt = replace_one(txt, "JS")
    txt = replace_one(txt, "JobWidgets")
    txt = replace_one(txt, "Plotting")
    txt = replace_one(txt, "Solid", "Solid")
    txt = replace_one(txt, "Sonnet", "Sonnet")
    txt = replace_one(txt, "ThreadWeaver", "ThreadWeaver")
    txt = replace_one(txt, "WidgetsAddons")
    txt = replace_one(txt, "WindowSystem")
    txt = replace_one(txt, "GlobalAccel", "KF5GlobalAccel")
    # tier2
    txt = replace_one(txt, "DNSSD")
    txt = replace_one(txt, "Auth")
    txt = replace_one(txt, "Completion")
    txt = replace_one(txt, "Crash")
    txt = replace_one(txt, "DocTools")
    txt = replace_one(txt, "XsltKde")
    txt = replace_one(txt, "I18n")
    txt = replace_one(txt, "JobWidgets")
    txt = replace_one(txt, "Notifications")
    txt = replace_one(txt, "Wallet")
    # tier3
    txt = replace_one(txt, "Bookmarks")
    txt = replace_one(txt, "ConfigWidgets")
    txt = replace_one(txt, "Declarative")
    txt = replace_one(txt, "KCMUtils", "KCMUtils")
    txt = replace_one(txt, "KIOCore", "KIOCore")
    txt = replace_one(txt, "KIOFileWidgets", "KIOFileWidgets")
    txt = replace_one(txt, "KIOWidgets", "KIOWidgets")
    txt = replace_one(txt, "KIO", "KIO")
    txt = replace_one(txt, "Kross", "Kross")
    txt = replace_one(txt, "Su", "KDESu")
    txt = replace_one(txt, "WebKit")
    txt = replace_one(txt, "Emoticons")
    txt = replace_one(txt, "IconThemes")
    txt = replace_one(txt, "Init")
    txt = replace_one(txt, "JsEmbed")
    txt = replace_one(txt, "MediaPlayer")
    txt = replace_one(txt, "NewStuff")
    txt = replace_one(txt, "NotifyConfig")
    txt = replace_one(txt, "Parts")
    txt = replace_one(txt, "PrintUtils")
    txt = replace_one(txt, "Pty")
    txt = replace_one(txt, "Service")
    txt = replace_one(txt, "TextWidgets")
    txt = replace_one(txt, "UnitConversion")
    txt = replace_one(txt, "XmlGui", "XmlGui")
    # tier4
    txt = replace_one(txt, "Style")
    txt = replace_one(txt, "KDE4Support", "KDE4Support")
    # plasma
    txt = replace_one(txt, "Plasma", "Plasma")
    txt = replace_one(txt, "PlasmaQuick", "PlasmaQuick")

    with open(path, "w") as f:
        f.write(txt)


def main():
    if len(sys.argv) == 2:
        return replace_all(sys.argv[1])

    for root, dirs, names in os.walk('.'):
        for name in names:
            if name == "CMakeLists.txt" or name.endswith("Config.cmake.in") or name.endswith("Config.cmake"):
                path = os.path.join(root, name)
                print(path)
                replace_all(path)


if __name__ == "__main__":
    sys.exit(main())

