# SPDX-License-Identifier: GPL-3.0-only

# Set things whether or not things worked
if(${IN_GIT_REPO} STREQUAL "TRUE")
    execute_process(
        COMMAND ${GIT_EXECUTABLE} --git-dir "${GIT_DIR}" rev-parse --abbrev-ref HEAD
        OUTPUT_VARIABLE INVADER_GIT_BRANCH
    )
    execute_process(
        COMMAND ${GIT_EXECUTABLE} --git-dir "${GIT_DIR}" rev-parse --short HEAD
        OUTPUT_VARIABLE INVADER_GIT_COMMIT
    )
    execute_process(
        COMMAND ${GIT_EXECUTABLE} --git-dir "${GIT_DIR}" rev-list --count HEAD
        OUTPUT_VARIABLE INVADER_GIT_COMMIT_COUNT
    )
    string(STRIP "${INVADER_GIT_COMMIT_COUNT}" INVADER_GIT_COMMIT_COUNT)
    string(STRIP "${INVADER_GIT_COMMIT}" INVADER_GIT_COMMIT)

    set(INVADER_FULL_VERSION_TEXT "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.r${INVADER_GIT_COMMIT_COUNT}")
else()
    set(INVADER_GIT_BRANCH "unknown")
    set(INVADER_GIT_COMMIT "unknown")
    set(INVADER_GIT_COMMIT_COUNT 0)

    set(INVADER_FULL_VERSION_TEXT "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
endif()

# Overwrite version_str.hpp
file(WRITE "${OUT_FILE}" "#define INVADER_VERSION_STRING \"${INVADER_FULL_VERSION_TEXT}\"\n#define INVADER_VERSION_COMMIT_HASH \"${INVADER_GIT_COMMIT}\"\n#define INVADER_VERSION_COMMIT_COUNT ${INVADER_GIT_COMMIT_COUNT}\n#define INVADER_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}\n#define INVADER_VERSION_MINOR ${PROJECT_VERSION_MINOR}\n#define INVADER_VERSION_PATCH ${PROJECT_VERSION_PATCH}\n#define INVADER_FORK \"${PROJECT_NAME}\"")
