include_guard()
include(ReadVersion)

function(clear_variable VAR)
    if (NOT ${VAR})
        set(${VAR} "" PARENT_SCOPE)
    endif ()
endfunction()

function(generate_meta_info OUT_FILE_NAME)
    set(options DESCRIPTION FILE_VERSION TEMPLATE_FILE_PATH COMMENTS COMPANY COPYRIGHT TRADEMARKS BUILD_INFO)
    set(oneValueArgs MODULE_NAME PRODUCT_NAME ORIGINAL_FILE_NAME PRODUCT_VERSION ICON)
    set(multiValueArgs)
    cmake_parse_arguments(
            ARG
            "${options}"
            "${oneValueArgs}"
            "${multiValueArgs}"
            ${ARGN}
    )
    if (NOT DEFINED CURRENT_MODULE_TYPE)
        message(FATAL_ERROR "CURRENT_MODULE_TYPE not defined, must use set_submodule_library or set_submodule_executable first")
    else ()
        if (CURRENT_MODULE_TYPE MATCHES "LIBRARY")
            if (WIN32)
                set(EXT_FILENAME ".dll")
            endif ()
        elseif (CURRENT_MODULE_TYPE MATCHES "EXECUTABLE")
            if (WIN32)
                set(EXT_FILENAME ".exe")
            endif ()
        endif ()
    endif ()
    set(DEFAULT_TEMPLATE_FILE_PATH ${CMAKE_SOURCE_DIR}/res/meta_info.rc.in)
    if (NOT ARG_TEMPLATE_FILE_PATH)
        if (NOT EXISTS ${DEFAULT_TEMPLATE_FILE_PATH})
            message(FATAL_ERROR "meta info template file not exist at ${CMAKE_SOURCE_DIR}/res/meta_info.rc.in\n"
                    "please set it with TEMPLATE_FILE_PATH parameter or copy file at ${CMAKE_SOURCE_DIR}/res/meta_info.rc.in\n")
        else ()
            set(ARG_TEMPLATE_FILE_PATH ${DEFAULT_TEMPLATE_FILE_PATH})
        endif ()
    endif ()

    set(RC_VERSION_MAJOR ${GENERAL_VERSION_MAJOR})
    set(RC_VERSION_MINOR ${GENERAL_VERSION_MINOR})
    set(RC_VERSION_PATCH ${GENERAL_VERSION_PATCH})
    set(RC_VERSION_TWEAK ${GENERAL_VERSION_TWEAK})

    clear_variable(ARG_FILE_VERSION)
    #    clear_variable(ARG_DESCRIPTION)
    clear_variable(ARG_COMMENTS)
    clear_variable(ARG_COMPANY)
    #    clear_variable(ARG_COPYRIGHT)
    clear_variable(ARG_BUILD_INFO)
    clear_variable(ARG_TRADEMARKS)

    if (NOT ARG_DESCRIPTION)
        set(ARG_DESCRIPTION ${ARG_ORIGINAL_FILE_NAME})
    endif ()
    if (NOT ARG_COPYRIGHT)
        string(TIMESTAMP DATE "%Y")
        set(YEAR_START "2020")
        if (NOT YEAR_START STREQUAL DATE)
            set(DATE "${YEAR_START}-${DATE}")
        endif ()
        set(ARG_COPYRIGHT "Copyright(C) ${DATE} All right reserved.")
    endif ()

    if (NOT ARG_ICON OR CURRENT_MODULE_TYPE MATCHES "LIBRARY")
        set(RC_ICON "")
    else ()
        set(RC_ICON "IDI_ICON1 ICON DISCARDABLE \"${ARG_ICON}\"")
    endif ()
    set(RC_VERSION_NUM ${RC_VERSION_MAJOR},${RC_VERSION_MINOR},${RC_VERSION_PATCH},${RC_VERSION_TWEAK})
    set(RC_VERSION_STR "\"${RC_VERSION_MAJOR}.${RC_VERSION_MINOR}.${RC_VERSION_PATCH}.${RC_VERSION_TWEAK}\"")
    set(RC_FILE_VERSION_NUM ${RC_VERSION_NUM})
    set(RC_PRODUCT_VERSION_NUM ${RC_VERSION_NUM})
    set(RC_FILE_VERSION "\"${ARG_FILE_VERSION}\"")
    set(RC_PRODUCT_VERSION ${RC_VERSION_STR})
    set(RC_COMMENTS "\"${ARG_COMMENTS}\"")
    set(RC_COMPANY_NAME "\"${ARG_COMPANY}\"")
    set(RC_FILE_DESCRIPTION "\"${ARG_DESCRIPTION}\"")
    set(RC_INTERNAL_NAME "\"${ARG_PRODUCT_NAME}\"")
    set(RC_LEGAL_COPYRIGHT "\"${ARG_COPYRIGHT}\"")
    set(RC_ORIGINAL_FILE_NAME "\"${ARG_ORIGINAL_FILE_NAME}${EXT_FILENAME}\"")
    set(RC_PRODUCT_NAME "\"${ARG_PRODUCT_NAME}\"")
    set(RC_PRIVATE_BUILD "\"${ARG_BUILD_INFO}\"")
    set(RC_LEGAL_TRADEMARKS "\"${ARG_TRADEMARKS}\"")
    set(RC_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/${ARG_MODULE_NAME}.rc)
    configure_file(${ARG_TEMPLATE_FILE_PATH} ${RC_FILE_NAME} @ONLY)
    set(${OUT_FILE_NAME} ${RC_FILE_NAME} PARENT_SCOPE)
endfunction()