# this script is intenden to be used in cmake -P mode in an target

find_package ( Git QUIET )
if( NOT GIT_FOUND )
    message( WARNING "git was not found." )
endif()

find_path( GIT_DIR_BASE .git "${CMAKE_SOURCE_DIR}" NO_DEFAULT_PATH )
if ( NOT GIT_DIR_BASE )
    message ( WARNING "git directory was not found." )
endif ()

if(NOT DEFINED OUTPUT_FILE )
    message( FATAL_ERROR "OUTPUT_FILE is needed." )
endif ()

if(NOT DEFINED PROJECT_BASENAME )
    message( FATAL_ERROR "PROJECT_BASENAME is needed." )
endif ()



set ( PROJECT_VERSION_MAJOR "0" )
set ( PROJECT_VERSION_MINOR "0" )
set ( PROJECT_VERSION_PATCH "0" )
set ( PROJECT_VERSION_SHA1 "0" )
set ( PROJECT_VERSION_COMMITS_SINCE_LAST_TAG "0" )
set ( PROJECT_VERSION_SHORT "v0_custom_build" )

function ( copy_if_new  FILE_NEW FILD_OLD )

    if ( NOT EXISTS "${FILD_OLD}" )
        execute_process( COMMAND ${CMAKE_COMMAND} -E copy "${FILE_NEW}" "${FILD_OLD}" )
    else ()
        execute_process( COMMAND ${CMAKE_COMMAND} -E compare_files ${FILE_NEW} ${FILD_OLD}
            RESULT_VARIABLE OUTPUT_IS_NOT_SAME
            OUTPUT_QUIET
            ERROR_QUIET
        )

        if ( ${OUTPUT_IS_NOT_SAME} )
            message( STATUS "GIT Vesion Header file ${FILD_OLD} changed, you need to re-run the build process to include the updated version information." )
            execute_process( COMMAND ${CMAKE_COMMAND} -E copy "${FILE_NEW}" "${OUTPUT_FILE}" )
        else ()
            #message( STATUS "${FILD_OLD} not changed" )
        endif ()
    endif ()

endfunction( copy_if_new )


if ( GIT_FOUND AND GIT_DIR_BASE )

    execute_process ( COMMAND ${GIT_EXECUTABLE} describe HEAD --abbrev=7 --long
                      OUTPUT_VARIABLE PROJECT_GIT_VERSION
                      OUTPUT_STRIP_TRAILING_WHITESPACE 
                      WORKING_DIRECTORY ${GIT_DIR_BASE} )

    string ( REGEX REPLACE "-" "." PROJECT_GIT_VERSION "${PROJECT_GIT_VERSION}" )
    
    string ( REGEX REPLACE "^v([0-9]+)\\..*" "\\1" PROJECT_VERSION_MAJOR "${PROJECT_GIT_VERSION}" )
    string ( REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" PROJECT_VERSION_MINOR "${PROJECT_GIT_VERSION}" )
    string ( REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PROJECT_VERSION_PATCH "${PROJECT_GIT_VERSION}" )
    string ( REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+[^g]*g(.*)" "\\1" PROJECT_VERSION_SHA1 "${PROJECT_GIT_VERSION}" )
 
    string ( REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\.([^g]*)\\..*" "\\1" PROJECT_VERSION_COMMITS_SINCE_LAST_TAG "${PROJECT_GIT_VERSION}" )
    set ( PROJECT_VERSION_SHORT "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" )

    set ( OUTPUT_FILE_TEMP  "${OUTPUT_FILE}.temp" )
    file( WRITE "${OUTPUT_FILE_TEMP}" 
        "#pragma once\n" 
        "// this file was generated by a cmake script ${CMAKE_CURRENT_LIST_FILE}\n" 
        "// see also CMakeModules/CreateVersionFile.cmake\n"
        "#define ${PROJECT_BASENAME}_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}\n" 
        "#define ${PROJECT_BASENAME}_VERSION_MINOR ${PROJECT_VERSION_MINOR}\n" 
        "#define ${PROJECT_BASENAME}_VERSION_PATCH ${PROJECT_VERSION_PATCH}\n" 
        "#define ${PROJECT_BASENAME}_VERSION_SHA1 \"${PROJECT_VERSION_SHA1}\"\n" 
        "#define ${PROJECT_BASENAME}_VERSION_COMMITS_SINCE_LAST_TAG ${PROJECT_VERSION_COMMITS_SINCE_LAST_TAG}\n" 
        "#define ${PROJECT_BASENAME}_VERSION_SHORT \"${PROJECT_VERSION_SHORT}\"\n" 
        "#define ${PROJECT_BASENAME}_VERSION_CUSTOM \"${CUSTOM_USER_VERSION}\"\n" 
    )
    
    copy_if_new( "${OUTPUT_FILE_TEMP}" "${OUTPUT_FILE}" )
    
endif()


