cmake_minimum_required(VERSION 3.16)

project(test_update_translation_macro)

find_package(Qt6 REQUIRED COMPONENTS Core LinguistTools)

function(create_app target)
    add_executable(${target}
        myi18nobject.cpp)

    target_include_directories(${target}
        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/some_dir)

    target_link_libraries(${target} Qt6::Core)
endfunction()

function(expect_target target)
    if(NOT TARGET ${target})
        message(FATAL_ERROR "Expected target '${target}' does not exist.")
    endif()
endfunction()

function(not_expect_target target)
    if(TARGET ${target})
        message(FATAL_ERROR "Target '${target}' exists unexpectedly.")
    endif()
endfunction()

function(expect_files_in_list list_var)
    set(file_names "")
    foreach(path IN LISTS "${list_var}")
        get_filename_component(file_name "${path}" NAME)
        list(APPEND file_names "${file_name}")
    endforeach()
    set(found_file_names "")
    foreach(expected_file_name IN LISTS ARGN)
        list(FIND file_names "${expected_file_name}" idx)
        if(idx EQUAL -1)
            message(FATAL_ERROR "Expected file name '${expected_file_name}' is not in "
                "'${list_var}'. Its value is '${${list_var}}'.")
        endif()
        list(APPEND found_file_names "${expected_file_name}")
    endforeach()
    list(REMOVE_ITEM file_names ${found_file_names})
    list(LENGTH file_names n)
    if(NOT n EQUAL 0)
        message(FATAL_ERROR "Unexpected file names in '${list_var}': ${file_names}\n"
            "Value of '${list_var}' is '${${list_var}}'.")
    endif()
endfunction()

# Test NO_GLOBAL_TARGET for lupdate.
create_app(app1)
qt6_add_lupdate(app1
    NO_GLOBAL_TARGET
    TS_FILES myobject_de.ts)
expect_target(app1_lupdate)
not_expect_target(app1_lrelease)
not_expect_target(update_translations)
not_expect_target(release_translations)

# Test NO_GLOBAL_TARGET for lrelease.
create_app(app2)
qt6_add_lrelease(app2
    NO_GLOBAL_TARGET
    TS_FILES myobject_de.ts myobject_en.ts
    QM_FILES_OUTPUT_VARIABLE qm_files)
not_expect_target(app2_lupdate)
expect_target(app2_lrelease)
not_expect_target(update_translations)
not_expect_target(release_translations)
expect_files_in_list(qm_files myobject_de.qm myobject_en.qm)

# Typical usage of qt_add_lupdate/qt_add_lrelease. Pass some options for good measure.
create_app(app3)
qt6_add_lupdate(app3
    TS_FILES myobject_no.ts myobject_fi.ts
    OPTIONS -source-language en_US)
qt6_add_lrelease(app3
    TS_FILES myobject_no.ts myobject_fi.ts
    OPTIONS -compress
    QM_FILES_OUTPUT_VARIABLE qm_files2)
expect_target(app3_lupdate)
expect_target(app3_lrelease)
expect_target(release_translations)
expect_files_in_list(qm_files2 myobject_no.qm myobject_fi.qm)

# Now do the same with qt6_add_translations.
create_app(app4)
qt6_add_translations(app4
    TS_FILES myobject_no.ts myobject_fi.ts
    QM_FILES_OUTPUT_VARIABLE qm_files
    LUPDATE_OPTIONS -source-language en_US
    LRELEASE_OPTIONS -compress)
expect_target(app4_lupdate)
expect_target(app4_lrelease)
expect_target(release_translations)
expect_files_in_list(qm_files myobject_no.qm myobject_fi.qm)

# Typical usage of qt_add_translations with a generated resource.
create_app(app5)
qt6_add_translations(app5
    TS_FILES myobject_ru.ts
    RESOURCE_PREFIX "/tränslehschns")

# qt_add_translations on a static lib with a generated resource and the default resource prefix.
# Extract the created resource targets.
add_library(staticlib1 STATIC
    myi18nobject.cpp)
target_include_directories(staticlib1
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/some_dir)
target_link_libraries(staticlib1 PRIVATE Qt6::Core)
set(staticlib1_resource_targets "")
qt6_add_translations(staticlib1
    TS_FILES myobject_da.ts
    OUTPUT_TARGETS staticlib1_resource_targets)
if("${staticlib1_resource_targets}" STREQUAL "")
    message(FATAL_ERROR "staticlib1_resource_targets is empty.")
endif()

# Explicitly specify SOURCES.
create_app(app6)
qt6_add_translations(app6
    TS_FILES myobject_cs.ts
    SOURCES myi18nobject.cpp)

# Explicitly specify INCLUDE_DIRECTORIES.
create_app(app7)
qt6_add_translations(app7
    TS_FILES myobject_nl.ts
    INCLUDE_DIRECTORIES some_dir)

# Build the update_translations target
add_custom_target(my_all_target
    ALL
    DEPENDS update_translations)
