# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.22)
project(pyside-tools)

include(cmake/PySideToolsSetup.cmake)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
    # create Qt6AndroidBindings.jar
    if (NOT DEFINED ANDROID_SDK_ROOT)
        message(FATAL_ERROR "Please provide the location of the Android SDK directory via "
                            "your toolchain file")
    endif()
    if (NOT DEFINED ANDROID_PLATFORM)
        message(FATAL_ERROR "Please provide the location of the Android Platform API level via "
                            "your toolchain file eg: android-31")
    endif()
    include(cmake/PySideAndroid.cmake)
    create_and_install_qt_javabindings()
else()
    set(files ${CMAKE_CURRENT_SOURCE_DIR}/pyside_tool.py
            ${CMAKE_CURRENT_SOURCE_DIR}/metaobjectdump.py
            ${CMAKE_CURRENT_SOURCE_DIR}/project.py
            ${CMAKE_CURRENT_SOURCE_DIR}/qml.py
            ${CMAKE_CURRENT_SOURCE_DIR}/qtpy2cpp.py
            ${CMAKE_CURRENT_SOURCE_DIR}/deploy.py
            ${CMAKE_CURRENT_SOURCE_DIR}/android_deploy.py
            ${CMAKE_CURRENT_SOURCE_DIR}/requirements-android.txt)

    set(directories ${CMAKE_CURRENT_SOURCE_DIR}/deploy_lib
                    ${CMAKE_CURRENT_SOURCE_DIR}/project_lib)

    if(NOT NO_QT_TOOLS STREQUAL "yes")
        set(TOOLS_PATH "${QT6_INSTALL_PREFIX}/${QT6_HOST_INFO_BINDIR}")
        set(LIBEXEC_PATH "${QT6_INSTALL_PREFIX}/${QT6_HOST_INFO_LIBEXECDIR}")

        list(APPEND files "${LIBEXEC_PATH}/uic${CMAKE_EXECUTABLE_SUFFIX}"
                        "${LIBEXEC_PATH}/rcc${CMAKE_EXECUTABLE_SUFFIX}"
                        "${LIBEXEC_PATH}/qmltyperegistrar${CMAKE_EXECUTABLE_SUFFIX}"
                        "${LIBEXEC_PATH}/qmlimportscanner${CMAKE_EXECUTABLE_SUFFIX}"
                        "${LIBEXEC_PATH}/qmlcachegen${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/lrelease${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/lupdate${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/qmllint${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/qmlformat${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/qmlls${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/qsb${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/balsam${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/balsamui${CMAKE_EXECUTABLE_SUFFIX}"
                        "${TOOLS_PATH}/svgtoqml${CMAKE_EXECUTABLE_SUFFIX}")

        if (APPLE)
            list(APPEND directories "${TOOLS_PATH}/Assistant.app"
                                    "${TOOLS_PATH}/Designer.app"
                                    "${TOOLS_PATH}/Linguist.app")
        else()
            list(APPEND files "${TOOLS_PATH}/assistant${CMAKE_EXECUTABLE_SUFFIX}"
                            "${TOOLS_PATH}/designer${CMAKE_EXECUTABLE_SUFFIX}"
                            "${TOOLS_PATH}/linguist${CMAKE_EXECUTABLE_SUFFIX}")
        endif()
    endif()

    list(APPEND directories ${CMAKE_CURRENT_SOURCE_DIR}/qtpy2cpp_lib)

    # pyside6-rcc, pyside6-uic, pyside6-designer, shiboken and pyside6-lupdate entrypoints
    foreach(file ${files})
        if(EXISTS ${file})
            message(STATUS "Installing tool: ${file}")
            install(FILES "${file}"
                    DESTINATION bin
                    PERMISSIONS
                    OWNER_EXECUTE OWNER_WRITE OWNER_READ
                    GROUP_EXECUTE GROUP_READ
                    WORLD_EXECUTE WORLD_READ)
        else()
            message(WARNING "${file} does not exist. Hence, pyside6-${file} will not work")
        endif()
    endforeach()

    foreach(directory ${directories})
        install(DIRECTORY "${directory}"
                DESTINATION bin
                FILE_PERMISSIONS
                OWNER_EXECUTE OWNER_WRITE OWNER_READ
                GROUP_EXECUTE GROUP_READ
                WORLD_EXECUTE WORLD_READ
                PATTERN "android_utilities.py" EXCLUDE) # excluding the symlink
    endforeach()

    # dealing with android_utilities.py
    set(ANDROID_UTILITIES_REALPATH
        "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/cross_compile_android/android_utilities.py")
    message(STATUS "Installing ${ANDROID_UTILITIES_REALPATH}")
    install(FILES "${ANDROID_UTILITIES_REALPATH}"
            DESTINATION bin/deploy_lib/android
            PERMISSIONS
                OWNER_EXECUTE OWNER_WRITE OWNER_READ
                GROUP_EXECUTE GROUP_READ
                WORLD_EXECUTE WORLD_READ)
endif()
