
# the root name of the qrc resources
set (qrc_NAMES
    debug
    desktop
    edit
    examiner
    find
    geoSelect
    keyDialog
    MacroEdit
    mail
    MvQOgcClient
    scmEditor
    uPlot
    window
    XServ
    )

if (ENABLE_CODES_UI)
    list(APPEND qrc_NAMES codes_ui)
endif()

set(style_NAMES
    light
    dark
)

# the base style, for each qrc name we have one *_light.qrc
# file in the git repo
set(base_STYLE light)

# extra styles: the qrc files for these styles are automatically generated from the
# base style
set(extra_STYLES
    dark
    )

#message(STATUS "current=${CMAKE_CURRENT_BINARY_DIR}")
#message(STATUS "src=${CMAKE_SOURCE_DIR}")
#message(STATUS "bin=${CMAKE_BINARY_DIR}")

# copy images to build dir
foreach( sn ${style_NAMES} )
    file(COPY ${sn} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)
endforeach()

# generate qrc files from the base style
set(qrc_FILES "")
foreach( n ${qrc_NAMES} )
    # build base qrc file name
    set(base_qrc "${n}_${base_STYLE}.qrc")
    # copy it into the build dir
    configure_file(${base_qrc} ${CMAKE_CURRENT_BINARY_DIR}/  COPYONLY)
    list(APPEND qrc_FILES  ${CMAKE_CURRENT_BINARY_DIR}/${base_qrc})
    ecbuild_debug("base_qrc=${base_qrc}")

    # generate qrc files for extra styles
    foreach( sn ${extra_STYLES} )
        set(extra_qrc_name "${n}_${sn}.qrc")
        set(extra_qrc ${CMAKE_CURRENT_BINARY_DIR}/${n}_${sn}.qrc)
        # modify image path in original base style qrc and saves a copy
        file(READ ${base_qrc} filedata)
        string( REPLACE "${base_STYLE}/" "${sn}/" filedata "${filedata}")
        file(WRITE  ${extra_qrc} "${filedata}")
        list(APPEND qrc_FILES ${extra_qrc})
     endforeach()
endforeach()

# the rcc files will be generated in this dir
make_directory(${CMAKE_BINARY_DIR}/share/metview/app-defaults/images)

# compile all qrc files into binary rcc files
set(rcc_FILES "")
foreach( f ${qrc_FILES} )
    get_filename_component(fname ${f} NAME)
    string(REGEX MATCH "^(.*)\\.[^.]*$" dummy ${fname})
    set(qrc_name ${CMAKE_MATCH_1})
    set(rcc_file ${CMAKE_BINARY_DIR}/share/metview/app-defaults/images/${qrc_name}.rcc)
    ecbuild_debug("f=${f} -> qrc_name=${qrc_name} rcc=${rcc_file}")
    if(METVIEW_QT6)
        QT6_ADD_BINARY_RESOURCES(${qrc_name} ${f} DESTINATION ${rcc_file})
    elseif(METVIEW_QT5)
        QT5_ADD_BINARY_RESOURCES(${qrc_name} ${f} DESTINATION ${rcc_file})
    endif()
    list(APPEND rcc_FILES ${rcc_file})
endforeach()


# ensure files are installed at install time
foreach( f ${rcc_FILES})
#    get_filename_component(fname ${f} NAME)
    install( FILES ${f}
             DESTINATION share/metview/app-defaults/images
             PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)

endforeach()

#if (ENABLE_CODES_UI)
#    set(codes_ui_qrc_NAMES
#        examiner
#        keyDialog
#        window
#        find
#        codes_ui
#    )
#    foreach( f ${rcc_FILES})
#        get_filename_component(fname ${f} NAME)
#        string(REGEX MATCH "^(.*)_.*$" dummy ${fname})
#        set(qrc_name ${CMAKE_MATCH_1})
#        message(STATUS "codes_ui qrc_name=${qrc_name}")
#        if (qrc_name IN_LIST codes_ui_qrc_NAMES)
#            install( FILES ${f}
#                     DESTINATION share/codes_ui/images
#                     PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
#        endif()
#    endforeach()
#endif()
