find_package(Git QUIET)

function(GIT_HELPER_SUBMODULES_UPDATE)
    if(NOT GIT_FOUND)
        message(STATUS "Git is not found theere submodules cannot be cloned")
        return()
    endif()
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
        execute_process(COMMAND "${GIT_EXECUTABLE}" submodule update --init --recursive
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE _RESULT)
        if(NOT _RESULT EQUAL "0")
            message(FATAL_ERROR "Command: `git submodule update --init --recursive` failed with ${_RESULT}")
        endif()
    else()
        execute_process(COMMAND "${GIT_EXECUTABLE}" config -f .gitmodules --name-only --get-regex submodule.*.path
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE _RESULT OUTPUT_VARIABLE _OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE)
        string(REGEX REPLACE "[\n\r]+" ";" _KEYS "${_OUTPUT}")
        string(REGEX REPLACE "submodule\\.([^\\.]*)\\.path" "\\1" _KEYS "${_KEYS}")
        foreach(_KEY IN LISTS _KEYS)
                execute_process(COMMAND "${GIT_EXECUTABLE}" config -f .gitmodules --get submodule.${_KEY}.path
                                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE _RESULT OUTPUT_VARIABLE _OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE)
                if(_RESULT EQUAL "0")
                    set(_KEY_PATH ${_OUTPUT})
                else()
                    message(FATAL_ERROR "Command: `git config -f .gitmodules --get submodule.${_KEY}.path` failed with ${_RESULT}")
                endif()
                execute_process(COMMAND "${GIT_EXECUTABLE}" config -f .gitmodules --get submodule.${_KEY}.url
                                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE _RESULT OUTPUT_VARIABLE _OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE)
                if(_RESULT EQUAL "0")
                    set(_KEY_URL ${_OUTPUT})
                else()
                    message(FATAL_ERROR "Command: `git config -f .gitmodules --get submodule.${_KEY}.url` failed with ${_RESULT}")
                endif()
                execute_process(COMMAND "${GIT_EXECUTABLE}" clone --recurse-submodules "${_KEY_URL}" "${_KEY_PATH}"
                                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE _RESULT OUTPUT_VARIABLE _OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE)
                if(NOT _RESULT EQUAL "0")
                    message(FATAL_ERROR "Command: `git clone --recurse-submodules ${_KEY_URL} ${_KEY_PATH}` failed with ${_RESULT}")
                endif()
        endforeach()
    endif()
endfunction()

function(GIT_HELPER_COMMIT_HASH GIT_COMMIT_VARIABLE)
    if(NOT GIT_FOUND)
        message(STATUS "Git is not found. Cannot calculate commit hash")
        return()
    endif()
    execute_process(COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
                    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" RESULT_VARIABLE _RESULT OUTPUT_VARIABLE _OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(NOT _RESULT EQUAL "0")
        message(WARNING "Command: `git rev-parse HEAD` failed with ${_RESULT}")
        set(${GIT_COMMIT_VARIABLE}_NOTFOUND ${_OUTPUT} PARENT_SCOPE)
        file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/.git_commit_notfound" ${_OUTPUT})
    else()
        set(${GIT_COMMIT_VARIABLE} ${_OUTPUT} PARENT_SCOPE)
        file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/.git_commit" ${_OUTPUT})
    endif()
endfunction()

if(NOT DEFINED CMAKE_ARGC)
    return()
endif()

if(NOT GIT_FOUND)
    message(FATAL_ERROR "Git is not found. Cannot calculate commit hash")
endif()

git_helper_commit_hash(GIT_COMMIT)
