# FIXME needed since qt_standard_project_setup() cannot be used due to issue
# building tests with GMock if CMAKE_AUTOMOC is enabled
# if(QT_VERSION_MAJOR LESS 6)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
# endif()

set(biometryd-qml_SOURCES
    device.h
    device.cpp
    converter.h
    converter.cpp
    fingerprint_reader.h
    fingerprint_reader.cpp
    identifier.h
    identifier.cpp
    operation.h
    operation.cpp
    service.h
    service.cpp
    template_store.h
    template_store.cpp
    user.h
    user.cpp

    plugin.h
    plugin.cpp)

if(ENABLE_QT6)
    qt_add_qml_module(biometryd-qml
        URI Biometryd
        VERSION 0.0
        SOURCES ${biometryd-qml_SOURCES}
        PLUGIN_TARGET biometryd-qml
        CLASS_NAME Plugin
        NO_GENERATE_PLUGIN_SOURCE
        NO_PLUGIN_OPTIONAL)
    qt_query_qml_module(biometryd-qml
        PLUGIN_TARGET module_plugin_target
        TARGET_PATH module_target_path
        QMLDIR module_qmldir
        TYPEINFO module_typeinfo
    )
    target_link_libraries(biometryd-qml PRIVATE
        Qt${QT_VERSION_MAJOR}::Qml
        biometry
        Threads::Threads)

    set(QML_PLUGIN_DIR "${CMAKE_INSTALL_LIBDIR}/qt${QT_VERSION_MAJOR}/qml/${module_target_path}")
    install(TARGETS "${module_plugin_target}" DESTINATION "${QML_PLUGIN_DIR}")
    install(FILES "${module_qmldir}" DESTINATION "${QML_PLUGIN_DIR}")
    install(FILES "${module_typeinfo}" DESTINATION "${QML_PLUGIN_DIR}")
else()
    add_library(
        biometryd-qml SHARED
        ${biometryd-qml_SOURCES})

    target_link_libraries(biometryd-qml biometry
        Threads::Threads
        Qt${QT_VERSION_MAJOR}::Core
        Qt${QT_VERSION_MAJOR}::Qml
        Qt${QT_VERSION_MAJOR}::Quick
    )

    # We make the module available to qml tests without
    # requiring installation of the module. With that, we
    # can test the module within the build env.
    configure_file(qmldir qmldir COPYONLY)

    set(PLUGIN_DIR ${CMAKE_INSTALL_FULL_LIBDIR}/qt${QT_VERSION_MAJOR}/qml/Biometryd)

    install(
        TARGETS biometryd-qml
        DESTINATION ${PLUGIN_DIR})

    install(
        FILES qmldir
        DESTINATION ${PLUGIN_DIR})

    if (NOT CMAKE_CROSSCOMPILING)
        add_custom_target(
            Content_generated_files ALL
            SOURCES ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes)

        add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes
            COMMAND qmlplugindump -notrelocatable Biometryd 0.0 ../ > ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes
            DEPENDS biometryd-qml
            WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
        )
        install(
            FILES ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes
            DESTINATION ${PLUGIN_DIR})
    endif()
endif()
