#! /usr/bin/python

from sh import ldd
from sh import mkdir, cp, rm, qmake
import glob
import sys
import os

LD_LIBRARY_PATH = "./libs"
QT_PLUGIN_PATH = "./plugins"

plugins = ['iconengines', 'imageformats', 'platforminputcontexts',
           'platforms', 'platformthemes', 'styles', 'xcbglintegrations']


QT_ICONENGINES_PLUGIN_PATH = "./plugins/iconengines"
QT_IMAGEFORMATS_PLUGIN_PATH = "./plugins/imageformats"
QT_PLATFORMINPUTCONTEXTS_PLUGIN_PATH = "./plugins/platforminputcontexts"
QT_QPA_PLATFORM_PLUGIN_PATH = "./plugins/platforms"
QT_PLATFORMTHEMES_PLUGIN_PATH = "./plugins/platformthemes"
QT_STYLE_PLUGIN_PATH = "./plugins/styles"
QT_XCBGLINTEGRATIONS_PLUGIN_PATH = "./plugins/xcbglintegrations"

blockList = [
    "ld-linux.so.2",
    "ld-linux-x86-64.so.2",
    "libanl.so.1",
    "libasound.so.2",
    "libBrokenLocale.so.1",
    "libcidn.so.1",
    "libcom_err.so.2",
    "libcrypt.so.1",
    "libc.so.6",
    "libdl.so.2",
    "libdrm.so.2",
    "libexpat.so.1",
    "libfontconfig.so.1",
    "libfreetype.so.6",
    "libgcc_s.so.1",
    "libgdk_pixbuf-2.0.so.0",
    "libgio-2.0.so.0",
    "libglapi.so.0",
    "libglib-2.0.so.0",
    "libGL.so.1",
    "libgobject-2.0.so.0",
    "libgpg-error.so.0",
    "libharfbuzz.so.0",
    "libICE.so.6",
    "libjack.so.0",
    "libkeyutils.so.1",
    "libm.so.6",
    "libmvec.so.1",
    "libnsl.so.1",
    "libnss_compat.so.2",
    "libnss_db.so.2",
    "libnss_dns.so.2",
    "libnss_files.so.2",
    "libnss_hesiod.so.2",
    "libnss_nisplus.so.2",
    "libnss_nis.so.2",
    "libp11-kit.so.0",
    "libpango-1.0.so.0",
    "libpangocairo-1.0.so.0",
    "libpangoft2-1.0.so.0",
    "libpthread.so.0",
    "libresolv.so.2",
    "librt.so.1",
    "libSM.so.6",
    "libstdc++.so.6",
    "libthai.so.0",
    "libthread_db.so.1",
    "libusb-1.0.so.0",
    "libutil.so.1",
    "libuuid.so.1",
    "libX11.so.6",
    "libxcb.so.1",
    "libz.so.1"
]


def getDynamicLibrarys(fpath):
    result = ldd(fpath)
    solist = result.split("\n")
    dynamicLibrarys = set()
    for so in solist:
        soPaths = so.split("=>")
        if len(soPaths) == 2:
            dynamicLibrarys.add(soPaths[1].split(" (")[0].strip())
    return dynamicLibrarys


def getAllDirDynamicLibrarys(fdir):
    plugins = glob.glob(fdir + "/*.so")
    alllibrarys = set()
    for so in plugins:
        if so:
            librarys = getDynamicLibrarys(so)
            alllibrarys |= librarys
    return alllibrarys


def main():

    versionPath = os.getcwd() + "/src/agent/version"

    version = ""
    with open(versionPath, "r") as f:
        version = f.read()
    print version

    installPath = sys.argv[1] + "/" + version.strip()
    print(installPath)
    mkdir(installPath, "-p")

    qt_plugins = qmake("-query", "QT_INSTALL_PLUGINS").strip()

    checker_execDynamicLibrarys = getDynamicLibrarys(
        os.getcwd() + "/src/checker/deepin-legalization-checker")
    agent_execDynamicLibrarys = getDynamicLibrarys(
        os.getcwd() + "/src/agent/deepin-legalization-agent")

    alllibrarys = set()
    alllibrarys |= checker_execDynamicLibrarys
    alllibrarys |= agent_execDynamicLibrarys

    print("======binary count========", len(alllibrarys))

    for key in plugins:
        librarys = getAllDirDynamicLibrarys(qt_plugins + "/" + key)
        alllibrarys |= librarys

    print("======all count(binary + plugins)========", len(alllibrarys))

    filterLibrarys = set()
    for fSoPath in alllibrarys:
        block = False
        for blockSo in blockList:
            if fSoPath.endswith(blockSo):
                block = True

        if not block:
            filterLibrarys.add(fSoPath)

    alllibrarys = filterLibrarys
    print("======all count(binary + plugins)========", len(filterLibrarys))

    ld_path = installPath + "/" + "libs"
    rm(ld_path, "-rf")
    mkdir(ld_path)
    for fSoPath in alllibrarys:
        print "cp ", fSoPath, "===========>", ld_path
        cp(fSoPath, ld_path)

    qt_plugins = qmake("-query", "QT_INSTALL_PLUGINS").strip()

    plugins_path = installPath + "/" + "plugins"

    rm(plugins_path, "-rf")
    for key in plugins:
        mkdir(plugins_path + "/" + key, "-p")
        print "cp ", qt_plugins + "/" + key, "===========>", plugins_path
        cp(qt_plugins + "/" + key, plugins_path, "-vrf")


if __name__ == '__main__':
    main()
