set(LAUNCHER_VERSION_HEADER "${SOURCE_DIR}/header/version.hpp")
set(LAUNCHER_VERSION_FILE "${SOURCE_DIR}/../version.txt")
set(LAUNCHER_VERSION "0.0.0")

set(SHOULD_UPDATE_FILE "1")

if(EXISTS ${LAUNCHER_VERSION_FILE})
  file(READ ${LAUNCHER_VERSION_FILE} LAUNCHER_VERSION)
  string(STRIP ${LAUNCHER_VERSION} LAUNCHER_VERSION)
  message(STATUS "LAUNCHER_VERSION: " ${LAUNCHER_VERSION})
else()
  message(
    WARNING "\"version.txt\" does not exist, unable to get launcher version.")
endif()

# update file if needed
if(EXISTS ${LAUNCHER_VERSION_HEADER})
  file(STRINGS ${LAUNCHER_VERSION_HEADER} match REGEX "${LAUNCHER_VERSION}")
  if(NOT "${match}" STREQUAL "")
    message(STATUS "version.hpp is up to date.")
    set(SHOULD_UPDATE_FILE "0")
  endif()
endif()

set(new_header "// Generated by CMake\n\n"
    "#define LAUNCHER_VERSION \"${LAUNCHER_VERSION}\"\n")

if("${SHOULD_UPDATE_FILE}" EQUAL "1")
  file(WRITE ${LAUNCHER_VERSION_HEADER} ${new_header})
endif()
