from conans import ConanFile, tools
import shutil


class QtConan(ConanFile):
    name = "qt"
    version = "5.12.10"
    license = "LGPL-3.0"
    author = "Gernot Walzl"
    url = "https://gernot-walzl.at/index.php?nav=C%2B%2B%2FQt"
    description = "Qt is a cute framework for creating graphical user interfaces"
    settings = "os", "compiler", "build_type", "arch"
    _modules = [
        "qt3d",
        "qtactiveqt",
        "qtandroidextras",
        "qtbase",
        "qtcanvas3d",
        "qtcharts",
        "qtconnectivity",
        "qtdatavis3d",
        "qtdeclarative",
        "qtdoc",
        "qtgamepad",
        "qtgraphicaleffects",
        "qtimageformats",
        "qtlocation",
        "qtmacextras",
        "qtmultimedia",
        "qtnetworkauth",
        "qtpurchasing",
        "qtquickcontrols",
        "qtquickcontrols2",
        "qtremoteobjects",
        "qtscript",
        "qtscxml",
        "qtsensors",
        "qtserialbus",
        "qtserialport",
        "qtspeech",
        "qtsvg",
        "qttools",
        "qttranslations",
        "qtvirtualkeyboard",
        "qtwayland",
        "qtwebchannel",
        "qtwebengine",
        "qtwebglplugin",
        "qtwebsockets",
        "qtwebview",
        "qtwinextras",
        "qtx11extras",
        "qtxmlpatterns"
    ]
    options = {
        "shared": [True, False],
        "commercial": [True, False],
        "build_tests": [True, False],
        "build_examples": [True, False],
        **{module: [True, False] for module in _modules}
    }
    default_options = {
        "shared": True,
        "commercial": False,
        "build_tests": False,
        "build_examples": False,
        **{module: False for module in _modules},
        "qtbase": True
    }
    short_paths = True

    def source(self):
        sourcefile = "qt-everywhere-src-%s.tar.xz" % self.version
        md5sum = "a781a0e247400e764c0730b8fb54226f"
        download = "https://download.qt.io/archive/qt/5.12/%s/single/%s" % (self.version, sourcefile)
        tools.get(download, md5=md5sum)
        shutil.move("qt-everywhere-src-%s" % self.version, "qt-src")

    def build(self):
        args = ["-prefix %s" % self.package_folder]

        if not self.options.shared:
            args.append("-static")

        if self.options.commercial:
            args.append("-commercial")
        else:
            args.append("-opensource")
        args.append("-confirm-license")

        if self.settings.build_type == "Debug":
            args.append("-debug")
        elif self.settings.build_type == "Release":
            args.append("-release")
        elif self.settings.build_type == "RelWithDebInfo":
            args.append("-release")
            args.append("-force-debug-info")

        if not self.options.build_tests:
            args.append("-nomake tests")
        if not self.options.build_examples:
            args.append("-nomake examples")

        for module in self._modules:
            if not getattr(self.options, module):
                args.append("-skip " + module)

        configure = "%s/qt-src/configure %s" % (self.source_folder, " ".join(args))
        if self.settings.compiler == "Visual Studio":
            with tools.vcvars(self):
                self.run(configure, run_environment=True)
                self.run("nmake", run_environment=True)
        else:
            self.run(configure, run_environment=True)
            self.run("make", run_environment=True)

    def package(self):
        if self.settings.compiler == "Visual Studio":
            with tools.vcvars(self):
                self.run("nmake install")
        else:
            self.run("make install")

        self.copy("LICENSE*", dst="licenses", src="qt-src")
        self.copy(".QT-*-LICENSE-AGREEMENT", dst="licenses", src="qt-src")
        for module in self._modules:
            if getattr(self.options, module):
                license_module = "licenses/%s" % module
                src_module = "qt-src/%s" % module
                self.copy("*LICENSE*", dst=license_module, src=src_module)
                self.copy("*COPYING*", dst=license_module, src=src_module)