#!/usr/pkg/bin/python3.13

import sys

if sys.platform == "darwin" and sys.version_info[1] == 7:
    # Patch find_library so it finds OpenGL library on newer macOS versions
    from ctypes import util
    orig_util_find_library = util.find_library
    def new_util_find_library( name ):
        res = orig_util_find_library( name )
        if res: return res
        return '/System/Library/Frameworks/'+name+'.framework/'+name
    util.find_library = new_util_find_library


from fsboot import init

init()

import os
import sys

if sys.platform == "linux" and os.getenv("QT_QPA_PLATFORM", "") == "":
    os.environ["QT_QPA_PLATFORM"] = "xcb"

if sys.version_info[0] < 3 or sys.version_info[1] < 2:
    print("You need at least Python 3.2 to run FS-UAE Launcher")
    sys.exit(1)

if os.environ.get("FSGS_PYTHONPATH", ""):
    sys.path.insert(0, os.environ.get("FSGS_PYTHONPATH"))

if os.getcwd().startswith("C:\\msys64\\home\\"):
    os.environ["PATH"] = "C:\\msys64\\mingw64\\bin;" + os.environ["PATH"]


def print_version():
    import launcher.version

    print(launcher.version.VERSION)


def frozen_qpa_platform_plugin_path():
    pass
    # import fsboot
    #
    # os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.path.join(
    #     fsboot.executable_dir(), "platforms"
    # )


def frozen_python_libs():
    pass
    # import fsboot

    # libs_dirs = [fsboot.executable_dir()]
    # if sys.platform == "darwin":
    #     # Add .app/Contents/Resources/Python to libs_dirs
    #     libs_dir = os.path.abspath(
    #         os.path.join(fsboot.executable_dir(), "..", "Resources", "Python")
    #     )
    #     print(libs_dir, os.path.exists(libs_dir))
    #     if os.path.exists(libs_dir):
    #         libs_dirs.append(libs_dir)
    #     libs_dir = os.path.abspath(
    #         os.path.join(
    #             fsboot.executable_dir(), "..", "..", "..", "..", "..", "Python"
    #         )
    #     )
    #     print(libs_dir, os.path.exists(libs_dir))
    #     if os.path.exists(libs_dir):
    #         libs_dirs.append(libs_dir)
    # else:
    #     libs_dir = os.path.abspath(
    #         os.path.join(fsboot.executable_dir(), "..", "..", "Python")
    #     )
    #     print(libs_dir, os.path.exists(libs_dir))
    #     if os.path.exists(libs_dir):
    #         libs_dirs.append(libs_dir)

    # for libs_dir in libs_dirs:
    #     for item in os.listdir(libs_dir):
    #         if item.endswith(".zip"):
    #             path = os.path.join(libs_dir, item)
    #             print("adding", path)
    #             sys.path.insert(0, path)


def frozen_requests_ca_cert():
    import fsboot

    data_dirs = [fsboot.executable_dir()]

    # FIXME: share-dir?
    # FIXME: expose variable outside of module?
    # get_data_dirs() ? get_data_dir()?

    if sys.platform == "darwin":
        data_dir = os.path.abspath(
            os.path.join(fsboot.executable_dir(), "..", "Resources", "Data")
        )
        print(data_dir, os.path.exists(data_dir))
        if os.path.exists(data_dir):
            data_dirs.append(data_dir)
        data_dir = os.path.abspath(
            os.path.join(
                fsboot.executable_dir(), "..", "..", "..", "..", "..", "Data"
            )
        )
        print(data_dir, os.path.exists(data_dir))
        if os.path.exists(data_dir):
            data_dirs.append(data_dir)
    else:
        data_dir = os.path.abspath(
            os.path.join(fsboot.executable_dir(), "..", "..", "Data")
        )
        print(data_dir, os.path.exists(data_dir))
        if os.path.exists(data_dir):
            data_dirs.append(data_dir)


def main():
    if getattr(sys, "frozen", False):
        frozen_qpa_platform_plugin_path()
        frozen_python_libs()
        frozen_requests_ca_cert()

    if "--version" in sys.argv:
        print_version()
        sys.exit(0)

    import launcher.apps

    launcher.apps.main()


main()
